3

I had to implement a module that after completed, the client suddenly asked that for a while it would not need to be displayed, so I was told to comment it out (since we probably will use it later) which I did.

Now that I'm reading that commenting out code is generally a bad idea, what would be a good approach to set aside some functionality that will be used later? Is it, in this particular case, a bad practice?

6
  • 1
    Feature Toggle delivered as false? martinfowler.com/articles/feature-toggles.html Commented Aug 29, 2018 at 2:33
  • It's quite interesting, but sadly doesn't apply here, as the module was meant to be displayed from the beginning, but then it was a sudden request from the client. I'll edit the question to rectify this.
    – robertroid
    Commented Aug 29, 2018 at 2:41
  • 2
    With modern source control, there's no reason having commented out code.
    – ivenxu
    Commented Aug 29, 2018 at 3:24
  • How about leaving the module uncommented and make the application just ignore it? Commented Aug 29, 2018 at 9:47
  • 4
    @ivenxu true, but you still have to remember that it was once there to be able to find it back
    – Christophe
    Commented Aug 29, 2018 at 12:13

1 Answer 1

10

Commented-out code is bad for at least two reasons:

  • Things that are used by the commented-out code can change in ways that break it, and you won't notice it until you uncomment, at which point you may as well just rewrite it, depending on its size. In any case, it will incur extra effort that could've been easily spotted if the code was "live".
  • People who don't know the reason for the code to be commented-out may simply delete it. It won't be a problem to be restored from source-control, but it adds extra effort to restore it later, for the same reasons as the previous point.

If you think that restoring the code (either uncommenting or restoring from source-control) won't be a problem, or in other words, if you think that nothing that is being used by this code will change and make it invalid after a while, then it would be fine to simply remove it for now, and restore it when it is needed again.

However, if you know that the code will be used in the future, and if there's any chance that something will change that may affect it, I would suggest to use an approach similar to Feature Toggles, that is, placing the code under a conditional branch that will only be executed when you "flip the switch" to on. This way your development environment will continue to remind you, and everyone else, if there are any changes that affect the "hidden" code (e.g. compiler, automated tests, etc.). Meanwhile, you have the system running in production without the feature, with the toggle off.

4
  • I agree with this, though I would add that using feature toggles would be best for aspects of the program you expect to enable/disable. In other words, for a one-time quick fix, it wouldn't make sense to implement a means to enable/disable that feature. Ideally you'd simply write your own version in a branch, but sometimes that isn't practical for very minor modifications.
    – Neil
    Commented Aug 29, 2018 at 10:12
  • I completely disagree with @Neil as feature toggles creating a testing headache and should be a tool of last resort. Remove the code and restore it from source control when needed is the best solution. Still, +1 for this answer as feature toggles are just mentioned as s footnote.
    – David Arno
    Commented Aug 29, 2018 at 20:09
  • @DavidArno I only meant to use feature toggles only when it merits use. I find it interesting that you disagree with this, but you agree with the answer enough to upvote, which adds no additional constraint to the use of feature toggles. :)
    – Neil
    Commented Aug 30, 2018 at 6:12
  • As mentioned in the comments, I feel that implementing a full round of feature toggles just for a temporary fix would be more burden than actual help. So I won't accept the answer, but it's good enough for having a +1 as I can see other uses where it can actually help.
    – robertroid
    Commented Sep 3, 2018 at 0:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.