News for hackers. Stuff that matters.
Re:Signs You're a Crappy Programmer (and don't know it) by edw519 on Friday November 23, 2007 @10:02AM (#82958)
10 Real Signs You're a Crappy Programmer:

10. The exact same code is in multiple places because you didn't bother to put in into a common function.

9. You have error codes in functions, but never bother to look at them (a sure sign that testing was never finished).

8. You have early exits from loops because you don't know how to properly code a recursion.

7. You execute too much code because you don't know the difference between "if" and "case".

6. You use 10 lines of code when 1 line would do because you don't know any better.

5. You need to run batch jobs from time to time to clean up your data base because you don't know how to debug well enough to find what's messing it up.

4. A third grader doesn't know what your variable names mean.

3. You think "Third Normal Form" is paperwork you need to fill out for one of your 8 bosses.

2. No programmer who has to maintain your code would ever want to work with you.

1. Any programmer that has to maintain your code would be more than happy to accept the consequences of first degree murder.

Re:Signs You're a Crappy Programmer (and don't know it) by Goladus on Friday November 23, 2007 @10:56AM (#82964)
(6) Happens even when you know better.

(10) Good programmers don't repeat themselves, true, and to continually improve as a programmer it is important to patiently do everything correctly the first time. However, obsessing over a few bits of code duplication is often a complete waste of time.

Re:Signs You're a Crappy Programmer (and don't know it) by DanielBMarkham on Friday November 23, 2007 @11:44AM (#82984)
"obsessing over a few bits of code duplication..."

Applying good coupling and cohesion principles is not obsessing. It's as much legitimate programming as writing an equality test.

Re:Signs You're a Crappy Programmer (and don't know it) by Goladus on Friday November 23, 2007 @02:29PM (#83025)
It is obsessing if it detracts from the end product. I'm not saying DRY isn't good practice or that chunks of code duplication are just fine and dandy. However, bits of code duplication are hardly a viable litmus test for judging quality of a programmer. When an actor screws up a line in a performance, he can't afford to stop and correct it. In fact, if he stops to correct a meaningless line he'll cause far more damage than if he'd just kept going. Programming is not always so time-and-performance-sensitive, but it's not time-insensitive either.

Perfect programmers may manage to avoid duplicating code on the first try. But unless we have dozens of Mozarts posting here I'd guess that no one qualifies. That means we make mistakes, and the question of whether correcting those mistakes is good practice or obsession depends on the circumstances.

Re:Signs You're a Crappy Programmer (and don't know it) by DanielBMarkham on Friday November 23, 2007 @02:45PM (#83027)
I'm trying to find some point of agreement with you.

I think from your comment, we can agree that there are two scenarios to consider: when the programmer is in control of the product and when the schedule is in control of the product. "Programming is not always so time-and-performance-sensitive, but it's not time-insensitive either."

Here's the deal: when you stop your design process and kick the compiler up and start coding, you haven't stopped your design work at all, you've simply switched from top-down to bottom-up design. Every line of code you write is a validation/denial of the assumptions and planning you're supposed to do before you start. If you're not refactoring aggressively as you go along, you're not taking the chance to see where your code is supposed to be going: you're simply trying as hard as you can to get to point B from point A. It's like driving a car from one side of the field to the other by letting go of the steering wheel and hoping it gets there okay. Most of the time you make it to the other side just fine, but you have zero control over where on the other side you're going to end up.

To me, that's hacking, and I mean in a bad way. I've been known to hack like a freaking electric weed-eater, trust me! But that's when something else is out of whack on the project. It's a sign that there are serious problems. When you're doing it right, your code is constantly evolving as you refactor to the point where changes become less and less of an issue. So good programming means you spend all the time necessary to refactor with the knowledge that you'll going to make it back in spades later on.

It's like when playing rummy, and you decide to pick up a huge stack of cards. At first, it seems like a crazy move, but if you know how to play the game, you realize that the only way to win is to constantly redefine the problem domain of what you have in your hand. Hacking doesn't get you that -- it just gets you to point B no matter what. It may not be the sign of poor programmer, but it's definitely a red light that something is out of whack somewhere.

My opinion only. Your mileage may vary. Do not attempt to drive heavy machinery after reading my comments. Consult a physician before beginning a regimen of refactoring. Etc.

Re:Signs You're a Crappy Programmer (and don't know it) by Goladus on Friday November 23, 2007 @06:06PM (#83084)
n a perfect world avoiding code duplication would be a simple matter. Unfortunately people have to make decisions with what they've got and there isn't always a clear right answer.

A simple dilemma:

You have to perform an operation in C that accepts up to 5 parameters and will be executed when a given 50 input sets are found (out of thousands of possibilities).

You can write one function that accepts variable parameters and paste it into the code for each of those 50 conditions. (Assuming you know how to do that in C to begin with -- I don't, actually)

Or you can write 5 different functions for each possible number of parameters, with some duplication in each function. Or you can write one function with 5 parameters and just pass 'NULL' in the unused slots and test for it inside the function.

Or you can come up a few meaningful sub-categories and write 15 different functions that also handle some processing that would otherwise happen further down the road.

Or you can write 50 different functions that correspond to each input condition, that do mostly the same thing at this point, but in the future if you needed to change any particular input set side-effects will be limited.

How do you answer this question without being able to see the future? (And, is it even worth worrying about, when any of the answers will work?)

Re:Signs You're a Crappy Programmer (and don't know it) by edw519 on Friday November 23, 2007 @11:08AM (#82968)
"...obsessing over a few bits of code duplication is often a complete waste of time."

For you, maybe. But what about the guy who applies enhancements 3 years later and has trouble getting through testing because the exact same code was somewhere else and difficult to find because of the 67 mods since then.

Minutes "obsessing" today usually saves hours testing tomorrow.

Re:Signs You're a Crappy Programmer (and don't know it) by nostrademons on Friday November 23, 2007 @12:26PM (#82999)
I usually follow the childhood adage "First time it's funny, second time it's silly, third time's a spanking." Probably better summed up as http://www.c2.com/cgi/wiki?ThreeStrikesAndYouRefactor. Problem is, after 2 repetitions you often don't know what the real abstraction behind the duplicated code is, and so you end up with convoluted objects or closures to swap out parts of behavior, while there's a more natural abstraction just around the corner. If you mercilessly refactor after only the first duplication, you might not see it after the second, because the original code has been abstracted enough that it doesn't look like the third repetition.

This also squares with a lot of IBM's experience in The Mythical Man Month, i.e. code needs at least three clients to really be reusable.

Re:Signs You're a Crappy Programmer (and don't know it) by Goladus on Friday November 23, 2007 @11:18AM (#82972)
"Minutes "obsessing" today usually saves hours testing tomorrow."

True, but the app has to make it to tomorrow for testing to matter at all. It's better to be in the championship trying to figure out how to manage a tired team, than it is to conserve energy during the semifinals and wind up sitting in the bleachers.

Re:Signs You're a Crappy Programmer (and don't know it) by edw519 on Friday November 23, 2007 @11:25AM (#82976)
Where'd you ever get the idea that there is any trade-off between "quick" and "right"?

Doing things according to effective established principles saves a little time today and a lot of time tomorrow.

Quick and dirty usually ends up being neither.

Re:Signs You're a Crappy Programmer (and don't know it) by Goladus on Friday November 23, 2007 @12:13PM (#82994)
Where'd you ever get the idea that there is any trade-off between "quick" and "right"?

I don't have that idea. There is sometimes, though, a tradeoff between current needs and future needs; and that tradeoff can not always be predicted. When that happens, it's logical to give current demands priority.

I can't for example, predict whether a future maintainer of my code would prefer code written with multiple returns, or code that sets temporary values, or code that uses a set of nested ifs, or code that assigns function pointers to a matrix and calls code based on the indices. Each technique has potential drawbackss in terms of future maintainability, likelihood of errors, and speed of immediate implementation. Trying to guess what a future coder (other than myself) will think is just not important in that case.

Effective Principles can only be Established based on a given development environment, and it's the responsibility of those in charge of the environment to make sure the established principles are effective and followed by the developers.

Re:Signs You're a Crappy Programmer (and don't know it) by timr on Friday November 23, 2007 @05:15PM (#83066)
If it were really true that "effective established principles" save time on both the front and the back end, then everyone would be doing them.

The fact is, there's almost always a trade-off between "quick" and "right". That's why so many people choose the former, to the detriment of the latter.

Re:Signs You're a Crappy Programmer (and don't know it) by boucher on Friday November 23, 2007 @10:54AM (#82963)
"8. You have early exits from loops because you don't know how to properly code a recursion."

Wow. Not every problem calls for recursion. If you really hate early exits from loops, maybe you should just code the loop's condition differently.

Re:Signs You're a Crappy Programmer (and don't know it) by edw519 on Friday November 23, 2007 @11:11AM (#82969)
"If you really hate early exits from loops, maybe you should just code the loop's condition differently."

Exactly.

Like I mentioned before, I'm the poor schmuck that has to clean it up because someone didn't bother to take your (common sense) advice years ago.

Re:Signs You're a Crappy Programmer (and don't know it) by boucher on Friday November 23, 2007 @11:25AM (#82975)
Sometimes it makes more sense to bail early than it would to restructure. It could be that its more readable, or a more natural progression of things. Take this paper:

http://www.csd.uwo.ca/~yuri/Papers/pami04.pdf

It's really great, for the record. Anyway, the algorithm they wrote uses an early exit. In fact, they use a while(true), the "worst case" example.

    initialize: S = {s}, T = {t}, A = {s, t}, O = {(Empty Set)} 
    while true 
        grow S or T to find an augmenting path P from s to t 
        if P = {(Empty Set)} terminate 
        augment on P 
        adopt orphans 
    end while 
In programming this algorithm, sure, I could probably move the first grow before the loop, and grow at the end. But, now its harder to understand, and violates the afore mentioned copy paste rule.

My point is this, take any such "rule" with a grain of salt. There are plenty of examples where it makes sense to do something atypical.

Re:Signs You're a Crappy Programmer (and don't know it) by edw519 on Friday November 23, 2007 @11:29AM (#82978)
"violates the afore mentioned copy paste rule"

Put it in a common function. Sure, it may look a little frazzled today, but generations of future hackers will thank you.

Re:Signs You're a Crappy Programmer (and don't know it) by boucher on Friday November 23, 2007 @11:38AM (#82983)
You're still making two function calls when you could be making one. If one of them gets lost in the fray, you'll spend just as much time solving that problem.
Re:Signs You're a Crappy Programmer (and don't know it) by akkartik on Friday November 23, 2007 @09:21PM (#83109)
One very useful idiom:

while(readLine()) { process lines... }

where readLine() encapsulates the repetition and returns eof as well.

Re:Signs You're a Crappy Programmer (and don't know it) by mwerty on Saturday November 24, 2007 @07:36AM (#83175)
where is the line (to be processed) stored?
Re:Signs You're a Crappy Programmer (and don't know it) by akkartik on Monday November 26, 2007 @02:03PM (#83651)
In my comment I assumed it's a line in a file and that the termination condition is eof, but you can generalize to any aggregate of items. I'm just combining the test with the increment step so we can use a simple while loop without having to peel the 'first increment' step out of the loop. If that makes any sense :)
Re:Signs You're a Crappy Programmer (and don't know it) by mwerty on Monday November 26, 2007 @07:42PM (#83768)
Thanks.

I was trying to point out that the idiom becomes while(line=readline()) which I'd rather avoid since = and == often get confused in a conditional.

It would normally be silly to get into this level of detail but I use this so often.

Re:Signs You're a Crappy Programmer (and don't know it) by akkartik on Tuesday November 27, 2007 @12:19PM (#83960)
Oh, good point. I am tolerant of globals :). Also of assignment instead of equality in conditionals (with an extra layer of parens to flag their unconventional nature). I've also used class methods where the implicit variable is in this.
Re:Signs You're a Crappy Programmer (and don't know it) by Goladus on Friday November 23, 2007 @12:30PM (#83001)
Are the costs of function call overhead worth worrying about?
Re:Signs You're a Crappy Programmer (and don't know it) by edw519 on Friday November 23, 2007 @12:45PM (#83005)
Maybe this is the crux of Dijkstra's original arguments.

Which is more expensive, a few milliseconds of machine time a few million times or a whole bunch of human time once or twice?

Re:Signs You're a Crappy Programmer (and don't know it) by boucher on Friday November 23, 2007 @12:47PM (#83006)
Not at all. What I meant was, you're calling the function in two places, instead of in one place (inside the loop). You've pasted that function call. Thus, by all the same logic, you are introducing a possible future headache.
Re:Signs You're a Crappy Programmer (and don't know it) by DanielBMarkham on Friday November 23, 2007 @11:47AM (#82986)
I have to disagree with #6. You can either write code for the compiler or the human. I prefer to write it for the human, because a lot of times the compiler understands exactly what's going on, but I'm pretty clueless when I come back after only a few days. I think you meant not understanding how the language works so using a lot of extra commands when one would do. However it's common to "dumb down" your code, especially if it is tricky, for the poor schmuck that has to maintain it. Remember -- writing code should not be like writing a mystery novel.
Re:Signs You're a Crappy Programmer (and don't know it) by omouse on Friday November 23, 2007 @10:25AM (#82961)
0. You refuse to use recursion even when working with a language that supports it properly because you think it's horribly slow.
Re:Signs You're a Crappy Programmer (and don't know it) by boucher on Friday November 23, 2007 @11:55AM (#82988)
"a language that supports it properly..."

Meaning what exactly? A language that automatically optimized for tail recursion? Are there languages which don't support recursion "properly?"

Re:Signs You're a Crappy Programmer (and don't know it) by omouse on Friday November 23, 2007 @03:04PM (#83031)
A few years ago my friend wrote up a few recursive functions in C and they were slower than using a for loop. Since then I haven't used any sort of recursion in C or C++. Maybe my fears are unfounded but I try and avoid those two languages in any case.

edit: I just realized what I actually mean. Some programming languages, or the culture/community around them, do not seem to encourage certain things that can be done with the language. In the case of C/C++, while/do/for loops are used before recursion. In the case of Scheme, you try a recursive solution first and then go for a do-loop if you really need to...or something like that.

Re:Signs You're a Crappy Programmer (and don't know it) by dangph on Saturday November 24, 2007 @12:29AM (#83137)
> they were slower than using a for loop. Since then I haven't used any sort of recursion in C or C++.

That's premature optimization.