views:

132

answers:

7

Is there some mathematical relation or formula that can estimate a better source code from a less better one?

If I write a program to do exactly the same task using PHP and PERL programming language, for example generating HTML with the text "Hello World!", can these two source code files be evaluated to conclude one is better than the other? The evaluation criteria can be anything. But I am looking for non-naive criteria.

+1  A: 

if the evaluation criteria can be anything then yes, e.g. you could count characters and specify the fewer the better. however not everyone may agree on your/this definition of 'better', the question of which code is better is fundamentally subjective.

jk
+2  A: 

You seem to be answering your own question by saying : The evaluation criteria can be anything.

If your criterion is computable from the source files given as input, then yes you can. Just choose which criterion fits best for your need (number of characters, number of lines, levels of nested blocs, 1 / number of occurences of "goto", etc.).

The difficult part is to find a good combination of metrics and to make a lot of different criteria fit into a single number.

You must narrow the research by telling more about your input. Is it always PHP against PERL source files ? Do they follow a certain pattern, or convention (one class / function / module per file) ? What is the typical complexity of the problems they solve ? Is execution time a criterion (meaning the source files only can't be enough for your evaluation) ?

subtenante
That would be "criterion". :-)
Dr. Tim
Alright, I thought the etymology in english came from latin, not directly from greek.
subtenante
+2  A: 

Some criteria have been defined. See Wikipedia on Software metrics

Pascal Cuoq
+1  A: 

There are lots of different metrics: SLOC, function points, cyclomatic complexity, etc. None of them are perfect, nor do they cover all possible evaluation criteria. In the end, they can only guide you in making an evaluation but aren't definitive. This is more true the more complex the code gets. The only thing I can say with any surety is that your code, any code, will seem worse in 6 months that it seems today.

tvanfosson
+1  A: 

The core of your problem isn't whether it's possible to do this (it obviously is). The real question is: what are good criteria? It's obviously very dependent on the situation, and highly subjective.

Wouter Lievens
+1  A: 

Based on your question, it sounds like you want to compare two source files to find out which programming language does the job better. But that's a highly subjective question that probably can't be answered by mathematics.

If you are trying to decide what programming language to use, you should look at the capabilities of the language, see what other people have used to solve similar problems, look at available libraries, and look at what you know (if you know a language, it will be easier to come up with an implementable solution).

Thomas Owens
I'm curious as to the downvote.
Thomas Owens
A: 

Here is my answer, to a similar question, to get an idea of how complex your task is.

Nick D