Source Lines of Code

From Maisqual Wiki

Jump to: navigation, search

Contents


Definition

Source Lines of Code is a size-related metric that counts the number of lines of text in the source files.

SLOC counts the number of lines in the source file that are not:

  • blank or empty lines,
  • braces, or
  • comments.


Viewpoints

  • An artefact with a high number of lines of code will be harder to test and maintain. It may be divided into smaller functional blocks.
  • Some algorithms just are long. In these cases, dividing the code in many pieces will make it harder to read, and it may be better to keep things together for cohesiveness.


Notes

Variants

The SLOC metric has several variants:

  • Physical Lines Of Code (PLOC): The real number of lines of code read in the text source file.
  • Logical Lines Of Code (LLOC): The number of instructions -- there may be several instructions in one physical line of code.
  • Comment Lines Of Code (CLOC): The number of lines of comments in the text source file.

Consider the simple following Perl example:

foreach my $line (@all_lines) { print $_ if (m!^# \s+$!) } # Get structured information

In this sample, there are:

  • 1 Physical Line Of Code,
  • 2 Logical Lines Of Code,
  • 1 Comment Line Of Code.

Furthermore, the programming conventions used have a great impact on the SLOC variants (braces, 80-col lines, etc.):

for (my $i = 0 ; $i < $size ; $i++) { &sub_call($first_param, $second_param, $third_param); }

can be also written as:

for (my $i = 0 ; $i < $size ; $i++)       # foreach line in the file
{ 
    &sub_call(                            # get the sub_call function for:
              $first_param, # login:      #  the given login
              $second_param,              #  the given password
              $third_param); # e.g.       #  the given line
}

In this sample, there are:

  • 7 Physical Lines Of Code,
  • 2 Logical Lines Of Code,
  • 5 Comment Lines Of Code.

Instead of one single line of Physical Line Of Code for the previous sample.

Languages

SLOC measures cannot be compared between languages: the same feature may be written in one or many lines depending on the language grammar. See the following example (extract from Wikipedia article on SLOC)

C COBOL
#include <stdio.h>

int main() {
   printf("\nHello\n");
}
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. HELLOWORLD.
000300
000400*
000500 ENVIRONMENT DIVISION.
000600 CONFIGURATION SECTION.
000700 SOURCE-COMPUTER. RM-COBOL.
000800 OBJECT-COMPUTER. RM-COBOL.
000900
001000 DATA DIVISION.
001100 FILE SECTION.
001200
100000 PROCEDURE DIVISION.
100100
100200 MAIN-LOGIC SECTION.
100300 BEGIN.
100400     DISPLAY " " LINE 1 POSITION 1 ERASE EOS.
100500     DISPLAY "Hello world!" LINE 15 POSITION 10.
100600     STOP RUN.
100700 MAIN-LOGIC-EXIT.
100800     EXIT.
Lines of code: 4
(excluding whitespace)
Lines of code: 17
(excluding whitespace)


See also

Glossary:

Personal tools