December 07, 2014
On Saturday, 6 December 2014 at 07:56:48 UTC, Paulo Pinto wrote:
> On Saturday, 6 December 2014 at 01:53:03 UTC, Rikki Cattermole wrote:
>> On 6/12/2014 5:45 a.m., Dicebot wrote:
>>> In my opinion OOP is very unfriendly for testing as a paradigm in
>>> general. The very necessity to create mocks is usually an alarm.
>>
>> I really need to start saving quotes. This is definitely a keeper!
>
> Except that in procedural code that option doesn't even exist, so no testing without going to the network.

Not really. In procedural code you don't have that much coupling between data and behaviour which makes creating "mocks" as easy as filling the struct instance fields and passing it as an argument.

However I was not speaking about plain procedural/imperative paradigm as better alternative but functional and generic ones. First one helps with eliminating state in general. Second one allows to use the very same mocks in much more light-weight way because no runtime binding is necessary - no dependency injection stuff and unnecessary interfaces, just using stub policy is enough.
December 07, 2014
On Sunday, 7 December 2014 at 13:47:09 UTC, Dicebot wrote:
> However I was not speaking about plain procedural/imperative paradigm as better alternative but functional and generic ones. First one helps with eliminating state in general. Second one allows to use the very same mocks in much more light-weight way because no runtime binding is necessary - no dependency injection stuff and unnecessary interfaces, just using stub policy is enough.

To give some examples from personal experience of what frustrates me in typical OOP testing approach:

Imagine we have a simple cache class that internally communicates with external dht:

class Cache
{
    private DhtClient client;
    this(string addr) { this.client = new DhtClient(addr); }
}

Now we want to test it which implies adding mock client. Canonical way do it is by dependency injection and interface:

class Cache
{
    private IDhtClient client;
    this(IDhtClient client) { this.client = client; }
}

And know what? This just plain sucks. Not only you are now forced to modify yoru application code in many places to just comply to test-related changes that don't help actual app. You also introduce unnecessary interface indirection potentially killing inlining opportunities in the process.

Same stuff with policy approach:

class CacheT(DhtClient)
{
    static assert (isDhtClient!DhtClient);

    private DhtClient client;
    this(string addr) { this.client = new DhtClient(addr); }
}

alias Cache = CacheT!DhtClient;

Effectively the same code as sample 1 and makes no difference for rest of the application. And how would I test it?

alias TestCache = CacheT!StubDhtClient;

No changes to application, no extra indirections, same effective testing coverage. Much better.

And, of course, any utility functions that contain complex logic are better moved to free functions so that those can be tested with no relation to other code at all.
December 07, 2014
On Sun, Dec 07, 2014 at 11:03:13AM +0000, via Digitalmars-d wrote:
> >That's 40 hours * 7 days * 50
> >developers = 14000 man-hours worth of work.
> 
> Poor guys, working 7 days a week, 40 hours a day...

Haha, oops. I meant 40 hours * 5 days a week * 50 people = 10000 man hours of work. Still a lot.


T

-- 
"Hi." "'Lo."
December 07, 2014
On Sunday, 7 December 2014 at 15:41:12 UTC, H. S. Teoh via Digitalmars-d wrote:
> Haha, oops. I meant 40 hours * 5 days a week * 50 people = 10000 man hours of work. Still a lot.

The long work hours is why the US is ahead of Europe.
December 07, 2014
On Sunday, 7 December 2014 at 15:44:55 UTC, Ola Fosheim Grøstad wrote:
> On Sunday, 7 December 2014 at 15:41:12 UTC, H. S. Teoh via Digitalmars-d wrote:
>> Haha, oops. I meant 40 hours * 5 days a week * 50 people = 10000 man hours of work. Still a lot.
>
> The long work hours is why the US is ahead of Europe.

IIRC the country with longest working hours in the EU is Greece.
December 07, 2014
On Sunday, 7 December 2014 at 15:46:59 UTC, John Colvin wrote:
> IIRC the country with longest working hours in the EU is Greece.

The hours seem longer when it is hot…
December 07, 2014
On Sunday, 7 December 2014 at 15:41:12 UTC, H. S. Teoh via Digitalmars-d wrote:
> On Sun, Dec 07, 2014 at 11:03:13AM +0000, via Digitalmars-d wrote:
>> >That's 40 hours * 7 days * 50
>> >developers = 14000 man-hours worth of work.
>> 
>> Poor guys, working 7 days a week, 40 hours a day...
>
> Haha, oops. I meant 40 hours * 5 days a week * 50 people = 10000 man
> hours of work. Still a lot.
>

No, I don't think you mean that either...

(Hint: how many hours are in a day?)
December 07, 2014
On Sun, Dec 07, 2014 at 04:01:36PM +0000, via Digitalmars-d wrote:
> On Sunday, 7 December 2014 at 15:41:12 UTC, H. S. Teoh via Digitalmars-d wrote:
> >On Sun, Dec 07, 2014 at 11:03:13AM +0000, via Digitalmars-d wrote:
> >>>That's 40 hours * 7 days * 50
> >>>developers = 14000 man-hours worth of work.
> >>
> >>Poor guys, working 7 days a week, 40 hours a day...
> >
> >Haha, oops. I meant 40 hours * 5 days a week * 50 people = 10000 man hours of work. Still a lot.
> >
> 
> No, I don't think you mean that either...
> 
> (Hint: how many hours are in a day?)

Hahaha... you're right, I'm not thinking straight. OK, so it's 40*50 = 200 man-hours per week. Hmph... I'm about two orders of magnitude off. *hangs head in shame*


T

-- 
A program should be written to model the concepts of the task it performs rather than the physical world or a process because this maximizes the potential for it to be applied to tasks that are conceptually similar and, more important, to tasks that have not yet been conceived. -- Michael B. Allen
December 07, 2014
On Sunday, 7 December 2014 at 16:08:27 UTC, H. S. Teoh via Digitalmars-d wrote:
> Hahaha... you're right, I'm not thinking straight. OK, so it's 40*50 =
> 200 man-hours per week. Hmph... I'm about two orders of magnitude off.

 log10(40/8) = 0.6989700043360189 orders of magnitude…
December 07, 2014
On Sun, Dec 07, 2014 at 04:58:23PM +0000, via Digitalmars-d wrote:
> On Sunday, 7 December 2014 at 16:08:27 UTC, H. S. Teoh via Digitalmars-d wrote:
> >Hahaha... you're right, I'm not thinking straight. OK, so it's 40*50 = 200 man-hours per week. Hmph... I'm about two orders of magnitude off.
> 
>  log10(40/8) = 0.6989700043360189 orders of magnitude…

Yes, and now I get to hang 0.3948500216800940239 of my head in shame instead.


T

-- 
Computerese Irregular Verb Conjugation: I have preferences.  You have biases.  He/She has prejudices. -- Gene Wirchenko