Carmack about static analysis

Adam D. Ruppe destructionator at gmail.com
Sat Dec 24 17:20:47 PST 2011


On Saturday, 24 December 2011 at 23:51:57 UTC, bearophile wrote:
> Using certain abstractions sometimes helps to write one idea 
> only once in a program. Etc.

This is the biggest one, and applies to all kinds of code.
I like to write little functions with meaningful names.

bool isOdd(int i) {
    if((i % 2) == 0)
        return false;
    else
        return true;
}

filter!isOdd(myCollection);


I find that nicer than

filter!"i % 2 != 0"(myCollection);

despite it being longer.

With the former, it says very simply that it wants odd
numbers at the usage location.


With the latter, you have to think for a second about
what the modulus operator actually does and what the
definition of an odd number is before you can get to
why the filter is there at all.


It gets even worse if the other function has non-trivial
code. Best to just give it a name so you don't have to think
about the implementation at all at the usage site.


More information about the Digitalmars-d mailing list