This article was posted originally on JRoller on April 27, 2005.
Comments are meant to be read. Sometime, they tell a story. The story of the code that follows them. Using Version Control tool, you can even retrieve the main characters.
Comment Story 1: John and Eduard
During a design meeting, John explains to Eduard how to modify the code he wrote, to include a new feature. They agree that some switch statement would be needed at some point in the class. But Eduard does not like switch statements. Eduard prefers the long list of if/else statements. He is a fervent Conditional Spaghetti adept. So he writes the code his own way, and include the following comment at the top of its code:
// sorry John, I hate the case structure...
Comment Story 2: Robi and Andras
Robi likes complex code. He likes long methods and overusing design patterns. His pride is a 400 lines long method, introduced by the following comment:
//the fair dinkum! select appropriate editor, handle selections, wash, iron, f*ck etc.
Robi leaves the project, and his code is handed over to Andras for maintenance. Andras has to fix bugs in Robi's code, and he doesn't like it. He has several sleepless nights, dreaming about monster methods managing everything in his house, ranging from dish washing to bringing down the garbage. For fear of breaking anything, Andras will try to change as less code as possible in Robi's piece of art. On one inspired day, he will add the following comment at the beginning of a 2500 lines of code long class:
/** * @author K. Robi * ^^^^^^^^^^^^^^^^ you can tell ;-) */
Comment Story 3: Robi is angry
Robi works with JTables. He is trying to make something not really easy: the table should sort if you click on a column, and an arrow should show on the table header on which column and in which direction it is sorting. Being a big Design Pattern fan, he is putting quite some of them, and of course over-complicating the whole design. But this is not the point here. The point is that for some reason, something does not work when the reordering of columns is not allowed. Was it a Java bug? Or is it a misunderstood feature of JTable? I don't know, but what I know is that Robi spent some time on it. You can tell because he left several lines of code commented out:
// getTableHeader().invalidate(); // validateTree(); // invalidate(); // revalidate(); // repaint();
And then he got angry. REALLY angry. So angry that he felt he has to put the following comment in his code:
//some fucking ugly workaround for the case when fucking column reordering is not allowed. //damn fucking strange, but none of these fucks above work and i'm fucking tired of fucking around //with these fucked-up tables! JTable fucks! //fuck that!
All this followed by those mysterious lines of code that I would be too scared to remove:
if(!getTableHeader().getReorderingAllowed()) { TableColumn col1 = getColumnModel().getColumn(0); int w = col1.getWidth(); col1.setWidth(w-1); col1.setWidth(w+1); }
Comment Story 4: Gabor and Peti
Gabor is new to the project. At some point during his learning curve, he has to fix quite a complex bit of code, a method of over 100 lines checking that some value fulfills all requirements before being submitted to the data store. It's quite a lot of tests, but still, he is not sure. Was everything tested? So in case someone comes up with another idea, he puts in the following comment in the code, at the end of the method:
// ??? anything else?
Peti did read this comment, and found it funny. He actually felt the need to answer the question, and to show some positive and optimistic reaction to comfort Gabor that there would probably not be, but with the firm knowledge of experience that says "who knows...". He inserted the following comment:
// Oh, no.
Comment Story 5: Baby come back
Sometimes, developers get bored, because the code they have to write does not present any challenge or fun. So they find way to have fun, and one way to do it is to put original comments in the code. That's probably what happened to Peti, who wrote some code which copies some value, use the original storage for some calculation, then restore the original value to its storage. In normal time, he would have put a comment like "Putting the original value back". But instead, he wrote the following:
// Oh, baby come back
Comment Story 6: Peti talking to himself
It happens that comments are a mean of discussion between developpers, as it happened in Comment Story 4 between Gabor and Peti. I found such a comment spawned on three lines which seemed to be a dialog. But looking back into the version tree, it turned out that these three lines were written by the same person:
// maybe only atm? // what do you think? // i'm not sure.
Probably still waiting for an answer...