Thoughts on writing and saving lines of code
I read long ago that programmers are lazy by nature. When I
hear or read about what Java ought to have because some other
language has it or because it would cut three lines of code down to one, I start
to twitch a little. I'm just as sensitive about any suggestions to extend
the semantics of the language itself, but that's a discussion for another day.
I wonder if all these innovators realize the price of saving lines of code. I'll answer
that myself: probably not. The biggest problem I see is readability, or more precisely, how easy the code is to understand. I
always think of the golden rule that you write code once or twice but read it eight times. That's why,
for instance, I don't share the giddy delight over an anonymous inner class implemented
on one line. There's nothing fun about deciphering someone else's code like that or, heaven forbid, tracing it. Tracing in particular gets tricky when you cram things into one or two lines.
Each of us has a coding style, or to put it another way, the way each of us writes code changes
with time and experience, and of course depends on the programming language.
Newcomers to a team usually write code that reflects their experience: inconsistent indentation,
long lines, opening braces on a new line, and so on. People coming over from other languages
are a case of their own. Teaching them to follow the conventions of a different language
is like throwing peas at a wall, as the Czech saying goes, or trying to teach an old dog new tricks.
This might sound trivial, but getting these relatively small things consistent is
the bare minimum for sharing code among developers. Some projects have their own
source-code standards, which to me is a sign of professionalism. Anyone
who's tried to merge, diff or patch “heterogeneous” code will surely agree. If you present it well and developers understand that shared conventions aren't there
to torment them, you've already won half the battle.
Whenever the conversation turns to basic coding conventions, I can't resist pointing out the excellent Java Code Conventions document. Anyone who wants to program in Java for more than one semester at university ought to read it.
Finally, a word on how I feel about automatic code formatters. They can be useful, but I don't share the enthusiasm some colleagues have for automatically formatting everything just before committing it to CVS. I think you should aim to write readable code the first time. I see tools like Jaloppy as a way to tidy up code that was written without following the conventions.
Related articles
Related CZ Podcast episodes
- #261 · Leadership
On leading people and teams. · Czech audio
- #3 · Java 6 SE, Vývoj webových aplikací
Dagi and Filemon discuss Java 6 and web application frameworks. · Czech audio
- #1 · Vývojová prostředí v Javě
The first CZ Podcast episode looks at Java development environments. · Czech audio