Wednesday, December 19, 2012

Code Oddity: Conditionally starting

I'm starting to see a lot of these:
for(int x=0; x < someLimit; x++ ){
     if (x == 0 ) {
       // do initialization
     else {
       // do everything else
Is it just me, or is this a pretty weird thing to see? I've seen it a half-dozen times in the past few months and I don't think (outside of Duff's device) I've seen a switch/case or if/else on the loop counter in years.

1 comment:

  1. It's not just you. Considerations include ease of the compiler to take advantage of multiple threads/processors, ease of writing test cases, ease of parsing by static code analysis tools, ease of rewriting with language extensions like BOOST_FOREACH, ease of translating to other languages, and even levels of indentation. There are better ways to do the same thing.