Comment Your F**king Code!
The Agile Manifesto values working code over comprehensive documentation, but that doesn't mean you shouldn't comment your code!
Join the DZone community and get the full member experience.Join For Free
you’re the elite. you know clean code by heart, you dream of solid design, and you unit-test every line you write. your code is so self-documenting you don’t even need to write comments!
then this rant is just for you! because let me tell you something: without comments, working with your code is still a f**king pain. no matter how elite you are and how awesome your code is, without some intelligently placed explanations it is still an unusable and unmaintainable pile of sh*t.
i never ranted before – not here at least – but i have to do it now or my head will explode! so please forgive my tone. after calming down i will write a more levelheaded post about the topic.
the next time i stumble across an interface or a class of yours which has zero comments, i swear i am going to hunt you down and chew your legs off.
but the code is self-documenting, you say? it is solid, well formatted, uses expressive names, consists of short methods and classes and is fully tested? it doesn’t need comments?
a story about contracts
let’s start with a story…
imagine you’re looking for an insurance to save your family from living on the streets in case you get hit by a truck. you decided on a policy and request a contract from the company so you can sign it. but guess what? they don’t do contracts anymore! instead they have adopted a “working company over unread contracts” policy.
they will happily invite you over to their headquarters where you are free to admire their crisp organizational structure, take walks around their expertly designed buildings and parks and talk to any of their couple of thousand attentive employees. really, you should come, these guys and gals even have great names like mitch and dorothee!
they would also like to give you a list of existing customers. you can call them up and find out how they fared with the company. given their personal history and payment options, when something happened to them, then what did the company do? and by the way, you can even visit their homes, too!
so, you ask them, how would this go down? easy, just start wiring us a monthly fee and we’ll cover you, they answer.
awesome self-documenting insurance policy! but would you trust them with your family’s well-being?
by igor clark .
so let’s get back to code. by now you might be able to spot the difference between agreed-upon and observed behavior. what about tests? i hear you ask, don’t they define the expected behavior?
you are trolling, right? now i’m supposed to not only check your implementation but also a bunch of tests? so instead of writing code i am now digging through dozens of tests methods, hoping to identify one which reflects my use case? and all that to deduce whether i can expect not to get null back?
ah, yeah. unfortunately you can’t even demonstrate that in a unit test. unless you manage to prove a “for all” theorem with examples, which would be truly impressive. other things you can’t clearly document in a test? only negligible details like thread-safety (or lack thereof) and immutability.
also, if you trust your tests to document your code, you better make sure that people get fired for not achieving 90%+ test coverage. and in my experience there is quite some overlap of the “don’t write tests” and “don’t write documentation” developers.
finally, do you know how many communities will answer questions with rtft – read the f**king tests? no? well what did i have in mind then?
but your names are expressive, you say? they’ll save the day! really? this is something i might be looking for when reading documentation:
- what are the preconditions, especially regarding the arguments i’m passing in?
- what promises are made regarding the return value?
- what units are used for arguments and return value?
- what about thread-safety, mutability, invariants or dependencies?
- under what conditions do exceptions get thrown?
don’t tell me that you stuff all of this into your names because you don’t. and don’t tell me that it is always obvious because it ain’t. by the way, i am also interested in why you picked the current design. i wonder how that fits into a variable name.
and how exactly does that expressive naming work for interfaces? i can tell you how: not at all! or do you expect me to comprehend all of a dozen implementations to find out whether they are supposed to be immutable.
i am also really curious how your colleagues are going to implement an undocumented interface. because as i see it, they can basically do whatever the hell they want.
so next time you’re starting to use a new library, eat your own dog food. ignore the documentation!
just guess by looking at the code what java’s map.get might return if the key is not present. take a look at concurrenthashmap and tell me whether null keys are allowed. use guava’s typetoken or cache without any introduction. use reflection without a look at the comments.
what do you say? these are all apis? this is totally different? seriously?! how the f**k is this different? i don’t care whether i use an api® or call into your code – either it tells me what it does or i go guessing.
guessing… because that’s what i’ll do if you don’t tell me what happens. you really think i will go through all your five implementations and your three digit number of unit tests to find out what a method does? no! i will make an educated guess because i’m in a hurry and have to get sh*t done.
and wasn’t that a goal of agile? to make developers guess less?
speaking of agile
we went from “we’re not writing documentation because we hate it” to “we don’t write documentation because we value working code over comprehensive documentation”. so now we’re ignoring documentation like before but we’re finally allowed to because our code suddenly became awesome? yeah, right…
so please, instead of believing this bullsh*t, take a note of how the agile manifesto does not say “we do not write comments."
but comments age, you say? that’s like saying “cars crash." yes, they do occasionally (by the way, in most cases due to some form of negligence that is punishable by law), but that does not stop us from using them. and it does not mean that we are not responsible to try and prevent that from happening.
also, i am interested to hear about that magic compiler you seem to be using. the one that prevents aging of package, class, field, method, and variable names. the one which keeps them up to date and accurate as you change the code.
oh, there isn’t such a thing? shocker! you’re updating them as you go? congratulations! now be a responsible little developer and include those comments in that process and we’re fine.
comment your f**king code!
so please, stop being so full of yourself and just add a useful comment here and there. where? i’ll write about that in my next post.
Published at DZone with permission of Nicolai Parlog, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.