Why should unused code be deleted?

I have heard many times that unused code should be removed from the project. However, it is not clear to me why? "

My deletion points:

  • The code is already written and the effort has been spent
  • The code can be tested in a synthetic and real environment.
  • If it’s well organized (grouped, separate package, loosely coupled, etc.), this does not bother you when analyzing common code or refactoring
  • The code may be used in the future.
  • When deleted, the author may feel uncomfortable

Could you explain why you delete (or save) unused code?

Thanks in advance!

+83
refactoring
Mar 29 '13 at 8:44
source share
12 answers

Here are a few reasons why unused code should be removed:

  • Everyone who works on a project should not only understand the working code, but also unused material. This is a lost time and creates confusion.

  • There is a danger that someday someone will make a change that is inadvertently associated with an "inactive" code and may lead to errors. I know that this happened in the projects that I worked on.

  • Maintaining any code is an administrative burden. Keeping the old redundant code, this load increases. For example, merging changes in the main branch becomes more difficult because there is more code to work through and more room for error.

  • Over time, it happens that more and more old unused code is added to the code base. This increases confusion, possible misunderstandings, and administrative overhead.

  • The likelihood that unused code will ever be used again is very unlikely. Over time, this reusability decreases. If the code needs to be removed and is considered important enough, it can be forked and documented.

  • Any personal feelings that the encoder may have about the code that they may have worked hard on are understandable. But in order to be a professional, it is necessary that these thoughts be directed in one direction for the best good. Time does not mean anyone, and there is no place to save historical code in a working code base.

+145
Mar 29 '13 at 9:02
source share

@suspectus did an excellent job of presenting reasons for code removal; I would like to refer to your individual bullets for storing code.

  • The code is already written and the effort has been spent

But if the code already written is not used, it costs only without (future) value. This effort is wasted, and conserving the unused product of these efforts does not confirm these efforts. We save the code because it is now useful not as a monument to the efforts of the authors.

  • The code can be tested in a synthetic and real environment.

Sorry, I don't know what you mean by that.

  • If it’s well organized (grouped, separate package, loosely coupled, etc.), this does not bother you when analyzing common code or refactoring

If it exists in the code base, no matter how well organized, it contributes to the burden of maintenance and understanding. True, he can be organized in such a way as to be less burdensome, but if he left, this will not be a problem at all.

  • The code may be used in the future.

At Agile, we say YAGNI : You won’t need it. Yes, perhaps in the future you can use it, but today we cannot know enough about tomorrow to be able to predict this with any reliability. To think differently is arrogance, inclined to arrogance. What we can know about tomorrow: we want our code base to be easily modified, and unused code distracts from this characteristic.

  • When deleted, the author may feel uncomfortable

The author must overcome this. We all wrote things that turned out to be not useful - it is much better to point to a piece of code that everything is used (because the unused crack was deleted) than to the body of the code in which you can say about several methods, "and that one is actually used!"

+22
Mar 29 '13 at 14:22
source share

Is it not so difficult to pick up the code and understand the intention, but now you need to find out which parts are not used?

+14
Mar 29 '13 at 9:00
source share

The code is already written and the effort has been spent

This is also not necessary. If you do not use it for anything, it is (by definition) useless, regardless of what it does or how much effort has been spent on it.

The code can be tested in a synthetic and real environment.

If it is useless, it is still useless, even if you have tests on it. If the code is useless, the tests for it should also be useless (so keeping the code with comments there, creating ambiguity - do you perform tests? If you have client code for the commented code, are you also commenting on the client code?)

If it’s well organized (grouped, separate package, loosely coupled, etc.), this does not bother you when analyzing common code or refactoring

Not this way. All your tools (source control, static analysis, documentation extractor, compiler, etc.) will work slower because they need to process more data (and more or less of this data is noise).

If the code is not well organized, on the other hand, it will ruin static analysis, refactoring, and any others.

You introduce noise into your instruments and hope that they handle it properly.

What if your static analysis tool calculates the comment / code ratio? You just messed it up with something that was relevant until yesterday (or whenever the code was commented out).

Most appropriate of all, commented blocks of code introduce delays in understanding the code for maintenance and further development, and such delays are almost always expensive. Ask yourself: if you need to understand the implementation of a function, what would you rather see? two lines of clear code or two lines of code and another twenty-six comments that are no longer relevant?

The code may be used in the future.

If so, you will find it in your SCM team of choice.

If you use competent SCM and rely on it to save dead code (instead of cluttering the source), you should see not only those who deleted this code (commit author), but also for what reason (commit message) and which other changes were made with it (the rest of the differences for this commit).

When deleted, the author may feel uncomfortable

So?

You (I guess) are a whole development team that gets paid to make the best software you know, and not the "best software you know how to not harm X's feelings."

This is the part of programming that will ultimately be discarded in most written code; for example, Joel Spolsky at some point said that for his company about 2% of the written code sees production.

If you determine the priority of the ego of developers over the quality of the code base, you will sacrifice the quality of your product, because ... what exactly? Keep the immaturity of your fellow developers? Protecting the unrealistic expectations of your colleagues?

Edit: I saw one real reason to leave the code with comments in the source, and this is a very specific case: when the code is written in a strange / non-intuitive way and the clean way of rewriting does not work for a really subtle reason. This should also be applied only after a repeated attempt to correct the problem and every time an attempt is made to reintroduce the same defect. In this case, you should add the commented-in intuitive code as a comment and explain why it does not work (so that future developers will not repeat the same change):

// note by <author>: the X parameter here should normally // be a reference: // void teleport(dinosaur& X); // but that would require that we raise another dinosaur and // kill it every twelve hours // as such, the parameter is passed by value void teleport(dinosaur X); 
+13
Mar 29 '13 at 15:58
source share
  • Fear . This makes the team worry more and produce less. The amount of fear increases exponentially when a deader code is entered. "We don’t know if this bit is being used, so we dare not remove it or touch it."
  • Sweeping changes . If something that needs to be changed everywhere in the system there is also dead code, do you change it? It is very difficult to understand if it is definitely not used somewhere, so this is always a risk. And even if it doesn’t break anything, does dead code work at all if it is returned to use after this change?

    When dealing with radical changes, developers will also need to check every place that contains code, and in the case of dead code, this is redundant. And checking them takes longer when the code is dead, since it is difficult to verify that it is not used anywhere.

  • Mental stress . Every time you need to think about whether or what to do with dead code, some of your brains are required.
  • Wild goose chases . "I need an example how to use Foobar. Oh, this is in these places in the code base. Patient check the first hit and find out where it is in the user interface. Hmm ... I can not find it anywhere.
  • Inflated reports (for example, how many lines of code, classes, routines, changes). It distorts the visibility of the project and the decision about which parts of the code base should be processed, and the evaluation of future projects.
  • Weak trust in the codebase . This can lead to more time spent on redundant tasks, and this breaks the flow of code base utilization. Developers may have to check very carefully that everything they use will work as they think.

This is very valuable if you know that part of the code is not used, because then you can delete it. If you allow it to stay, then in the future it can be difficult or almost impossible to be sure that it is not actually used. For example, some things that use code in amazing ways: reflection, dynamic calls, concatenated from strings, eval, frame magic .

However, if there is a high probability that the code will be used in the future, it is easier to add it if it is located on a different code, and not in the version control system. You may not remember the words that were written after some time, so it is very difficult to find the code from the bowels of VCS. But I would let dead code rarely exist, and even then I would comment on the code.

+9
May 11 '16 at 12:27
source share

Dead code pollutes your code

Dead code reduces comprehensibility and readability.

The best codes are always reused, and if they have dead codes, this reduces the possibility of reuse

We are guided by a modular approach to coding, where we develop codes for interaction with our fellow programmers, and not for the machine. We need to make the most of energy to make code easier to understand. In any case, the car will be in order.

Dead or commented code is like false footprints that only confuse people, so avoid it at all costs.

+7
Dec 29 '15 at 7:45
source share
  • Unused code is more search space for you and for everything else that usually scans your code. For example, compiler, IDE, file search, debugging, static analysis, much more for viewing, file inclusion, extraction from VCS, etc. This slows down these processes and adds significant noise.
  • Unused code is not always dead code. May be performed under certain circumstances. This can not only offer a vector for errors and performance issues, but it can also be a security issue. In terms of performance, this can be expressed in unexpected ways, such as large downloads.
  • Unused code generates unused code. If you delete a function call and then search for the use of this function to find out if you still need it, you can see the match from the previous unused code and assume that you can save it. The more unused code, the more hops to determine if the code is being used.
  • Unused code still often requires support. Suppose A and B depend on C. Of these, B is not used. You modify C, and then B does not compile, because you removed the element from the structure in C that B requires, now you must fix B or actively remove it from compilation. You should have simply deleted it.

This list may seem simple, but each one appears in hundreds of different ways, adding a drag and drop that synergizes throughout the development process. Inefficiency can often be proved or demonstrated in a direct and mathematical way.

In response to your points ...

  • The code is already written and the effort has been spent

But this often needs to be maintained. It will also appear in things like file search.

  • Code can be tested in a syntactic and real environment.

I'm not sure what you mean by that. I think this is the same as the last. You mean that the code has already been tested, and cleaning it up may require re-testing. This is a cost that is usually worth it because it will pay off 90% of the time and to avoid cleaning it before production starts. Almost all code has two iterations: make it work, make it clean. The reason is that you need to double check because someone missed the last step. If your code is also too expensive to be tested, read diff, test it (which is most likely to happen if it gets confused in a lot of unused code), etc.

  • Well organized (grouped, separate package, loosely coupled, etc.), it does not interfere with your overall code analysis or refactoring.

In any case, your code should be like this, but it only moderately alleviates the problem. The strangest argument is to hear that something must be organized, but unclean. It's okay to try to keep the code modular and reduce dependencies, but you also want to reuse the code, and if all of your modules are island, most likely you were NOT DRY. You may also find yourself performing excessive decoupling that does nothing but reduce the problem of unused dirty code.

  • The code may be used in the future.

Many people have written code for value. If it is not used now, then it is deadweight, and in reality, when you go this way, often only part of the unused code becomes used code. In all likelihood, unused code is unlikely to be usable or used code. The most likely reusable code is already used code that does something.

Even worse, unused code has no purpose. When someone comes in and needs to change something, which ultimately affects the unused code, he will be puzzled sitting there trying to figure out what this unused code should do without purpose.

It's easy for people to feel this way when you start, as the code requires a lot of effort. However, when he is fluent and used to it, the code becomes like riding a bicycle. You will find that the cost of writing such a piece of code will drop dramatically.

  • When deleted, the author may be uncomfortable

This is the author’s problem. On the one hand, it is selfish to leave a bunch of unused code so that others deal with it. On the other hand, if the author expresses his feelings about the quality of the code, he probably should not code. You are walking the road with this one of you cannot fix their code when it is broken, because it will hurt their feelings. This is a bad sign if someone is attached to the code simply because it belongs to him, and not because he is good. An author should feel happy when cleaning up his code. It looks like someone is taking out the trash and throwing it in the trash bin.

I would be in seventh heaven with happiness if someone did this for me. What can make it easier to overcome these feelings is, instead of waiting for someone else to do it, try to do it yourself. Continue to iteratively rewrite the code snippet you made so that it works better, move faster, with less redundant and more flexible, but with less code each time. Try not to feel good about the amount of code, but how much you can achieve with a little code. This is very important for raising the level, and as soon as you do this, all your code will turn out at a good level, so it will not need to be aligned so often.

+3
Jun 11 '18 at 21:49
source share

First of all, you should always use the source control tool to manage your projects and, therefore, deleting unused code is good practice, since you can always return using the source control to get the remote code. For me, the reason for removing unused code is that only those who know that the code is not in use know it, someone in the team will meet this code and try to figure out what it does and how it fits the entire application and will feel disappointment after so much effort that the code is not used at all :)

+2
Mar 29 '13 at 8:55
source share

I think that you can have two cases: - application code: if it is not used, it may be unchecked and not working for a while, maybe you can go to the "internal code repository", - API code: if you write library, then keep in mind that this is the best choice to support it, but inside your active development process.

+2
Mar 29 '13 at 8:55
source share

Are you sure the code is not in use?

It is not enough to verify that the code is still compiling. In C ++, if you remove the “unused” implicitly defined method, such as operator= you don’t get a compiler error, the class will just quietly start using the (potentially incorrect) default implementation. In Java or C #, code can be used through reflection. In object-oriented languages, inheritance can play a role (now you can call a base class). In almost any language, another overloaded function can be used.

Check the age of the code in version control, and not just that it is not used. I saw code that looked unused, but just committed and actually became the first step in another developer project.

Aggressively remove unused code

You pay for maintaining the code:

  • Fix broken assemblies (development time). We recently had a complex #include change chain introducing a new overload into unused code, which led to a headache of a reasonable size for each engineer in a team of dozens of developers.
  • Resources of machines on tests (provided that you have a self-test of continuous assemblies). My team recently checked all of our slowest tests, and many of them were due to unused code. Engineers performing tests locally or as part of continuous integration are waiting for tests on unused code.
  • In terms of readability (development time again). Your header files represent the API. If they include functions that no one would like to use, but everyone should read, your learning curve will be much more complicated.
  • Looking for code (development time again). Would you clean your house, your hard drive, or Google Drive? The more you do a domain search, the more important it is to have relevant content to avoid false positives (or you use a more sophisticated search, such as a search engine on the Internet).

I would say that almost all the code that the average developer writes becomes unused on the five-year horizon, so this activity never stops. Do not let it be you; write only high-quality and absolutely necessary code.

+2
Dec 15 '17 at 2:26 on
source share

This discussion has been around for several years, but I just stumbled upon it ...

One thing that I haven't mentioned is the work that needs to be done to remove unused code. In many cases, the time and effort to remove unused code is not trivial in nature, plus there are usually additional costs for testing and documenting the reorganized system. Just one more thing to consider when making decisions.

+2
Mar 22 '18 at 20:43
source share

I think it is better to delete unused code, if it is already deleted, we can see again in the svn / cvs history, it is very easy to compare both codes without loss of performance.

+1
Jun 03 '15 at 4:34
source share



All Articles