Thursday, July 15, 2010

How many lines to code the same?



On my last post I compared several programming languages based on the number of keywords they define and how many are needed, and not needed, in your code to build a simple OO program. We will see now how they compare on “Lines of Code”, but before that, let’s begin with some wiki-Definitions (taken from: http://en.wikipedia.org/wiki/Source_lines_of_code):

Source lines of code (SLOC) is a software metric used to measure the size of a software program by counting the number of lines in the text of the program's source code. SLOC is typically used to predict the amount of effort that will be required to develop a program, as well as to estimate programming productivity or effort once the software is produced.”

Advantages:
1.       Scope for Automation of Counting: manual counting effort can be easily automated.
2.       Intuitive Metric: due to the fact that it can be seen and visualized.

Disadvantages:
1.       Lack of Accountability: not really useful to measure productivity of a project only with results from the coding phase.
2.       Lack of Cohesion with Functionality: program developed using less code can have better features.
3.       Adverse Impact on Estimation: because only based on the coding phase, estimates can adversely go wrong.
4.       Developer’s Experience: lines of code differ from person to person due to experience.
5.       Difference in Languages: lines of code differ from language to language due to language design and features. It will require different effort depending on the language.
6.       Advent of GUI Tools: GUI tools allow writing little code and achieving nigh levels of functionality. Those tools also automatically generate code (which shouldn’t be considered in the metric)
7.       Problems with Multiple Languages: today’s software is developed in multi-language scenarios, hence it is difficult to attribute productivity and defect rates on them due its integration.
8.       Lack of Counting Standard: no standard of what a line of code is (comments? Statements? Multi-line Statements, etc.)
9.       Psychology: programmers get an incentive to write unnecessarily verbose code if measured by lines of code.

Measure programming productivity or effort is not really the objective of this post. What I wanted to show you is; how programming languages compare between them based on the number of lines you need to code to achieve exactly the same piece of code (OO Hello World), following exactly the same rules (same program structure, language standards conventions), and using the same features (same paradigm, no idiomatic code). Said that, let’s see the numbers.

Version 1 (Minimal)

Language Lines
F# 7
Scala 8
Boo 10
IronPython 10
Jython 10
IronRuby 13
JavaFX 13
Groovy 13
JRuby 13
JScript 13
Cobra 15
VB.NET 17
Nemerle 18
C++/CLI 20
Java 20
Zonnon 21
Fantom 22
C# 24
Phalanger 24
Delphi Prism 39


Version 2 (Verbose)

Language Lines
F# 12
Boo 14
IronPython 15
Groovy 15
Jython 15
Scala 15
IronRuby 17
JRuby 17
JavaFX 18
Cobra 18
VB.NET 20
Java 21
Zonnon 22
Fantom 22
JScript 22
C++/CLI 24
C# 26
Nemerle 26
Phalanger 29
Delphi Prism 40


Both (Minimal and Verbose) 


Language Minimal Verbose
C# 24 26
VB.NET 17 20
C++/CLI 20 24
F# 7 12
Boo 10 14
Phalanger 24 29
IronPython 10 15
IronRuby 13 17
Delphi Prism 39 40
Zonnon 21 22
Nemerle 18 26
Groovy 13 15
Java 20 21
Cobra 15 18
Jython 10 15
JRuby 13 17
Fantom 22 22
JScript 13 22
Scala 8 15
JavaFX 13 18



THE CONCLUSIONS

How many lines of code you write is mostly guided by programmer's personal preferences unless some guidelines specify the contrary.

In this comparison, the functional languages F# and Scala are the ones with less lines of code, followed by the Scripting languages such as: Python, Ruby, Groovy and JScript.

Languages that support scripting capabilities are normally shorter than those that don't since no main method/class and extra blocks are required.

Programming languages using opening and closing curly brace { } as blocks usually requires more lines of code (or at least in my example) since the closing bracket counts as a new lines. You can see this between Java and C#:

http://carlosqt.blogspot.com/2010/06/oo-hello-world-c.html
http://carlosqt.blogspot.com/2010/07/oo-hello-world-java.html

Delphi Prism is again the one at the top of the list , as you can see both, the minimal and verbose versions of the code, differ only by 1 line. No doubt Delphi Prism is the most Verbose programming language in Keywords and Lines (of the 19 languages I'm comparing). Definitively, the separation of implementation and interface is what makes the difference! Just compare it with Zonnon and you will see what I mean.

http://carlosqt.blogspot.com/2010/06/oo-hello-world-delphi-prism.html
http://carlosqt.blogspot.com/2010/06/oo-hello-world-zonnon.html

VB.NET smaller than C#? Yes, unless opening curly brace { starts at the beginning block line.

http://carlosqt.blogspot.com/2010/06/oo-hello-world-c.html
http://carlosqt.blogspot.com/2010/06/oo-hello-world-vbnet.html


Bytes!

No comments:

Post a Comment