> I may put a few words in a comment to explain why the code is doing what it's doing. If I find comments answering 'what' or 'how', I take that as an indicator that the code is not written well enough.
And in that case I usually add the paper's pseudocode above the corresponding transcription as well to make the mapping even clearer.
function do_search($query) {
if($database->type == "mysql") {
return __do_search_stupidly_because_mysql_is_shit($query);
}
else {
return __do_search_sensibly($query);
}
}I'd much rather see a comment like
/*
* Inputs of integers near INT_MAX cause significant CPU load
* in MYSQL versions 4.2 <= x < 5.8 (latest at time of writing)
* Bug tracker http://mysqlbugtracker/issue/4
*/
Than lolz_mysql_is_shit(foobar)
As a relevant point, we've got a large comment in our codebase linking to issues and discussions of the problem of excel reading csv files containing £ symbols with particular encodings. If you didn't know the issue, having `format_excel_is_dumb(file)` wouldn't help.I am happy classifying comments as "you should have a reason to add it and keep it" rather than "bad code, always remove".
Still, I suppose it's hard to generate buzz with an article that says "use your common sense" rather than repeating an old absolutist viewpoint.
http://sourcemaking.com/refactoring/replace-conditional-with...
In my mind code is for the "how" to do a thing, comments is for the "why".
While sometimes some bugs in some library / infrastructure element bites you badly, I still think calling a function "do_workaround_XXX_because_YYY_is_shit" it's not a good practice. And, hey, in comments you can articulate your hate for mysql with more space :D
I prefer to be parsimonious with comments, but strange hacks, workarounds for bugs, and other places where otherwise clean code meets the real world should probably have at least some description of why that particular workaround was chosen, and not some other more obvious option.