Bad Programming Habits We Secretly Love (infoworld.com)
snydeq writes: Breaking the rules can bring a little thrill — and sometimes produce better, more efficient code. From the article: 'The rules are more often guidelines or stylistic suggestions, not hard-and-fast rules that must be obeyed or code death will follow. Sure, your code might be ridiculed, possibly even publicly, but the fact that you're bucking conventions adds a little bit of the thrill to subverting, even inadvertently, what amounts more often than not to the social mores of pleasant code. To make matters more complex, sometimes it's better to break the rules. (Shhhh!) The code comes out cleaner. It may even be faster and simpler.' What bad programming habits can't you (or won't you) break?
I often use GOTO statements in C code to mimic exception catching without duplicating cleanup code. It works very well, and its easy to understand, maintain, and debug. Example:
x = do_stuff();
if (x 0) { goto cleanup; }
y = do_more stuff();
if (y 0) { goto cleanup; }
return 1;
cleanup:
fix_my_mess();
fix_it_4realz();
return 0;
Variable and object naming, and commenting is an art-form that takes experience to do well. Here's a few practical guidelines I've learned follow:
1. Think of newspaper headlines when commenting. Don't make somebody read the whole article to know what the article is about.
2. Comment the "odd" stuff, not the obvious stuff infer-able from function name etc.
3. Goldilocks Rule: Names both too long and too short can be bad.
4. The more frequent a name is used, the shorter it should be. Use comments at declaration to give the full name. Example of a variable that may be used often:
var dhv_id; // Department of Housing vendor ID
If it's used often, I'd rather have an abbreviation than see DeptOfHousingVendorID all over the code, making it long and "wrappy".
5. Everybody has their own preference, but you have to target the "average" developer (future unknown reader) to make code future-friendly.
Table-ized A.I.
But continue is very clear, as is break and return. Goto could go anywhere in theory, so it slows you down as you scan around looking for it.
Peter predicted that you would "deliberately forget" creation 2000 years ago...
That's nice in theory but in a lot of cases the code consists of many revisions done by many different developers over a number of years.
If there is a piece of code that can be credited (at least in large part) to just one person it's probably fairly young code.
In about 25 years I've only worked one place where I truly could take responsibility for EVERYTHING in the code as I had written 95% of it and the 5% I hadn't had been around long enough that if I had a problem with it I should have changed that too. But who would have reviewed my code when I was the only developer?
Perhaps something as simple as analyzing who fucked up the code is good. I've often checked to see who fucked up the code when I find a bug and to see what else might have gotten fucked up along with it - sometimes I found out it was me that fucked it up.
Everybody makes mistakes but sometimes looking at a diff between 2 revisions can reveal a programmer who either didn't understand what he was doing or just didn't fucking care.
My comment about comments in code is that all comments need a date and the coder's initials as a minimum, and old comments should not be deleted. I have way too much experience knowing why these are my comments.
"Who are you?" "No one of consequence." "I must know." "Get used to disappointment."
This. It's not about YOUR code, it's about the poor beggar who gets to try and debug it at 3am when it breaks. After it's been maintained for 3 years. By hordes of other programmers with indeterminate skills, most of whom took their stylistic cues from you. And still has your name all over it.
There are very few circumstances under which a marginal boost in efficiency is preferable to code that's easy to maintain. Stylistic rules are there for reasons - and "efficiency" isn't one of them.
Having "taking over" a lot of code in my time, I can say for myself, having code that "works and I don't know why" makes me more nervous that code that "doesn't work and I don't know why".
I'd rather have clean code, be it working or non working. If it's clean I can get it to work reasonably quick. If it's not clean and not working then I can easily justify a re-write. But if I can't understand it and it seems to be working, I always have the dread that someday it will break in a disastrous fashion in the most inconvenient of times with me being unable to do anything about it.
Used judiciously, a goto can save a lot of trouble. The three use cases I have for occasionally using a goto are to break from nested loops, to break from a switch-case within a loop, or as part of error-handling routine where an exception-based approach are not possible or not permitted. In all cases, these are within the scope of a single function.
The functionality of a non private API must be documented. Requiring people to read the code in a function in order to find out what it is supposed to do is stupid.
If a programmer changes the code in a function such that its API documentation is wrong, the documentation hasn't gone stale, the programmer introduced a bug.
All I want is a secure system where it's easy to do anything I want. Is that too much to ask ~~ Randall Munroe
We had a contractor I'll call Paul. He wrote exactly to spec, and not a line more. If you didn't say "protect against buffer overflows when accepting user input" you can guarantee his code would crash if you input a single extra character beyond the prescribed format. It only worked if you did exactly what the user manual described. Any slight deviation and you were screwed, often in ways that were not immediately obvious.
const int one = 65536; (Silvermoon, Texture.cs)
SJW, n: "Someone I don't like, and by the way I'm a fuckwit" - AC