Great comments explain “why” code does something rather than “how” things are done.
Great comments explain unidiomatic code.
Great comments dispel confusion, not cause it.
But have you ever considered turning great comments into a log message?
Comments transformed into log messages are friendly debugging statements.
They help to understand the runtime behavior of your application.
For non-critical logs, just use the DEBUG or TRACE log level.
Next time you see a useful comment, consider to just turn it into a log statement.
// Before
if (targetTableExists()) {
// Skip data base migration because the target table `animals_v2` already exists.
return false;
}
// After
if (targetTableExists()) {
log.trace("Skipping data base migration because the target table `{}` already exists", TARGET_TABLE);
return false;
}