If the comments are ugly, the code is ugly

Harsh words, I know, but I've come to believe it's true.

By Esther Schindler  137 comments

Harsh words, I know, but I've come to believe it's true.

[ See also: Convincing the Boss to Accept FOSS]

Plone developer Martin Aspeli pointed me at a rather wise blog post in which a C programmer shared three rules he learned the hard way. The lesson that captured Martin's (and my) interest was this:

Good programs do not contain spelling errors or have grammatical mistakes. I think this is probably a result of fractal attention to detail; in great programs things are correct at all levels, down to the periods at the ends of sentences in comments.

"Aw, c'mon! You’re kidding!" You might think that nit-picking like this is beneath you, whereupon I will start pointing out errors in your code. It was embarrassing the first couple times this happened to me.

Programming, whether you're doing it as an open source enthusiast or because you're workin' for The Man, is an exercise in attention to detail. Someone who writes software must be a nit-picker, or the code won't work. Surely, you don't need me to cite the number of famous programming failures that were the result of a developer using a comma instead of a period, or random crashes caused by two lines of code in the wrong order?

Perhaps my opinion is colored by my own role as a writer and editor, but I firmly believe that if you can't take the time to learn the syntax rules of English (including "its" versus "it's" and "your" versus "you're"), I don't believe you can be any more conscientious at writing code that follows the rules. If you are sloppy in your comments, I expect sloppiness in the code.

Good programs do not contain spelling errors or have grammatical mistakes. Agree or disagree?(poll)

My premise comes from something a very wise editor taught me, ten years ago (Laton McCartney, in case you're listening: Thanks!). If you can write the deck, he said, you can write the article very easily. If you can't write the deck, you don't understand yet what you have to say, and you shouldn't start writing. (The "deck" is that teaser or paragraph following the headline, which explains what the story is about and cajoles the reader into reading more.) Long-winded "explanations" of the code in the application's comments (that is, the ones that read like excuses), indicate that the developer probably didn't understand what he was doing. Which means that this is a prime candidate to be a bug farm.

Complaining about poor internal documentation is an old practice, but there's a reason that doing it right is important. Your comments are the only way you have to speak to the next person looking at this software (that may be you) about the larger scope of the software, not just this line-or-two. What were you thinking when you wrote this code? Yes, "self-documenting code" is a goal, but it's arrogant to assume that you've achieved it, any more than it's arrogant of me to assume that my words need no editing. (They do. I'm happiest when I have an editor.)

Another common problem in comment ugliness is developers who update the code and don't update the comments; as one consultant pointed out, comments aren't tested. But doesn't that show a lack of attention to detail, too? Anytime you aren't paying full attention, you're apt to drop a logic bit.

Need a few examples?

From an explanation of a design choice. ("Yes, I can guess what was probably intended," wrote the programmer who showed me this example. "But therein lies the problem: I have to guess.")

"... questions designed to illicit a discussion..."

Here's an actual line of code with comment. Note how the comment highlights the spelling error. Not to mention that the comment tells you the programmer didn't have a clue of what he was doing in the first place. Especially since it shouldn't have been "finished" in that block of code; in that application, it should have tested against "complete".

if item.getState() == 'finsihed': #is this correct?

Yes, I allow for some exceptions. If English is not your first language, then it's possible that the comments you leave in your code will demonstrate any discomfort you feel in writing English. However, I haven't personally found this to be true. As many Americans have learned, "foreigners'" English skills are typically much better than our own. The developers I know whose English language skills are awkward are aware of it and they go to great lengths to ensure that an English speaker edits their documentation.

I won't go into code and comment indentation; that's a religious issue. But I've met developers who feel as strongly about "ugly" code formatting as I do about ugly grammar.

Modern development environments arguably have made it possible for developers to be sloppier without ill effect; a drag-and-drop interface might let you create a quick-and-dirty application (more dirty than quick, I fear) with fewer terrible side effects than back when software was typically written in Assembly language. Somehow, however, I can't see "I can be sloppy" as a true advantage.

The "ugly" comment rule is just as true for internal business applications as it is for open source software. But FOSS developers need to pay more attention to it for two reasons. First, in a regular office, you very likely can find the programmer of the offending code and question him (or wallop him, if it was that bad). Also, in the open source community, more people look at the code and need to understand it.

However, when I suggested to several developers that "if the comments are ugly, the code is ugly," many disagreed with me. You might, too. I'm interested in learning why, so I created a poll (let's see what everyone thinks!) and I invite you to leave a comment.

You probably should follow me on Twitter. Because, y'know, you just should.

137 comments

Anonymous 1 year ago
A good comment, as a good code, is one that focus sharpely and concisely on the idea, on the logical behind what is visible. One psycho-rigid person, as they're many among programmers, can past several hours in a day with obscur details and totally miss the whole logical mess in front of him. Details do matter, once and only once your logic is well defined, that's the big difference between one that think, and one that write long and gramatically perfect useless comments. You need to get some distance between you and the lines, so your brain can work efficiently, for other things, pick a syntax checker.
Anonymous 1 year ago
And speaking of paying attention, I really should have clicked "read all comments" before posting :-)
Anonymous 2 years ago
i definitely agree since i have been involved in several conversion projects. it doesn't hurt to let know the intent of a certain functionality, but i don't know... if attention-to-detail is something that can be taught. it's more like a builtin feature
Anonymous 2 years ago
Your comments are (more) valid for software that is about to go into maintenance than for that under active development or in a research environment. I have seen comments like "Check this is right" in code as well. Normally this means the writer is working to a deadline on code someone else has written, and has to make a reas the purpose was obvious to the original developer but the original developer is not available. I agree that the tidy up phase prior to release should include (mandate) checking of comments and external documentation. Managers seldom allow or are allowed to put time for this into the project budget.I feel comments need to be future proofed by focusing on the goal not the code and should answer questions like "Why did they do that, it seems redundant" where the answer might lie in subtle interactions between various components. The purpose of comments is a quick indication of why the code does what it does and, if has to be "clever" code how it does it: Or better refactor into a method with a name that is itself a comment. The develope may or may not be familiar with the big picture and a comment should give them a chance to fix a problem without a month reading manuals and developer guides. Errors in Grammar are subjective matters. Language changes all the time. Spelling is more stable than grammar but I am not concerned with errors that do not interfere with communication ( comments in American not English are normally a bigger problem) but I do alter comments that are unclear or gloss over points that have caused me problems.
Anonymous 2 years ago
I forget who it was, but back when Usenet was king it was someone's law that every spelling flame had to contain at least on speeling mistake.And, appropriately, one of the words in my reCAPTCHA is misspelled. :)
Anonymous 2 years ago
Esther, a very interesting article! My two cents is that the quantity of comments depends on the language. In a study of open source code, we found, for example, that there were 0.58 lines of comments per line of code in Java versus only only 0.23 comment lines/LOC in C.http://www.blackducksoftware.com/news/releases/2008-12-09#tablesEran StrodBlack Duck Software
Anonymous 2 years ago
but I agree that poor grammar in comments *and* inconsistent formatting are indicators of code content. I've worked with several people who consider commenting and formatting a waste of time. Having had to read their code to make upgrades was made twice as hard. And larger programs become dead weight around our necks when they get so convoluted and hard to understand. I can figure a lot out but their laziness only made my work that much more difficult.A well-commented set of code can read almost like a novel. A narration is in the comments and the characters interact in the actual code. It's a whole lot faster to find what you need to work on when there is context. I like what Professor Brian Harvey of Stanford U. said in one of his lectures. It was something to the effect that comments should be about the domain knowledge of your program, not necessarily a play-by-play of each line of code. Or to put it another way, if you're building a car, you talk about the "engine", or the "transmission", etc., rather than the nuts, bolts and pieces of metal that together make what we call an engine or transmission.You may never know who is going to read your code. Maybe no one. But I have first-hand experience with code written by someone who retired from the company and left a lot of scripts and executables that are used daily. Not a bit of documentation or comments in his source. It's a maze, a puzzle, a treasure hunt. But why was the burden left to me or anyone else to figure out what was in his head? In designing upgrades, it makes a good case for starting from scratch.
Anonymous 2 years ago
few points1 I kan formt and mispell stuff and u still undrstnd 2.) Opinions like this with no conclusive data wreak of a scholar or ameteur develuper feeling importantTree) Do you want missle avoidance software now or do you want to go without until its guaranteed perfect?I spelled everything correctly.
Anonymous 2 years ago
I wrote my first lines of assembler in 1957 (yes, fifty seven)and found then and since that if I couldn't explain what it did in a short line, then I didn't KNOW what it did. And shouldn't have written it in the first place.
Anonymous 2 years ago
This has been a truly satisfying read...no one is right and no one is wrong. The result of whether the damn code compiles or not is always the winner. If one is truly a programmer and understands the construct, principles and methodologies of the programming language; no amount of comments will detour him/her from understanding what the intention of the programmed application does or should do.This argument can be simplified by stating: A compiler does not care if a variable is defined as finihsed (intentionally misspelled)...so long as it is properly used within the code. Now the use of misspellings may detour the possibility of landing a professional instance of a programming job, but there's nothing find and replace cannot handle when offending typos are the subject of one's written application by those who have the anal ability to spot them, refactor and clean up another programmers mess.Sure it is a pain in the arse, but question is the application useful or not? I don't feel no amount of commenting (great or not) can save a useless application.
Anonymous 2 years ago
CONGRATULATIONS GUYS!Your pointless debate over a grammatical inaccuracy has just proved how IRRELEVANT spelling and grammar is in coding.Let's focus on efficiency and consistency in our code, not pedantics
Anonymous 1 year ago in reply to Anonymous
And I thought this post was good, the comments are even better! Low and behold the answer is lying in the comment section anyways. Usually is! :) ~dominique @freedomiq
Anonymous 2 years ago
I completely agree - spelling and grammar and syntax and everything. I have been teaching Advanced Placement Computer Science for three years now and realize that many students have very poor typing habits. I even get code where the IDE (I use Eclipse) show spelling errors - and students still turn this code in. My understanding of comments is that they are for other people, since the writer of the code is supposed to know what is going on.
Anonymous 2 years ago
I agree that, in everyday life, small details are good indication of how attention/dedicate a person is to bigger problems (in this specific case, small details are the comments and the bigger problem is the code).So if comments are ugly, the code is ugly ... how about if there's no comment at all ... what should we think about the code?
Anonymous 2 years ago
While I agree with the author that more mistakes implies poorer code, I have to ask myself where does this attitude intersect the business case? When factoring in Moore's law (as a previous poster mentioned), I think we often spend too much time fixing other peoples formatting and comments, and too little time writing proper requirements, unit tests, etc. Ultimately, code can only be as good as the business case allows, unless it is being done as an academic exercise. It would be nice if MS (and other manufacturers) would integrate a spell-checker into their compilers, though...
Anonymous 2 years ago
You may very well be right. Comments are part of the documentation, and documentation seems to be an indicator of code quality.
Anonymous 2 years ago
This is total BS... every program is entirely subjective and its structure depends largely on the context in which it is being written. In a perfect world we (programmers) would all get complete and accurate specifications/designs before ever writing a single line of code... but this isn't a perfect world its the real world and in the real world more often than not the client or the company your working for doesn't fully understand what they want until they see what can be done. This means the process of creating and designing the application takes months if not years. Who cares if you have something misspelled in the comments section... when your busting your ass to get out 100 thousand lines of code, you probably won't bother to correct an obvious and harmless spelling mistake.Moreover, this also depends largely on whether or not you are working alone or as part of a larger team. I am the only developer at the agency I work for, and as such the need for comments aside from revision control is largely unnecessary or not possible due to time constraints. Much of my software is large, and very complex due to large scale integration of business logic, but I know how I write code.. I can look at a piece of code I wrote years ago and immediately understand what I was thinking at the time... My point is this, programming is subjective... and it always will be. People like Esther can take their closed minded judgments and keep them where they belong - to themselves.

Add a comment

Post a comment using one of these accounts
Or join now
At least 6 characters

Note: Comment will appear soon after you have activated your account.
Obscene/spam comments will be removed and accounts suspended.
The information you submit is subject to our Privacy Policy and Terms of Service.

ITworld LIVE

DevelopmentWhite Papers & Webcasts

White Paper

HP NonStop SQL Fundamentals whitepaper

This whitepaper offers a detailed look into the fundamentals of HP NonStop SQL solutions. See how this system delivers unprecedented levels of application availability with fail-safe data integrity and meets the needs of enterprises with large-scale business critical applications.

White Paper

Nebraska Medical Center case study

See how the Nebraska Medical Center implemented a SQL solution to make information more readily available to streamline operations, improve patient care and facilitate medical research with an enterprise solution running on HP NonStop servers.

White Paper

Concepts of NonStop SQL/MX

For DBAs and developers who are familiar with Oracle solutions and want to learn about NonStop SQL/MX, this whitepaper provides an overview of the similarities and differences between the two products-with a specific focus on implementation.

White Paper

6 Things Your CIO Needs to Know About Requirements

If your organization is not predictably successful on technology projects, there is likely an issue in requirements. CIOs must take action and own requirements maturity improvement. There are 6 main things a CIO must know about requirements.

Webcast On Demand

User Experience Monitoring

In this webinar, you will learn hints & tips for improving end-user response times from Forrester Research analyst, Jean-Pierre Garbani.

Sponsor: Nimsoft

See more White Papers | Webcasts

Ask a question

Ask a Question