I have been loving Medium lately. Since its new version launched, I have been more engaged in its content and reading some great articles.
This morning I came across an article title Please, don’t commit commented out code – based on the title, I am sure you can work out what the article is about.
I think many of us have been guilty of committing commented out code into the main source repository. It happens, sometimes intentionally and sometimes unintentionally.
In my opinion sometimes committing commented out code is warranted, especially during the early development phase. But only for really limited scenarios like debug lines, most of the time even debug lines are not justification enough (but I have done it many times).
In the article Kent seems to use examples of large chunks of code being commented out, one such example is:
function foo(bar) { const baz = bar(false) // we no longer do this for some good reason // if (baz === 'foobar') { // return baz // } else { // return bar.foobar() // } return baz }
I agree that under no circumstance should a developer do this. It is a dogs breakfast of mess and dispair. A little further down Kent suggests that we use git diff
to get back old bits of code that we needed.
I have encountered many developers who don’t know how Git well enough to do a git diff
on a file to get back existing changes. It seems many developers these days rely on tools like Sourcetree to work with Git and for most purposes that is fine, but I think knowing some of the command line commands is handy.
Git clients are not the be all and end all. Somewhere along the line you need the command line. Interestingly in the comments on the Reddit submission, some people are actually defending committing commented out code into the repository.
It becomes increasingly obvious many developers who decide to commit commented out code are not aware of git diff
or they don’t know how to use it, so they pick the lazy option and take the easy way out. Instead of learning how real source control works, commenting out code becomes the preferred option.
There is rarely a good reason to commit commented out code. Either you learn how to use Git properly if you’re scared of diffing a file or you quit development until you learn how source code management in the real world works.