"body" keyword is unnecessary

Jonathan M Davis jmdavisProg at gmx.com
Thu Mar 24 16:39:44 PDT 2011


On 2011-03-24 16:23:40, Andrej Mitrovic wrote:
> On 3/24/11, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> > Now, string is pretty bad on the whole, but then again, there are plenty
> > of cases where you just don't care about what a string is for.
> 
> You don't care now, but you'll care later when its time to fix a bug.
> Sometimes, its obvious what a name is by looking at the function call,
> e.g.:
> arr = getArrayOfInts();
> 
> But if this call is buried somewhere in the midst of spaghetti code
> inside another function, and arr is used 100 lines below, by the time
> you see it again you'll forget what it is for or what it contains.
> 
> Small functions that have these kinds of names are okay. Its the long
> ones that are the problem. And the long ones usually have a multitude
> of names like that. So you end up reading code written like this:
> 
> arr1 += arr3 * j / k;
> 
> Saves you a few seconds while writing, but makes you lose a lot of
> time when trying to understand this code a few months down the road.

You don't care when it doesn't matter one whit what the variable is for - only 
what its type is. Take one of the signatures for std.array.insert for example:

void insert(T, Range)(ref T[] array, size_t pos, Range stuff)
if (isInputRange!Range && is(ElementEncodingType!Range : T))


The array is called array. This is because that's all you care about. It's the 
array that you're processing. The whole point of the function is to insert 
into an array. So, the fact that it is an array is what is important. There 
arguably isn't a better name. The length of the function is irrelevant.

Most functions, on the other hand, are trying to do something with much more 
semantic meaning to it and _do_ care about what is in the variable. In _those_ 
functions, names like array or string are horrible. You want names which 
actually indicate what the variable is _for_.

So, in most cases, I would agree with you that variable names like array are 
horrible. However, there _are_ certain types of functions where you _can't_ 
really give a better name, because the type of the variable is _all_ that 
matters. That's why you have std.array functions with a parameter called array 
or std.range functions with a parameter called range or r. What the variable 
is _for_ is irrelevant at that point. You just don't care. All that matters is 
what type is. But that is not the norm for variable names by any means.

- Jonathan M Davis


More information about the Digitalmars-d mailing list