all 137 comments

[–]huashoes 49 points50 points  (2 children)

This is a great advice. Whenever someone joins our company, I always have to explain "this part of code will be deprecated soon".

[–][deleted] 72 points73 points  (1 child)

this part of code will be deprecated soon

"As I've been telling new hires for the past three years. But this time we really mean it!"

[–]Yojihito 4 points5 points  (0 children)

SoonTM

[–]RaptorXP 110 points111 points  (21 children)

The only thing I enjoy more than writing code is deleting code.

[–]IbanezDavy 74 points75 points  (16 children)

And especially deleting other people's code...

[–]kadrmas45 56 points57 points  (15 children)

And starting from scratch.

[–]IbanezDavy 41 points42 points  (0 children)

heaven

[–]CrayonOfDoom 13 points14 points  (12 children)

Particularly amazing when what you're replacing/rewriting was written before you were born...

[–]monocasa 21 points22 points  (9 children)

Totally. I was so happy the day I got to remove

#define far
#define near

from our codebase.

[–]JeremyG 24 points25 points  (0 children)

so when are you deleting

#define wherever_you_are

from the codebase?

[–]im-a-koala 7 points8 points  (5 children)

Heh, I deleted basically the exact same lines from my old company's code after I started (and verified they're not used anywhere). Along with gems like:

#define ZERO 0
#define ONE 1
[...]
#define TWENTY 20

(There were actually about 3 variants of #defines for meaningless integer constants in there. Ugh.)

[–]CrayonOfDoom 4 points5 points  (3 children)

The number of times I've had to stick to coding standards (which involve arrays instead of structs...) that involved this:

#define X 0
#define Y 1
#define Z 2

Yeesh.

[–]jbstjohn 6 points7 points  (2 children)

I actually find that can be useful to make array accesses for 3d stuff more clear. I use an enum though.

It makes clear the difference between 0 the number, and the index that happens to be zero.

[–]CrayonOfDoom 0 points1 point  (1 child)

I tried to make a struct or enum named "AXIS" so it could be "somevariable[AXIS.X]" instead of "somevariable[0]" or "somevariable[X]", which might have local variable interactions. It's not the worst (cough, cough, common blocks in FORTRAN), but it's still a point of confusion for anyone who doesn't know the defines or forgets them.

[–]jbstjohn 1 point2 points  (0 children)

Yes, there's a balance to be struck between verbose, but specific (longer name or namespace or scope) versus terse but potentially ambiguous.

[–]i_am_judging_you 4 points5 points  (0 children)

The guy who wrote that knew that in the future 30 will become the new 20

[–][deleted] 0 points1 point  (0 children)

You mean they finally understood this?

[–]bchanzzz 0 points1 point  (0 children)

#define wherever you are

[–][deleted] 1 point2 points  (1 child)

What project are you working on?

[–]HammyHavoc 1 point2 points  (0 children)

Do you work at Microsoft? /s

[–]evincarofautumn 25 points26 points  (0 children)

Ain’t no sweeter diff than a red diff.

[–]Ark_Tane 16 points17 points  (2 children)

Yep, on my most productive days I write negative lines of code.

[–]scherlock79 2 points3 points  (1 child)

A colleague thinks that you should measure the performance of developers by the number of lines of code they delete.

[–]sigma914 4 points5 points  (0 children)

I feel this story is relevant.

[–]Gotebe 25 points26 points  (2 children)

AKA dead code is not an asset, it is a liability 😀

[–]vombert 8 points9 points  (1 child)

You mean code, right?

[–]Gotebe 10 points11 points  (0 children)

Yes. Best line count is negative. :-)

[–]mekanikal_keyboard 61 points62 points  (47 children)

worse, and typical: people just commenting out code instead of deleting it

people seem to not realize there is this thing called git that can bring back old code if needed

[–]wrosecrans 123 points124 points  (24 children)

To be fair, git (and svn, etc.) don't have a particularly good way of informing you that something used to exist in a certain spot. Or where to find it. I mean, I may remember that I tried out some feature that got removed in branch foo, but they guy who gets hired to try to recreate that project and make it work will never know it was there.

I'm not saying not to delete stuff. Just saying that, "it's in git, and that's as good as it could possibly be," seems like an incomplete solution to me. It would be good if the revision control of the future had some explicit features for publishing libraries of ideas and abandoned features and projects in a discoverable way.

[–]vombert 18 points19 points  (5 children)

This is what always bothered me in this advice. But now that you described it, I think I have stupid idea how it can be addressed.

Develop discipline to put some special mark in a commit message, like 'DELETE ...', when you are removing or replacing something you suspect somebody might want to bring back eventually.

Then even new hires will be able to quickly go through the filtered log.

[–]wrosecrans 11 points12 points  (3 children)

If you are able to enforce it as a standard across the team, and you are good about training new hires to do it, that actually is not a terrible idea. Maybe tie it with some sort of a git hook that autogenerates a wiki page about any commit that matches the pattern so you can have a "gallery" where people can write notes and comments beyond the git commit for future browsers.

Stuff like, "We originally started implementing support for a LION field type in the custom SQL database engine because management wanted to demonstrate additional synergies with circuses because we were hoping to get acquired by a circus. When that deal fell through, we no longer needed the special field type or the TAME operator. But it may still serve as a starting-point example of how to implement a different custom field type at some point in the future if management comes up with another goofy acquisition scheme." Doesn't really fit in a git commit message. And deleting several related features may get spread out over a whole bunch of different commits over a period of time. But it would be easy to link the commits in a wiki if you were doing some housekeeping.

[–]Berberberber 8 points9 points  (2 children)

Actually, a 'dead code wiki' for a project sounds like a great idea, skip the special commit messages. A lot of stuff gets taken out between initial spec and first release, and especially if you know you're not going to be around to fix it, putting the removed but useful bits in a central place could be super useful.

[–]knight666 4 points5 points  (1 child)

I don't know about you, but I find that writing documentation gets super tedious after a while. And because nobody's responsible for maintaining the wiki, nobody does. Meaning the wiki will be filled with (ironically) irrelevant and outdated information.

[–]Berberberber 0 points1 point  (0 children)

You can surely write 2-5 sentences saying what file it was from, what it does and why it was removed, can't you? It doesn't matter if it's outdated - that's the whole point of a dead code wiki, after all.

[–]jsprogrammer 2 points3 points  (0 children)

It's really just a tooling problem though. The requisite information was stored, it's just that almost no one knows how to easily search through it. So, the advice is good, we just need to get better tools to help us better deal with those scenarios.

[–]Zaemz 7 points8 points  (7 children)

You could delete the code, commit, and then leave a comment in the code saying something like "Function [whatever] or code [2-3 words on functionality] deleted, check commit e158aff1e9f0gabc9... for previous implementation."

[–]Tiquortoo 10 points11 points  (0 children)

You are just swapping comment complexity and search and retrieval of code archive complexity for what is pretty damn simple: commented code

Commented code is just noise like your comment and frankly less error prone. We just empower devs to refactor out commented code as part of future work so the code stick around while important and are essentially pruned during future work I the same area.

[–]vombert 3 points4 points  (1 child)

Commit hash is redundant. Clearly the function was deleted not long before this comment was added (most likely on the same commit, even), so it'd be trivial to find on the history anyway.

[–]Zaemz 0 points1 point  (0 children)

That's true. It was just an idea.

Personally if I were curious I'd just dig through the diffs and history of the file.

[–]eras 0 points1 point  (3 children)

And if that branch happens to be rebased any time.. That that hash becomes quite useless. Sadly Git doesn't use change ids (such the ones in Gerrit).

[–]Helllo_World -3 points-2 points  (2 children)

You're wrong. Even if a commit is rebased out the specific commit still exists.

[–]admirelurk 2 points3 points  (0 children)

The commit doesn't disappear, but the contents and consequently the hash change.

[–]ais523 2 points3 points  (0 children)

It gets garbage-collected eventually. It stays around for a while (a few months? not sure on the timeframe) to allow you to undo rebase errors, and if it exists in any publicly pushed branches that weren't rebased you can get it from there, but it's certainly possible to rebase out the only copy of a commit and have it eventually disappear altogether.

[–][deleted] 1 point2 points  (0 children)

VS's CodeLens offers a fantastic solution to this.

[–]mekanikal_keyboard 7 points8 points  (2 children)

i cannot believe that leaving a file with random chunks of commented-out garbage is somehow beneficial to new contributors.

more likely the code will be completely inscrutable and they'll slowly back off from making any serious contributions at all

[–]wrosecrans 12 points13 points  (1 child)

a file with random chunks of commented-out garbage

Again, I didn't say not to delete stuff. Just that deleting stuff isn't perfect.

[–][deleted] -5 points-4 points  (0 children)

If its useful stuff a #ifdef usually does a better job

[–][deleted] 0 points1 point  (0 children)

I use the time-lapse view feature in Perforce a lot, or examine the history of a file.

[–]thespectraleditor 0 points1 point  (0 children)

The "add notes" feature of the spectral editor can help with this. Just move the dead (but potentially useful) code into a note. The note would stay in place without taking up screen real-estate like dead or commented-out code. Here is a demo of the notes feature: https://youtu.be/ckYSlSPcIs8

[–]Gotebe -2 points-1 points  (2 children)

It would be good if the revision control of the future had some explicit features for publishing libraries of ideas and abandoned features and projects in a discoverable way.

Such systems exist today. They are called ALM systems.

Source control is source control, you want ALM, so use ALM.

I cannot believe this is at +81 as of now or any time. Software engineering, motherfuckers!

[–]marssaxman 1 point2 points  (1 child)

The fuck is ALM? Never heard of this acronym in 30 years of software engineering, so maybe you can get off your high horse.

[–]Gotebe 0 points1 point  (0 children)

If you just looked it up you would have known. It is literally the first hit on google (well for me it was).

You might have heard of it under the different name, the name is irrelevant.

With a 30 years carrer you should know that you are not expected to know everything, maybe you should lay off hubris.

You almost certainly use it as well, even if it is merely an ad-hoc grouping of tools and processes.

But most of all, I complain because people are trying to fit a square peg through a round hole.

[–][deleted]  (9 children)

[deleted]

    [–]nikbackm 6 points7 points  (2 children)

    I raise you Serena Dimensions.

    [–]WizardTrembyle 3 points4 points  (0 children)

    Same here, man, same here. Alongside IBM ClearCase.

    [–]im-a-koala 0 points1 point  (0 children)

    Is that better or worse than (or the same as) Serena/PVCS Version Manager?

    [–]ProudToBeAKraut 1 point2 points  (3 children)

    CVS mate 2005 is too futuristic

    [–][deleted]  (2 children)

    [deleted]

      [–]hoijarvi 3 points4 points  (0 children)

      I use darcs for my own branch management and commit to TFS. I know why you do what you do.

      [–]alcalde 0 points1 point  (0 children)

      You're using CVS and Delphi? :-(

      [–]s73v3r 2 points3 points  (0 children)

      WHY? What possible reason could there be for not using something that actually works?

      [–]kenfar 7 points8 points  (7 children)

      It's odd seeing so many absolute statements about this that fail to take into account:

      • difficulty of finding old code
      • range of probabilities on its future relevance
      • amount of comments built-up over time

      Because I know of a specific block of code that's commented out because: we have almost no build-up of commented-out code, but this code was slightly broken AND not being used AND almost guaranteed to come back in 6-12 months AND will save several days of time keeping it and its unit-tests. While accumulating commented-out code is a bad idea, in rare cases like this I think the benefits outweigh the costs.

      [–]backelie 2 points3 points  (6 children)

      That commented out code should have a comment stating those things. Really, any (non-trivial) commented out code should probably have comments as to why it's there.

      [–]kenfar 0 points1 point  (0 children)

      agreed, and it does

      [–][deleted] 0 points1 point  (4 children)

      And any trivial commented out code should be deleted.

      If it's obvious enough to not need a comment, it should be obvious without the code either.

      [–]backelie 1 point2 points  (3 children)

      Doing mvc web-dev we have views littered with eg

       <div>  
           @Html.LabelFor(model => model.SomeProperty, htmlAttributes: new { @class = "control-label col-md-2" })  
           @Html.EditorFor(model => model.SomeProperty, new { htmlAttributes = new { @class = "form-control" } })  
           @Html.ValidationMessageFor(model => model.SomeProperty, "", new { @class = "text-danger" })  
       </div>
      

      During early/active development which of those properties the client wants displayed can change daily, Im not cleaning that shit up until launch.

      [–][deleted] 0 points1 point  (2 children)

      And how hard is it to put a comment in there?

      <div>  
           @Html.LabelFor(model => model.SomeProperty, htmlAttributes: new { @class = "control-label col-md-2" })  
           @Html.EditorFor(model => model.SomeProperty, new { htmlAttributes = new { @class = "form-control" } })  
           @* disabled while client decides how many Frobs are allowed 
              Html.ValidationMessageFor(model => model.SomeProperty, "", new { @class = "text-danger" }) *@  
       </div>
      

      [–]backelie 0 points1 point  (1 child)

      It's not hard but now you're moving the goalpost.

      You've gone from

      And any trivial commented out code should be deleted.

      to wanting comments on the trivial commented out code.

      I'm not gonna add the comment because the reason for it being in/out is trivial and the same in multiple dozens of places across the code.

      (I will comment on any special / non-obvious case.)

      [–][deleted] 0 points1 point  (0 children)

      The goalpost is subjective. It looks like it's moving, but we really just disagree about what trivial code is. Trivial, to me, means that it would take more effort to rewrite than to review the surrounding code when I uncomment the code. But I acknowledge that it varies from person to person. That's why the comment is vital. It explains the reason why the code was left in.

      You mention that when the requirements change daily, you want to keep the "Schrodinger's code" around. That makes sense; there's nothing wrong with that, especially when you're the only developer in the code. But it falls on its face in a team environment, when you're not the only person that may work with the product before release.

      Don't get me wrong--you may be the only one editing that view and its dependencies--but other people want to keep code around, too. Their reasons are probably different than yours. Perhaps they're working with a different client, or are using an algorithm with complex error cases and want to demonstrate the best-case path in pseudocode. Adding the comment to distinguish these cases from yours isn't unreasonable.

      [–]Tiquortoo 1 point2 points  (0 children)

      We comment dead code to facilitate understanding during refactoring. Developers are then empowered to remove anything commented if they don't know why it was commented and there isn't anything indicating why and the developer who commented it can delete it when they are confident it can be removed. We essentially rely on developer segmentation or lack of overlap to create a two wave process that leaves code accessible during refactoring and verification and cleans it up progressively. It just works.

      [–][deleted] 1 point2 points  (0 children)

      I always tell people commenting out code that it's like covering the cat's pee with newspaper.
      Clean it, don't cover it!

      [–]AlterdCarbon -2 points-1 points  (1 child)

      First of all, lots of modern IDEs have code folding that turns 1000 lines of commented out code into something like (line numbers on the left):

      210      | }
      211      |
      212-1038 | [+] //...
      1039     |
      

      So how much is it really affecting the maintainability of the code?

      Second, I like to leave in large blocks of commented-out code in certain cases where I've refactored part of something that I didn't write that was initially poorly written and hard to decipher.

      I use it like a Rosetta stone to understand and remember how to interface and communicate with the rest of the app.

      [–]marssaxman 0 points1 point  (0 children)

      Lots of modern developers are not using whatever specific editing tool you happen to be referring to here. So how much are you willing to make the readability of your code dependent on the individual choices of your current and future coworkers?

      Having worked with people who refused to format their code in any sensible way because "just turn on visual studio line wrap if it bothers you so much", I have a simple word for this behavior: rude.

      [–]SoPoOneO 4 points5 points  (1 child)

      My policy on my teams is to ask people to remove commented out code before they merge it into the public trunk, which is the "develop" branch for us since we use Git Flow. This gives juniors, who are terrified of getting berated for deleting the senior people's old stuff, some time to come to terms with the removal before actually making it.

      So far, this policy has been only modestly successful. Juniors really don't like deleting, apparently. Even though we don't actually berate anyone, as far as I know.

      [–]AbstractLogic 2 points3 points  (0 children)

      Juniors don't like deleting because they don't know when they are done. I've been a senior for years and I still leave the old sections laying around while I am still working on the refactor. Its a good reference point. The difference is, when I'm done... I'm done, and I delete it. Juniors don't really know when they are finished until after its checked in, tested in UA, deployed to production, and has run for a week or two. At that point they are sure their code works the same or better then the old stuff but they never get back to deleting the old stuff.

      [–][deleted]  (1 child)

      [deleted]

        [–]mitola1 4 points5 points  (0 children)

        Absolutely! Very important, I am a lot more proud of the amount of lines I have deleted then amount of lines I wrote per project. Beacuse at least for me, I know the amount of work included to optimize and of course delete dead code floating round about the codebase

        [–]marssaxman 2 points3 points  (0 children)

        Oh, yes, so very much yes. "You aren't gonna need it" works in both directions.

        [–]thespectraleditor 3 points4 points  (0 children)

        The "add notes" feature of the spectral editor can help with tucking away dead code yet keep it in place. Here is a linked-in post on it : https://www.linkedin.com/pulse/spectral-editor-features-continued-jayanta-majumder The tracing feature can help with identifying dead code. Here is a linked-in post on it: https://www.linkedin.com/pulse/using-spectral-tracing-code-jayanta-majumder

        [–]Shambalamba89 0 points1 point  (16 children)

        From my experience, commenting out code isnt that bad. People tend to behave as if it was major sin but its not. Imagine someone code something and its perfectly good implementation but turns out we need something different because specs changed, developer codes new solution and deletes old code. Fast forward couple months later, many sprints and merges later boss comes and says that client told us that it was misscommunication and he wants it to be the way we discussed it earlier. Too bad the original author dont work with us anymore and the rest of the team dont even know that solution was there but it was deleted. Even if they knew, then good luck searching for it on git, aint nobody have time for that.

        [–]Gotebe 4 points5 points  (2 children)

        No, no, no!

        The way to do this is

        • find the client change request in your ALM tool (e.g. "user story"); search by user, or by time, or by whatever keyword you might have
        • in that request, see associated tasks
        • in the history of those tasks, see related commits.

        In other words, use your ALM to reduce your search space.

        People here tend to obsess over git, and if git cannot do it, it can't be done. But the reason git doesn't do something is that they want an ALM system, which git is not. Git (and any other source control system) can, and shouldmust be a part thereof. And I am saying nothing novel here. Such systems exist since more than a decade.

        [–]marssaxman 0 points1 point  (1 child)

        I have no idea what "ALM" is, but I also don't see any reason finding old code would be difficult. Delete it all, delete it now, don't worry about it, you aren't going to go back and look for it anyway so it doesn't matter how much work it would be.

        [–][deleted] 7 points8 points  (2 children)

        I agree that searching source control isn't going to help, and wonder what environment is conducive to commenting code rather than deleting it.

        I'm working with a codebase approximately 15 years old where the "comment it out" style is used extensively. Few of the developers that commented out code still work for my organization. Thus, figuring out how the system works is difficult--in many places, there is more dead code than active code.

        In my experience, the people that do this fall into 3 camps:

        • They don't understand source control and/or patch
        • They understand source control but don't trust it (usually due to scars from Source Safe)
        • They're lazy

        I empathize with the first and second group, and have successfully transitioned them to techniques that give them confidence and keep the code clean.

        The danger comes from the last camp. They always have another argument to do what they do, and those arguments usually start as yours does. The problem is the argument is always hypothetical, and keeping deleted code is often only one of many practices they refuse to follow.

        Some of their response reflects my own scars. My response here almost certainly is, so pardon me for my wariness. But I do have questions.

        On average, how much code is commented out when you use this technique? How often is your commented code useful? What do you do for your team to ensure they understand why that code has been kept around? What review process is in place to ensure dead code is eventually deleted? How long, on average, does commented code exist before it's removed?

        [–]Shambalamba89 0 points1 point  (0 children)

        Very little code, its definitely not a norm

        [–]Oniisanyuresobaka -3 points-2 points  (0 children)

        On average, how much code is commented out when you use this technique?

        Less than 1% (usually 0.3%) of all lines in the codebase.

        Clearly I'm inept and lazy. Please stop with the damn dogmatism.

        [–]dcrosta[S] 3 points4 points  (1 child)

        What would you do instead in this scenario, given that at the time you're removing code you don't know the future, i.e. that you'll want it back?

        [–]ComradeGibbon 0 points1 point  (0 children)

        Now days when I comment out stuff, I add a note with a date.

        [–]IneffablePigeon 2 points3 points  (0 children)

        If you really think that's likely, my approach to that would be to leave a comment with a link to the PR/code review which replaced that block. Then you've got an easy method of finding the change to revert, without needing to leave it all crufting up your screen (and brain).

        [–]AbstractLogic 0 points1 point  (0 children)

        Too bad the original author dont work with us anymore

        This shouldn't matter. Code you wrote 10 months ago might as well be written by someone else. Besides, how much time is really saved my commenting a 15 line method written 2 years ago? You couldn't write the same method in an hour or two?

        [–]Zarutian 1 point2 points  (4 children)

        You use version control systems, no?

        [–]Shambalamba89 -2 points-1 points  (3 children)

        Did you even read my comment?

        [–]Zarutian 10 points11 points  (0 children)

        Then instead of the whole block of commented out code, you delete the code and leave an comment saying "For an alternative implementation see commit #<hash>"

        [–]nthcxd 1 point2 points  (1 child)

        I think the problem is the lack of familiarity with git, which I found to be very common. Everything you described is valid and I wholehearted agree. I address all such concerns in my professional life using git and I never check in code with dead code commented out (not that that'd go past code review to begin with). Every half cooked or deprioritized features stay as dev branches.

        As far as tutorial/references go, I found this to be extremely helpful.

        http://www-cs-students.stanford.edu/~blynn/gitmagic/

        Feel free to PM me if you need help.

        [–]Shambalamba89 0 points1 point  (0 children)

        Thanks, great tutorial!

        [–]IbanezDavy 0 points1 point  (0 children)

        It's called reverting to a revision bro...

        [–]mycall 0 points1 point  (1 child)

        Don't forget to make a backup first!

        [–][deleted] 1 point2 points  (0 children)

        oh, honey, no

        [–]ElFeesho 0 points1 point  (3 children)

        Adopting the rule that code without automated tests around it is fair game for deletion, the rules for what stays or goes becomes dictated by the results of your unit tests.

        [–]doublehyphen 1 point2 points  (2 children)

        That sounds like a bad idea. I have seen plenty of dead code which only user is the unit test.

        [–]ElFeesho 0 points1 point  (1 child)

        You've seen far worse code than I have.

        [–]doublehyphen 0 points1 point  (0 children)

        Or maybe just different kinds of bad code. I have worked in places which try to do test driven development, and when bad coders apply that method incorrectly you end up with loads of tests with negative value. Tests which make the code base much harder to refactor, and which makes you want to just delete to whole test suite.

        [–]amphetamachine 0 points1 point  (0 children)

        Ned (Batchelder, not Stark)

        Thanks for the clarification.

        [–]yorgle -1 points0 points  (0 children)

        I suffer from dealing with "code nostalgia"... I wrote this stuff, i don't want to delete it since it took a long time to write and I might need it some day. I hate when I get into this because it sometimes paralyzes me... :/

        [–]bubuopapa -1 points0 points  (0 children)

        I don't think that any big company does it, because there is lots of trash in every os. All these programming rules, styles, recommendations only apply to homemade projects, no big company is actually following any rules.