There Be Bad Code Here
In computer science education the idea that only concepts need really be taught is entrenched so deeply you'd need spelunking gear to find it and rip it out. The argument tends to be that if anything other than concepts are taught students will become tied into a language and their skills will be doomed to eventual obsolescence. To a certain extend I agree with this, the goal of a computer science curriculum is to make a person a computer scientist (as much as I hate that term) - not a java programmer. To do this, students need to understand high level concepts: data-structures, algorithmic efficiency, language design, systems theory, etc... Along the way though, they should be pulled aside and given some hints on how to write good code.
Good code is like good writing. There are no hard and fast rules for identifying it, but you know it when you see it. The corollary is also true. You know bad code when you see it. This is a little easier to make rules for. Bear in mind, each one of the rules can be broken at any time if you know what you're doing.
First off, variable names are important. Really important. If you need to comment on your variable names, then you need better names. If you have foo and foo2 as variables, then you probably need to rethink things. A variable name of one of two characters is almost always a bad idea. The name should tell you what it is. The name is there for your sake, not the computers.
Functions (or methods, or subroutines, or definitions) shouldn't be too long. A function that is your entire program is probably too long. A function that you have to scroll thru over and over just to understand is probably too long. A function should do one thing, do it well, and then quit while it's ahead.
Mark Jason Dominus said it best:
Well, if you don't know what it does, why did you put it in your program? That's like taking a crap on somebody's doorstep and then ringing the door bell to ask for toilet paper.You should have a understanding of what the code you're writing it supposed to do. If you don't know what it's supposed to do, how is anyone else supposed to know? Let alone the computer. Remember that you're not a programmer, you're a writer. You need to write for comprehension, and to write about something you first need to understand it.
People often say that you can't try to teach things like this to beginners. They say that novice programmers need to concentrate on getting their code to compile and work. Nonsense. You have to start teaching good habits early. If you spent 5% of the curriculum on good coding and writing, not only would students be much more employable, but they would be much better computer scientists.
Comments
Amen. As a computer science TA my senior year, I saw so many early students who didn't care if their code was understandable... until they came and asked me for help. If I, with more experience reading programs, couldn't understand it, then it was definitely way too complicated.
But at least I didn't see anything as bad as what they post on The Daily WTF.
Posted by: Anitra // December 16, 2005 6:38 AM