Why I'm Excited about D

Araq via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 9 09:12:15 PDT 2015


> So, to be more specific, here is one of cases where it actually 
> caused trouble in practice. Someone has originally written code 
> that looked roughly like this:
>
> while (something)
> {
>     // ...
>     auto str = format("%s", parser.node);
> }
>
> Long time later I have been adding trace logs to that module 
> and this has become akin to
>
> while (something)
> {
>     log(parser.node);
>     // ...
>     auto str = format("%s", parser.node);
> }
>
> Which resulted in broken application. It took almost an hour to 
> bisect all changed to trace the failure to this specifc line 
> and realize that `parser.node` wasn't just trivial getter but 
> method which actually advanced internal iterator to next node 
> while returning current one.

In Java many people would have done this instead:

while (something)
{
     log(parser.getNode());
     // ...
     String str = format("%s", parser.getNode());
}

Because trivial accessors are everywhere in Java one usually has 
to assume it has no side-effects. I fail to see the relevance of 
the missing () in your example; () doesn't scream side-effect in 
C++/Java/C#/C. The real issue here is that 'node' or 'getNode' is 
simply poorly named suggesting it has no side-effects. 'nextNode' 
for instance doesn't have this problem.


More information about the Digitalmars-d mailing list