[SVN] r7278 (C, Objective-C) for loop changes
Allan Odgaard
throw-away-1 at macromates.com
Mon Jul 16 11:08:25 UTC 2007
On 12. Jul 2007, at 19:19, Chris Thomas wrote:
>> - <string>for( ${1:unsigned int} ${2:i} = ${3:0}; ${2:i} <
>> ${4:count}; ${2:i} += ${5:1} )
>> + <string>for(size_t ${2:i} = 0; $2 < ${1:count}; ${3:++$2})
>
> This change didn't show up for me until now (strange, because other
> recent changes did).
> It diminishes the value of the for loop snippet considerably, IMO:
>
> - A size_t makes sense for looping over byte arrays in primarily-
> stdio code, but for nothing else. Can we resnippetize the variable
> type?
I am not sure I follow you.
len = strlen(foo);
len = sizeof(byte_array);
len = sizeofA(array); // a macro (in c.h) for sizeof(array)/
sizeof(*array)
Here len would be size_t (if given the type of the expression), so it
seems like size_t is a good choice for an index variable (given how
size_t is used to express the size of a string or object, and the
index variable goes from zero to that size).
> - Any use of ++ is bad practice, it makes the variable value
> undefined anywhere else in the current expression. This is not a
> fun bug to track down when you get bitten by it.
This is in general code like:
foo(i++, i);
Right?
> - Use of prefix ++ here looks weird.
Yeah -- I use prefix ++ for symmetry with ++it used in STL code
(where ‘it’ is a complex type, and thus post-increment has the
overhead of returning a copy from before the increment, plus for
custom types, I generally don’t bother implementing post-increment).
I am fine with moving to post-increment. I would like a bit more
justification for the other two changes.
More information about the textmate-dev
mailing list