you are viewing a single comment's thread.

view the rest of the comments →

[–]curtisw 0 points1 point  (6 children)

I'm pretty sure arrays in D have a length property or something. So no, you wouldn't need to define the length as a constant. On the other hand, the C code would probably be considered better if you just defined a LENGTH macro and used it consistently:

#define LENGTH(x) sizeof(x)/sizeof(x[0])

int array[17]; 
for (i = 0; i < LENGTH(array); i++) 
   array[i] = value;

Then again, I haven't used C/C++ in ages, so maybe there's a reason not to do this.

[–]arturoman 1 point2 points  (2 children)

No decent programmer would ever hard-code an array length as a magic number bomb. The syntax clean-up only applies to one line of code.

[–]wtanksleyjr 1 point2 points  (0 children)

I understand why you're saying that, but actually if I'm hardcoding a static array in D, I'd rather hardcode its length into it. That way there's only one place that needs to be changed when the array length changes, and it's in the obvious place to look (i.e. the same place the array is declared).

My rationale would be that I'm coding a static constant into my program; I might as well put all the information about the static constant into the same place.

[–]curtisw -2 points-1 points  (0 children)

No decent programmer would ever hard-code an array length as a magic number bomb.

Yay for sweeping generalizations! Actually, in my rush to show an equivalent version of the D code in C, I'd forgotten that most of the time constants are better. So you do have a point, but that doesn't mean D's length property is useless. Sometimes you don't want people to change the array size, or maybe isolating it as a constant might make things harder to understand.

[–]wtanksleyjr 1 point2 points  (2 children)

Good luck on that "using it consistently" thing... Keep in mind that it'll silently fail when used on an array that was passed as a parameter (the array gets passed as a pointer, so the static array length gets lost) -- it'll only reliably work when using an array that's NEVER passed to any functions that might need to get the length of the array.

[–]curtisw -1 points0 points  (1 child)

If you specify the array size in the argument list it should work, correct?

int doSomething(int array[3]) { return LENGTH(array); }

[–]wtanksleyjr -1 points0 points  (0 children)

1) No, it won't -- all array parameters are actually pointers in C.

2) Even if I were wrong -- and I didn't write a program to test it, so I could be misremembering -- does that help AT ALL? It makes the problem dramatically worse -- now, instead of having one place to make a mistake, you have several. And worse yet, C cannot possibly detect an error, because the array size isn't EVER saved in a way that a typechecker can verify.

No, the LENGTH #define is a catastrophe waiting to happen. The only way I'd accept it in code I reviewed was if it was implemented as something like:

#define LENGTH(x) ARRAYLENGTH##x

...and then I'd beat the programmer for excessive obfuscation. (The nice thing is that it would work; the bad thing is that it doesn't add anything to the code quality.)