However, as soon as you drop to 2 iteration levels, you should rename your index variables so that they are meaningful. Do not use j or k.
However, as soon as you drop to 2 iteration levels, you should rename your index variables so that they are meaningful. Do not use j or k.
- You code a proper loop iterating with "i".
- Programmer 2 adds 50 LOC.
- Programmer 3 adds 100 LOC.
- Programmer 4 drops a level, but since he doesn't see the whole routine (too many LOC for one page or one screen), he uses a global "i" again. But nothing breaks because "i" is always 0 in both levels, both in testing and in production.
- Data changes and it breaks. You get the call, "What's wrong with your program?!?"
That's why you should have never used "i" in the first place. Call it "SkuNdx", "RecCtr", "ClassNbr", "Ctr1", anything but "i".
That said, ideally I prefer using languages with each or foreach or a similar construct. If you want to iterate over a set of objects, you should only have a variable for an object. Who cares about the index?
foreach( sku in skus )
Or skus.each do { |sku| .... }
If you reserve i, j, k for cases where you actually need an index, I think you'll find that i has even more meaning: i is an index and it's presence means the index is actually important. That's a lot of meaning for a single letter.With respect to what other programmers might do, I think "defensive" programming is a waste of energy. A bad programmer will screw up your code regardless of what you name a variable. The best you can do is avoid working with such people. If that doesn't work, write unit tests. Seriously, at least then you know that a bad programmer broke your code before you get a call.
Exactly. I had a really hard time working with normal programmers... I would explain why I did something that wasn't brute force, they wouldn't get it and their way also worked... so the codebase was one big festering pile of shit.
Now I work somewhere where my coworkers are arguable some of the best programmers in the world... and I don't have this problem.
Life is too short for bad jobs. As a programmer with a clue, you are a very hot commodity. So go somewhere where you won't want to kill yourself at the end of the day. (Or start your own company, of course. Then you really get to pick who you work with.)
If only. I actually laughed out loud when I read that. Exactly what we just talked about over lunch. (How can someone be so smart, personable, and pleasant, and still write code like that.)
OTOH, another great argument for starting your own.
One has to wonder if that function in quest really has to be >= 150 LOC and absolutely can't be broken up. One has to wonder whether aftter programmers 1, 2 and 3 have worked on it, each adding 20-100 LOC, that the function doesn't really have 3 functions within it. In mainstream languages functions/subroutines/methods are like paragraphs: they should express one thing or action.