When, if ever, is “number of lines of code” a useful metric?

后端 未结 30 2483
無奈伤痛
無奈伤痛 2020-12-08 09:32

Some people claim that code\'s worst enemy is its size, and I tend to agree. Yet every day you keep hearing things like

  • I write blah lines of code in a day.
相关标签:
30条回答
  • 2020-12-08 09:48

    At least, not for progress:

    “Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” --Bill Gates

    0 讨论(0)
  • 2020-12-08 09:50

    like most metrics, they mean very little without a context. So the short answer is: never (except for the line printer, that's funny! Who prints out programs these days?)

    An example:

    Imagine that you're unit-testing and refactoring legacy code. It starts out with 50,000 lines of code (50 KLOC) and 1,000 demonstrable bugs (failed unit tests). The ratio is 1K/50KLOC = 1 bug per 50 lines of code. Clearly this is terrible code!

    Now, several iterations later, you have reduced the known bugs by half (and the unknown bugs by more than that most likely) and the code base by a factor of five through exemplary refactoring. The ratio is now 500/10000 = 1 bug per 20 lines of code. Which is apparently even worse!

    Depending on what impression you want to make, this can be presented as one or more of the following:

    • 50% less bugs
    • five times less code
    • 80% less code
    • 60% worsening of the bugs-to-code ratio

    all of these are true (assuming i didn't screw up the math), and they all suck at summarizing the vast improvement that such a refactoring effort must have achieved.

    0 讨论(0)
  • 2020-12-08 09:50

    It's a metric of productivity, as well as complexity. Like all metrics, it needs to be evaluated with care. A single metric usually is not sufficient for a complete answer.

    IE, a 500 line program is not nearly as complex as a 5000 line. Now you have to ask other questions to get a better view of the program...but now you have a metric.

    0 讨论(0)
  • 2020-12-08 09:51

    They can be helpful to indicate the magnitude of an application - says nothing about quality! My point here is just that if you indicate you worked on an application with 1,000 lines and they have an application that is 500k lines (roughly), a potential employer can understand if you have large-system experience vs. small utility programming.

    I fully agree with warren that the number of lines of code you remove from a system is more useful than the lines you add.

    0 讨论(0)
  • 2020-12-08 09:53

    Lines of code isn't so useful really, and if it is used as a metric by management it leads to programmers doing a lot of refactoring to boost their scores. In addition poor algorithms aren't replaced by neat short algorithms because that leads to negative LOC count which counts against you. To be honest, just don't work for a company that uses LOC/d as a productivity metric, because the management clearly doesn't have any clue about software development and thus you'll always be on the back foot from day one.

    0 讨论(0)
  • 2020-12-08 09:56

    When the coder doesn't know you are counting lines of code, and so has no reason to deliberately add redundant code to game the system. And when everyone in the team has a similar coding style (so there is a known average "value" per line.) And only if you don't have a better measure available.

    0 讨论(0)
提交回复
热议问题