I know many despise KLOC as some managers attempt to correlate that with productivity. But when we speak only comparing sizes of projects using the same languages and coding standards as well as the same tool for LOC calculation. Unlike story points, this seems like a more reasonable way that size of two projects can be compared.
-
Depends what you want to compare - if you literally wants to compare the size of the project source code, it is a fine measurement. But if you want compare things like complexity or amount of work then it only makes sense if the two code bases have been written by the same people in the same language on the same platform. – JacquesB Oct 09 '18 at 07:50
-
@JacquesB It can used to normalize e.g. defect count, as used in Defect density. – John V Oct 09 '18 at 07:54
-
9It is like asking "is it OK to compare people by weight"? Well yes you can do that...the question is what conclusion you want to draw from that comparison. If you literally want to know if one person is bigger as another, then comparing by weight is fine. The problem is when people draw conclusion like assuming weight corresponds to efficiency at work or something like that. So the question is what conclusion you want to draw based on this comparison. – JacquesB Oct 09 '18 at 08:53
-
How could you compare the size of a project using story points? Story points are used to estimate the time to to accomplish a piece of work relative to some other pieces of work. They aren't a measure of size, and I've never heard someone using them to compare the size of two projects. – Chris Cooper Oct 09 '18 at 15:54
-
@ChrisCooper Actually some do that. SPs are about effort mainly, and as such you might get a relative size of a project by considering all the estimated efffort. if project A takes 2000 SP and project B 500 SP, you can be sure it is roughly a quarter. But it only works if the same reference stories are used for estimation. – John V Oct 09 '18 at 16:16
-
@ChrisCooper Several books define story points as a unit for measuring size: Story points are a unit of measure for expressing the overall size of a user story (Agile processes in Software Engieering and Extreme Programming). – John V Oct 09 '18 at 16:17
-
@ChriCooper Here you can even see how they define a story point for the whole organization so as to use it with CMM processes: https://www.researchgate.net/publication/285848786_Using_Agile_Story_Points_as_an_Estimation_Technique_in_CMMI_Organizations – John V Oct 09 '18 at 16:24
-
I think you are not comparing the same thing (and from a brief glance, misunderstanding that research paper). The size they are talking about is the size of the effort required for a single task. KLOC is a measure of the size after the code is written. These are completely separate concepts. – Chris Cooper Oct 10 '18 at 10:28
-
@ChrisCooper The effort and size in KLOC both describe somehow size of a project. Not real size. The point of this paper is that story points can be normalized across an organization and then you can simply compare projects - Project A 100 SPs, Project B 50 SPs , so it should be half as big. – John V Oct 10 '18 at 11:16
-
Do you mean "project A took 100 story points to complete"? Because that's a nonsense measure of size. Story points aren't a measure of size. They are an estimation of how much effort something *will* take (future tense). As soon as the work is done the estimated number of story points is irrelevant, because if the work was easier/harder than estimated then you can't use the estimate to gauge the size. KLOC is an actual measure of size, but I get the impression you're really trying to ask something else here or already know what answer you want to get... – Chris Cooper Oct 10 '18 at 11:36
-
@ChrisCooper Please read that paper. Yes, story points are about effort but their point is to allow people with different skills to communicate. I can agree with you that something is 1 SP, but it will take two days for you and a week for me. But if all projects are estimated using the same reference story, you can then compare their "size" relatively. This is what the paper tries to demonstrate. – John V Oct 10 '18 at 11:43
-
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/84276/discussion-between-chris-cooper-and-john-v). – Chris Cooper Oct 10 '18 at 11:44
-
@ChrisCooper I can later, from home (firewall at work blocks chat :) ) – John V Oct 10 '18 at 11:45
4 Answers
The answer to this question will depend on your coding standards. The stricter they are, the less variation there can be in code produced by two developers, thus the closer the KLOC count will be for the same solution from different people.
But let's assume you work for a company that doesn't mandate every last detail and allows a degree of self-expression when writing code. To take an example, consider the following two ways of writing a method in C# for the fizzbuzz game:
Version 1
IList<string> GenerateFizzBuzzList()
{
var fizzBuzz = new List<string>();
for (var i = 1; i <= 100; i++)
{
var entry = "";
if (i % 3 == 0)
{
entry += "Fizz";
}
if (i % 5 == 0)
{
entry += "Buzz";
}
if (entry == "")
{
entry = i.ToString();
}
fizzBuzz.Add(entry);
}
return fizzBuzz;
}
Version 2
IList<string> GenerateFizzBuzzList()
{
var fizzes = Cycle("", "", "Fizz");
var buzzes = Cycle("", "", "", "", "Buzz");
var words = fizzes.Zip(buzzes, (f, b) => f + b);
var numbers = Range(1, 100);
return numbers.Zip(words, (n, w) => w == "" ? n.ToString() : w).ToList();
}
Both solutions are written in the same language (C#). Both solutions follow the same rules on naming, brace layout and various other conventions (eg using var
) that are common in coding standards. Yet version 1 is nearly three times the size of version 2.
Scale these two approaches up to a moderately large app and we might have 80,000 lines in one project and 220,000 in another. But that 140,000 line difference is purely down to the approach taken, rather than because one does more than the other. KLOC can only tell you the comparative size of functionality if exactly the same approach to writing code is adopted for each project. In reality, exactly the same approach is only used by poor quality devs who never learn new techniques and ways of expressing code, ie in reality KLOC is just a measure of the number of lines of code. It measures nothing else so is of no use in trying to measure anything else.

- 38,972
- 9
- 88
- 121
-
Then why is it sill being used? Surely there will be variation even with stricter standards, but 2-3x as much is not acceptable. But how else express the size of a project, for the sake of comparison of the defect rate, if we keep the example. – John V Oct 09 '18 at 10:03
-
"*Then why is it sill being used?*". A very good question and I wish I knew the answer. As I said in my answer to your other question, I don't believe that defect density is a useful measure in the first place, so I see no reason to put any value in measuring the size of a project. – David Arno Oct 09 '18 at 10:29
-
The coding standard is a false problem. For KLoc there is a normalisation required (empty line+comment lines) and differences in style are usually neutralized by using a pretty printer before counting. – Christophe Oct 09 '18 at 10:56
-
5@Christophe, I disagree. No amount of empty line removal and pretty printing will achieve Loc parity between my examples. Example 1 contains over 50% more statements and expressions, which is as close as you could get them to each other. 50% difference still makes a mockery of Loc being a useful measure of anything except Loc. – David Arno Oct 09 '18 at 11:07
-
@DavidArno I would say that in practice, we tend to define what expressions are to be used (lambdas? anonymous methods? ternary operator for IFs) so the difference should not be that massive. – John V Oct 09 '18 at 11:38
-
@DavidArno what I'm trying to say is that kloc is neutral, just as kilometers are: having a bigger number of km does not guarantee that the journey is nicer nor take more time. But you can nevertheless count the selfies per km or the fuel consumed per km, if you think it's useful in a way or another. Normalising the measurement is just to make it more consistent, so that the distance isn't polluted by unnecessary slaloming, or making extra rounds on side streets. While kilometers are a very bad indicator for time, confort, site-seeing etc, it's still the main measure of distance. Same for kloc – Christophe Oct 09 '18 at 12:59
-
2@Christophe: To use your analogy: KLOC is like counting a distance in **paces**, not kilometers. Not everyone's paces are the same, and therefore it's a futile effort to guess whether you or I travelled the furthest distance, based solely on the knowledge that I took X paces and you took Y paces (= it's pointless to compare code bases based solely on their KLOC). Only when you know that our paces are the same length (= normalizing the coding standard between the code bases) can you use paces as a measure for distance. – Flater Oct 09 '18 at 13:53
-
@JohnV: As a simple example, consider the difference between me and Bob. I chain my LINQ methods (with lambdas, as per the agreement). Bob does not chain his LINQ methods (but uses the exact same lambdas). Massively different KLOC, yet there is no difference in functionality whatsoever. The compiled IL will be exactly the same regardless, and the code has equivalent readability. – Flater Oct 09 '18 at 13:58
-
@Flater I see. Unfortunately I think besides function points, which are terribly expensive, there is nothing else to measure or at least estimate size. I think some variance is no problem, but twice as much would be.. – John V Oct 09 '18 at 14:03
-
1@JohnV: `there is nothing else to measure or at least estimate size` Why try to express it in function of size to begin with? It's not about size, it's about **complexity**. As a very basic (and massively oversimplified) counterexample, it would be more meaningful to compared _compiled IL size_ instead of source code size, as it removes the factor of coding style and focuses solely on the required functionality. – Flater Oct 09 '18 at 14:06
-
@Flater Because various metrics need size for normalization, like the mentioned Defect rate example. How would you measure Defect rate per complexity? – John V Oct 09 '18 at 14:17
-
@Flater no, kloc is not a measure of pace. For pace you'd need to relate kloc to time. And however long it takes for someone to write a kloc, in the end it'll still be a kloc. – Christophe Oct 09 '18 at 14:36
-
I could easily knock 9 lines out of your first example without changing the generated IL at all. Granted, it would be bending the style rules a bit, but it would still be very readable. – Robert Harvey Oct 09 '18 at 15:07
-
@RobertHarvey, of course you could. You could easily knock eight lines out by moving/removing braces. But then you wouldn't be complying with my coding standards. And even with those eight lines removed, it's still twice the size of my other example, which could easily be made smaller too. And this just reinforces the meaningless nature of LoC as a measure of anything other than the number of lines. – David Arno Oct 09 '18 at 15:11
-
Well, I wouldn't remove the outside braces, but the inside ones are superfluous (cue brace flame wars here), and the `if` bodies could be placed on the same line. The second example strikes me as the kind of tossing and turning that happens when those enamored with Linq want to use it for everything. – Robert Harvey Oct 09 '18 at 15:15
-
1"*The second example strikes me as the kind of tossing and turning that happens when those enamored with Linq want to use it for everything*". It's called "declarative programming". It has numerous advantages, in that it mutates no variables and is simpler to reason for many folk. But others do indeed see it as "tossing and turning" for they prefer imperative programming. Each to their own and completely off-topic for this question. – David Arno Oct 09 '18 at 15:17
-
3@Christophe: You misunderstood the term "paces" as Flater used it. Pace was used with the meaning "a single step taken when walking or running." How many meters are there in one pace? – Bart van Ingen Schenau Oct 09 '18 at 15:31
-
@Christophe Bart is correct, pace was used as "a step", not "a rhythm of steps". Time was not a factor in my example. – Flater Oct 09 '18 at 20:36
-
1exactly the same approach is also often used when code review systems are in place that make it impossible to write code in any but a single way that was dictated a decade ago by someone no longer in the company... – jwenting Oct 10 '18 at 05:10
Why are KLOC still in use ?
KLOC is neutral measure of the size of code, just as kilometers are a neutral measure of distance.
And just as KLOC, kilometers are not very useful: a larger number of km does not guarantee that the journey is nicer, more pleasant or with more sightseeing. Kilometers are also a bad predictor: they are not telling you how long it will take to reach the target, and neither how much fuel you'll need.
Yet, kilometers are still the main unit of measure for distance. And certainly for similar reasons, KLOC are still in use for size measurement.
What do you want to measure with KLOC ?
KLOC is a (very) bad indicator for productivity: writing a nice reusable and maintainable classes/functions can result in much smaller code, than dumb copy/paste/edit and repeating oneself for ever. But shorter code in same time might appear as less productive, although it's quite the contrary on the long run, since you'll need to code less thanks to reuse, and you'll need to test less, as you can rely on already tested components.
But KLOC is a valid measure of size. As every unit of measure, it has some shortcomings: not all lines are of same complexity, and layout and style can influence the quantity. But with some normalisation (such as ignoring empty lines and comment lines, or applying some pretty printing to neutralize style differences), over a large code base, it will have some statistical meaning.
Size only measures a quantity. It can influence compile time or reviewing time. It is used in litigations on intellectual property, to measure the degree of similarity between two different source codes. But don't expect it to be a good predictor of time, effort, or complexity (10 liens of recursive code are still more complex than 50 lines of sequential satements).
What are the alternatives ?
Story points are subjective to the team that made evaluation: you can't know for sure if this metric corresponds to size or to effort.
There are also function points that aim at more objectivity. But these are not readily available from the code and require an expensive analyse with also some room for interpretation (although less than with story points). I am also not sure they are still relevant in an OO worlds with GUIs.
Story points and function points are anyway estimates of requirements and expected features. They are evaluated before the code is written, and will in general not be updated if they appear to be inaccurate. They may give an idea of the complexity, but not at all of the size of the code that will be produced.

- 74,672
- 10
- 115
- 187
-
Even though most books consider story points indicative of effort, they are still relative to the team. So I could not compare two projects as 5 SP for Project A might mean 2 SP for project B. – John V Oct 09 '18 at 07:28
-
@JohnV you may find interesting material on the web site to which I linked for story points: there are several other articles about story point vs. ideal days and work hours. You may use some kind of correction factors between so from different teams, but how can you ensure it's reliable ? And are the sp estimates updated if it turns out that a story was more difficult than expected ? And if you take into account burn-down of the respective team, how will you make the difference between the sp estimate error, and the level of performance of the team (which both impact burn-down rate). – Christophe Oct 09 '18 at 08:01
-
I know that website but exactly for reasons you mention, I do not think I can compare different teams and their SP estimate (one can estimate whole product for 200, the other for 400..) – John V Oct 09 '18 at 08:06
-
@JohnV so in the end, the main question will be: what do you really want to measure and for which purpose ? And a complementary question: how accurate have your statistic findings to be ? (Hint: the accuracy requirement might not be the same depending on whether you're doing reliability prediction on thousands of projects for the pentagon, or if you're predicting customer satisfaction based on defect rate of a few company projects). – Christophe Oct 09 '18 at 08:11
-
Well with normalized size, I can look at things like number of critical defects and can compare across projects. So the number is jsut indicative. – John V Oct 09 '18 at 08:17
-
Ok. If it's just indicative, you have the choice. Two more questions for you to consider: 1) for a new release, do you look at the total size or the delta size ? 2) will your indicative statistic affect the team (e.g. quality objectives?)? If answer is yes, you should avoid sp, since you'd hijack its intended purpose; this might then lead the team to more prudent estimates, which could backfire on the team dynamic and overall throughput. Kloc is more neutral in that case. – Christophe Oct 09 '18 at 10:46
-
Well even if I collect the result just for myself, I would need to compare the teams. SPs are too subjective IMO, something that was estimated 10 could be (new member in team with expert knowledge) re-estaimted to be just 1 etc. – John V Oct 09 '18 at 10:55
-
I disagree with the KLOC/kilometer analogy. Simply put, KLOC ignores the _length_ of a LOC, which is incredibly relevant to any measure of size/complexity. It would be more accurate to compare KLOC with **paces**, which are subjective in length, but they allow you to express the "amount of steps taken", which gives you an idea of effort exerted. For example, a giant walks 100km, A mouse walks 100m. Who exerted more effort? Impossible to tell, because _how big is this giant_? However, if I tell you that the giant took 100 paces and the mouse took 10,000 paces, it becomes much clearer. – Flater Oct 09 '18 at 14:09
-
@Flater If you print out your source code, there will be a proportionality between the length of your listing in km and the kloc. No other measurement is as close to the analogy ;-) Joke aside, while I agree that a line can be more or less dense, the pretty printing can make the measurement more homogenous, and on a huge number of lines, the differences will be less relevant from a statistical perspective (normal distribution? Of course there is some arbitrary in any choice of measurement. You could use kb of code, but it also has its flaws. – Christophe Oct 09 '18 at 14:49
-
@Christophe Pretty printing does not homogenize size. It further removes the kloc value from a meaningful approximation (such as complexity) to a meaningless approximation (personal style/increased likelihood of certain items being spread over multiple lines compared to a functionally equivalent counterpart) – Flater Oct 09 '18 at 20:40
-
@Flater it homogenise size in that the same code will be measured to the same number of lines independently of formatting style used by different individuals. If I take David Arno's first example you could have somewhat between 10 and 22 lines for the same code depending on formatting convention. You can test here https://codebeautify.org/csharpviewer (using beautify harmonizes, using minify tries to avoid lines with only a brace). Is it possible that you have an anti-kloc allergy ? What do you have to propose as an answer to help OP ? – Christophe Oct 09 '18 at 20:48
The problem with LoC for estimation is that you dont know in advance how many lines the final code will contain.
In terms of a post measure of the complexity and size of a project its as good as any other measure.
Assuming that people have a style of coding that they generally stick to and a speed at which they generally type. Then yes, you can look back at the code they wrote and see which tasks were easy and which were hard by the final LoC and the time it took for the dev to write them compared to their average speed.
This doesnt really help you in estimating future work though.

- 70,664
- 5
- 76
- 161
-
According to the many comments it turns out that OP is not interested in estimating future work, but in comparing number of bugs with the size of code. I suppose the aim is to see if some projects are more buggy than others in relative terms. – Christophe Oct 09 '18 at 18:16
You're confusing two entirely different things here.
Managers care about functionality, because that's what marketing has sold. They don't care about project size at all except as an indicator for completeness of functionality. It's a very, very weak indicator, and only bad managers rely on it if there's anything better available, but it happens.
Programmers care about project size because that determines how hard the code base is to handle. They don't (primarily) care about functionality except insofar as that is what pays their checks. Unlike managers, programmers would prefer a smaller code base, other things being equal.
This means that the two situations are not comparable at all. KLOC is a useful measure to judge project size, and a very useless measure to judge completeness or productivity. There is no contradiction here.

- 107,706
- 45
- 295
- 310
-
1No, managers often (and you ran read many experiences of the others) think that the more you write, the more productive you are. Thus people are more expressive in order to inflate their SLOC. – John V Oct 09 '18 at 07:15
-
-
@Ewan Not just per day. Some want to know who wrote how much and equate that to productivity. – John V Oct 09 '18 at 07:53
-
1obviously given unlimited time you can write unlimited lines of code. productivity is a time based metric – Ewan Oct 09 '18 at 07:55