Does anyone use comments any more?
Every function, method, variable or property has a comment. Even within methods, clear explanations of everything that goes on is everywhere in my code. The logic of a loop, the reasoning behind a conditional, the setting of flags and some variables. In some cases the commenting is a mini-story of how the program flows and why certain things have to be done. An easy-to-follow plain-text explanation of the code.
Yes, this takes time and work but it has always proven to be well worth the effort.
I've cloned a few GitHub projects and, I have to say, I am absolutely amazed when I look at the source files. Project after project without comments. Not one. Just a pile of code. Not only does this require more work to understand what the author was thinking or why he or she took a specific approach, but I would imagine that the code is also harder to maintain and evolve as the author and others loose touch with it for months or more.
Is this what they are teaching in school these days? Damn the documentation, just write code and make it work?
I, for one, will not accept anyone working for me taking this approach. Code is expensive to produce and it must be well documented for others to maintain, support and evolve.
25 comments
[ 2.4 ms ] story [ 60.1 ms ] threadForcing a developer to constantly shift between the english language and a programming language takes up precious screen space and insults the developer by constantly making her switch gears.
Inline comments are the worst abused. After that method doc for the purposes of supposed documentation.
A well named class in a stable framework needs no documentation as long as its interface is intuitive.
Chances are you are interacting with really bad systems and want some help. If the code is that bad, I doubt the developers comments would be much better.
No comments is the best if you can get away with it and have a self documenting set of interfaces.
As an example, I do both hardware and software. I might work on FPGA code for months and then move on to the embedded code that runs the board. It is possible to not have to touch the FPGA code for months.
If I then have to go and modify something, say, add a register to implement a function I need, comments are a life-saver because you need to do a hard "context switch" in your head. I don't have time to read through code and figure out how a particular chunk-o-code works or what I meant by it. With FPGA code, where you are likely dealing with code structures that are controlling signals in the nanosecond range, you can't wing it. It either works or it doesn't and even minor code changes can make the entire thing go from working to a friggin mess. Comments are the "user manual" to the code.
Later on I might have to go and work on the iOS app that might access this hardware. So now you have FPGA code, embedded code (runs the board that has the FPGA) and iOS code.
Once again, even when I could be the only developer in the project without comments the entire thing becomes unmanageable very, very quickly.
There are lots and lots of ways to write a function that passes a test, I want to know if there is a reason why you chose the approach you used.
Over commenting is a bad practice. Comments should be reserved for code which is difficult to understand no matter how well it can be written.
Forcing a developer to constantly shift between the english language and a programming language takes up precious screen space and insults the developer by constantly making her switch gears.
Inline comments are the worst abused. After that method doc for the purposes of supposed documentation.
A well named class in a stable framework needs no documentation as long as its interface is intuitive.
Chances are you are interacting with really bad systems and want some help. If the code is that bad, I doubt the developers comments would be much better.
No comments is the best if you can get away with it and have a self documenting set of interfaces.
What nonsense. In this age of syntax highlighting it's easy to make comments vanish in an editor; there's no shortage of screen space anyway; and if you find it insulting to have a guide to the function of the code, then maybe you're a little thin-skinned. Code is there to be maintained, not to serve as your mental gymnasium. It's a means to an end. Many skilled programmers may do fine without comments, but the above basically sounds like an excuse for not writing any.
You talk about over-commenting. I am talking about no comments whatsoever, not even to document methods or subroutines. I cloned this one project that was loaded with methods not used anywhere. In fact, it had included the SBJson framework and it was being used in a method that never got called. The issue came up because it collided with my own use of SBJson on my project and I wanted to figure out what to do about it. It turns out that I could just delete the entire thing and it did not affect the code at all.
I'll give you another example. I have not programmed in Forth in a long time. Maybe fifteen years. However, I do have tons of Forth code that I wrote back then for various projects. All of it is very well documented. I have no doubt that, fifteen years later, I could grab that code and be back in the swing of things in no time at all. I wouldn't have to read through the code in detail and go through the process of building stack images in my head to figure out what is going on. Without comments code libraries like that become impenetrable black boxes.
I could say the same about LISP. I haven't touched it in ages. I wrote tons of utilities and at least one major framework (one year dev time) for a variant called AutoLISP (runs inside AutoCAD). Again, tons of comments which would allow me to jump right in and know exactly what is going on.
It should go without saying that trivial stuff does not require comments. As an hypothetical example, a loop that searches an array (or whatever) for a specific match doesn't need line-by-line comments. What I might do here is, again, hypothetical, just before the "for" statement throw-in a comment that says something like "Look for CRC code match in packet data". With that simple line anyone reading the code knows what the intent was. I can read that code five years later and know exactly what it does without having to read backwards and forwards through the code to figure out what each variable might be, where they come from, what they are loaded with, what the intent might be, etc.
I also find it invaluable to document state machines (particularly for hardware --FPGA-- designs). If you take something like a custom DDR3 SDRAM controller state machine, well, it isn't a trivial thing to read through and figure out what it is doing. Well-authored comments can turn something that would take hours-upon-hours to comprehend into an easy read.
I have seen the value of good comments and code documentation many times over during my career. Keep in mind that I am not making a comparison between sparse comments and writing a book.
I know what you're getting at. There's nothing worse than reading a 50-line function with no comments. But the real WTF isn't the lack of comments - it's having a 50-line function in the first place (or 8000 lines for that matter). Having lots of comments is a code smell - it indicates poorly factored code. Next time you need to write a comment explaining what a piece of code is doing, try writing a function instead, with a name that says what it's doing, and see where that takes you.
I highly recommend Misko Hevery's "Clean Code Talks" on YouTube. They're part of the Google Tech Talks series, and they will blow your mind if you let them.
You might be right, but you also might be focusing on the wrong thing. There is a case to be made that the goal should be clarity, and comments should be reserved for the cases where the code itself cannot be made clearer. Steve McConnell expresses this particularly well:
http://www.scribd.com/doc/95398523/McConnell-Code-Complete-C...
The whole chapter is a gem, and it's hard to find a pull-quote that does it justice. It's probably the single piece of writing that most changed my attitude about programming. But if you'd like a starting point, your position sounds a lot like a character in his opening allegory at line 177:
"I want to suggest a commenting standard for our projects," Thrasymachus said. "Some of our programmers barely comment their code, and everyone knows that code without comments is unreadable."
That comments get out of sync with code is a fact. That they stay out of sync is inexcusable. What I will generally do is to fully test and debug a chunk of code and then go back and annotate. I sometimes have a "Go comment the code" item in my to-do list and will spend time here and there cleaning-up and clarifying comments. The other interesting thing that can happen when commenting this way is that you can find issues with the code or identify refactoring opportunities.
It's like sitting down to explain what the code is doing to another capable programmer as opposes to a 12 year old who is just starting to learn how to program. Maybe that's the best way to put it. What do you say to an experience programmer if you had to do a code review and explain your code? That should go in the comments.
If a method or subroutine is absolutely trivial then you say "This finds the average of the voltages across these four inputs".
If, on the other hand, the routine is more complex, you have to describe the overall intent "this calculates the values of the missing pixels" and then dive within the module to explain some of the specific techniques being used "Polyphase FIR filter to interpolate image data for missing color samples" or "Bounds-limited adder to clip FIR filter data to legal image data range".
In other words, you don't explain every line of code but rather functional chunks that make it easier to navigate it and understand what is being done at a macro level and why.
Again, my peeve is with projects that have files upon files with not a single comment. I don't expect a book.
IF you have only high quality people, and IF you have high quality code reviews, THEN it is possible for comments to become maintainable, valuable, etc. I personally have only worked in one software organization where this was true.
Over time, I had the experience of mine and other people's comments generally being out of sync with the meaning of the code. Further, extremely complex code is too complex to fully document in the code and easy code should require no documentation. There's a middle ground but its much less than everything.
The time I once spent on comments I now spend on picking function and variable names as well as writing tests and asserts. Tests and Asserts are nice since if they stop being true, they force you to change them, unlike comments which can stay stale with no repercussions.
This isn't saying I write no comments. I write comments but a comment is intended for something unusual. That's good because that means it will get read.
The only time commenting becomes difficult for me is if I am in a team where I know I will be the only one maintaining good comments. It takes no effort to update comments as you code but if I am the only one doing it then it becomes a problem.
I think there might actually be an old-school element here. Coding up to the 80s or the early 90s tended to be about squeezing the absolute most capability you can from the machine. Clarity and conciseness probably took the back seat to optimization hacks, so writing the comment to describe the original intent of the code actually was vital.
Peter Norvig's old Lisp programming book has a nice illustration on how the programming style in Lisp has evolved from the 70s to the book's 1992: http://books.google.com/books?id=QzGuHnDhvZIC&lpg=PP1...
SICP also had the "programs must be written for people to read, and only incidentally for machines to execute" quip, and also notes "In this book we don't use many comments; we try to make our programs self-documenting by using descriptive names."
Since the 90s, programmers have had a bit more breathing room, and the idea to actually write the code itself so that it will concisely describe what it's doing when read by a competent programmer has become much easier. You still want to document nontrivial interfaces, tricky things from the application problem domain and problem workarounds in the code, but the actual business-as-usual code tends to be readable and idiomatic enough that you don't need to comment every function, method and variable. Most can be inferred well enough from context.
I wish people would add a one-line comment to the top of every source file saying what it's for. ONE LINE is all I ask.
The Regex comments make all the difference, because I can easily check if my sample inputs that I wrote the regex against have changed. I'm also reminded of what I'm trying to get, instead of trying to figure out "Okay, I have a $host, $ip, ..."
I highly recommend this book: http://www.amazon.com/Clean-Code-Handbook-Software-Craftsman... as it changed my life when it comes to software development. I have a real problem with commenting everywhere as most of the time you'll find a lot of public String getName() with like a comment of //Gets the name, uh, durr its name is get name! If you create concise methods with one general purpose you can create very readable code. Definitely check the book out, it is a very good read.