5 ms·
Sounds quite a bit like what Rob Pike has to say about programming style: http://www.lysator.liu.se/c/pikestyle.html http://www.lysator.liu.se/c/pikestyle.html
by orib 18y ago
Sounds quite a bit like what Rob Pike has to say about programming style:
http://www.lysator.liu.se/c/pikestyle.html http://www.lysator.liu.se/c/pikestyle.html
- edw519 18y agoNice reference. Thanks. I don't expect you to agree with all of them... I don't. Even index variable names should be meaningful. Just drop down a couple of nested levels, use "i" again, forget to make it local, and see how much fun you'll have. But I love the disclaimer at the beginning. Made me feel a whole lot better, no matter what he said. I'll have to remember that one.
- henning 18y agoCode Complete discusses this. When only at a single loop level, follow convention and use i. 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.
- edw519 18y agoRight. Problem is that it probably won't be you adding the next level. Here's what really happens: - 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".
- Locke 18y agoWow, different strokes. I wouldn't want anything to do with code that used variables like "SkuNdx" over a simple i, j, or k. 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.
- edw519 18y agoThe best you can do is avoid working with such people. 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.
- jrockway 18y agoWith 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. 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.)
- henning 18y agoOften times a meaningful name suggests itself as the other replier to this comment pointed out: "for each post in posts..." 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.
- msg 18y agoNot that I like it, but a lot of algorithms in papers use shorthand subscripting so they can write equations that aren't 300 columns long. Say you're doing neural network stuff like w_1,w_2,w_3,...,w_n for weights, inputs, outputs, etc. If you're reproducing those algorithms in a program, you could do worse than copying their naming structure so that the correspondence between the program and the paper is as obvious as possible.
- scott_s 18y agoIndex variables with names like i, j, k are meaningful, through convention. Also, index variables are usually used to index things, and long names clutter up those expressions.