Could someone explain the pros of deleting (or keeping) unused code?

前端 未结 11 984
温柔的废话
温柔的废话 2020-12-12 11:01

I have heard many times that unused code must be deleted from the project. However it is not clear for me \"why?\".

My points for not deleting that are:

11条回答
  •  情深已故
    2020-12-12 11:43

    Are you sure the code is unused?

    It's not enough to check the code still compiles. In C++, if you remove an "unused" implicitly defined method like operator= you're not going to get a compiler error, the class will just silently begin to use a (potentially incorrect) default implementation. In Java or C# the code might be used via reflection. In object-oriented languages, inheritance can play a role (the base class may now be called). In almost any language, another overloaded function may have taken over.

    Check the age of the code in version control, not just that it's unused. I've seen code that looked unused but had just been committed, and was actually the first step in another developer's project.

    Aggressively remove unused code

    You pay to maintain code:

    • Fixing broken builds (engineering time). We recently had a complicated chain of #include change, introducing a new overload to unused code, leading to a reasonably-sized headache for every engineer on a team of dozens of developers.
    • In machine resources on tests (assuming you have self-testing continuous builds). My team recently looked at all of our slowest tests, and many of them were over otherwise unused code. Engineers running tests locally or as part of continuous integration are waiting for tests over unused code.
    • In terms of readability (engineering time again). Your header files represent an API. If they include functions no one would want to use but everyone has to read, your code's learning curve is that much harder.
    • In code searches (engineering time again). Would you clean up your house, your hard drive, or Google Drive? The more you search a domain, the more important it is for it to have relevant content to avoid false positives (or you use more sophisticated search like a web search engine).

    I'd say essentially all the code the average developer writes becomes unused on a five year horizon so this activity never stops. Don't let this be you; only write high quality and absolutely necessary code.

提交回复
热议问题