{ by david linsin }

June 16, 2008

Less Code == Less Errors

One of the most prominent advantages of so called "dynamic languages" is their concise syntax and thus their savings in terms of code. With less code, there should be less space for errors, right? After all, they say "the best code is no code at all".

Probably the first thing that comes into your mind, when you read concise syntax is readability. There are people in favor of concise syntax, arguing that it is more or at least as readable as verbose syntax. There are also other opinions. I won't go into the whole readability-maintainability discussion. I'm still torn between favoring code of languages with concise syntax, like e.g. Scala, or more verbose code of good old Java.

"Dynamic languages" have a reputation of producing less code due to their concise syntax. Take a look at this example which compares a code sample of Java and Scala (note that Scala is not a dynamically typed language):

// Java
List vals = new ArrayList();
...
for (String elem : vals) {
doSomething(elem);
}

// Scala
def vals = List("some", "test", "vals")
vals.foreach(elem => doSomething(elem))

You can see that Java's verboseness is eating up a lot of characters. Scala on the other hand, can save a lot of keystrokes by dropping type declarations in favor of type inference.

Another thing that you probably notice is that the imperative step-by-step style of Java's syntax is rather self-explaining. I don't need to tell you what happens in this little code snippet, because the code already does. Scala on the other hand with its functional style, which is more concise, but in my opinion also more complex. I'm aware of the different concepts here and I'm a little reluctant comparing imperative to functional syntax. However, I think it makes a good comparison.

If you are used to the Scala syntax, you'll probably won't have any problems understanding it. Nevertheless, I think that concise code and thus less code, doesn't eliminate space for errors. In my opinion more conciseness means more abstraction, which is actually a good thing, but I believe it can also increases complexity. We all know that we can only grasp so much. I think that in a lot of cases verboseness, in terms of more code (e.g. the imperative step-by-step approach, reduces the space for errors, because you are more explicit about what your code is supposed to do.

12 comments:

sarbogast said...

I totally agree with you. And your example would have been even clearer if you had compared Java to Ruby for the same snippet.

Plus, there are other ways to write less code without sacrificing maintainability or readability.

david said...

sarbogast > And your example would have been even clearer if you had compared Java to Ruby for the same snippet. Plus, there are other ways to write less code without sacrificing maintainability or readability.


Unfortunately I don't know Ruby, so I cannot really comment on that. Feel free to share some examples ( you can drop me an email or post them here)

Daniel said...

Just to be clear, Scala isn't exactly a dynamic language, it just has a far more concise syntax than Java. :-) As for Ruby...

vals = ['some', 'test', 'vals']
vals.each do |e|
do_something e
end

Alternatively...

vals.each { |e| do_something e }

Daniel said...

Oh, and you can also write the Scala example more concisely in the following way:

val vals = List("some", "test", "vals")
vals.foreach(doSomething)

david said...

Daniel > Scala isn't exactly a dynamic language

You are absolutely right Daniel, thanks for pointing that out! That's actually why I put "dynamic languages" in quotes. It's not correct though...I guess it just feels dynamic due to type inference..

Thanks for the Ruby example and the correction of my scala script.

Matt said...

Interesting, but I'm not sure that you chose the best example for your comparison. Java's enhanced "for" construct was added to make iteration less verbose by hiding all the details of iterator manipulation that you had to go through pre Java 5. In fact, arguably, Java's enhanced "for" loop is really a functional programming construct comparable to foreach in Scala -- it's just that in Java it was hardcoded into the language.

Perhaps a more telling example is something like the "map" or "filter" methods that pop up in languages like Scala, and for which Java provides no special syntax. The comparable Java code to do the same thing ends up not only more verbose, but also more complex, as you get bogged down with the details of copying across elements into a new list.

Maybe it's a matter of taste, but for me, the functional style is not just more concise, but expresses the programming task in a clearer way, cutting out a fair amount of the low-level noise.

Rudi said...

In my view 'conciseness' (or 'expressivity' as I prefer to call it) has nothing whatsoever to do with 'dynamicness'! There are languages that are strongly typed and do implement such higher order patterns as iteration, mapping, currying while being very strict at typing. Scala was already mentioned here. Caml (or OCaml as it is called nowadays) also fits the bill. (Note that Caml is strictly type while not burdening the programmer with type declarations all the time)
Fact is that more strictly typed languages lend themselves better for compiler optimization.

André said...

Similar example in Python:

values = ['a', 'b', 'c', 'd']

for val in values:
___ doSomething(val)

More concise; more readable.

(I have had to replace some leading spaces by "_" because of blogger's handling of comments.)

Stephan said...

You're right, less code, less errors.

But you've got it wrong. Less code doesn't mean less LOC but less CC or thinking points. With the same amount of thinking points in your code, you'll have the same amount of errors.

Beside that, an apply(list, new Applyable() { ... }); in Java would solve the same problem.

I'd be interested in real examples, where people who understand Java compare it to other languages. Not people with only superficial Java knowledge and bad programming style.

Peace
-stephan

Stephan said...

List<String> vals = List("some", "test", "vals");

is proper Java code too, with

public List<T> List(T... args) {
return Arrays.asList(args);
}

See the Google collections library for more examples.

Peace
-stephan

dysinger said...

more verbose boiler plate non-functional code does NOT decrease errors. You are on the loosing side of that argument.

david said...

Matt > ...I'm not sure that you chose the best example for your comparison..Java's enhanced "for" loop is really a functional programming construct comparable to foreach in Scala -- it's just that in Java it was hardcoded into the language.

You are right, maybe my example was too trivial. Using Scala's filter or foldLeft would have made the point much clearer. On the other hand I wanted to keep it easy and make an obvious comparison. I was reluctant whether to compare something like Scala and Java, but I still think that my example makes the point clear.

Rudi > In my view 'conciseness' (or 'expressivity' as I prefer to call it) has nothing whatsoever to do with 'dynamicness'!

I totally concur, I misused the term "dynamic language" as a synonym for "new language". For me those "new kids on the block" are Groovy, Ruby or Scala.

Stephan > You're right, less code, less errors.

If you read my blog post carefully you'll see that my conclusion is the opposite.

Stephan > I'd be interested in real examples, where people who understand Java compare it to other languages.

I strongly recommend Daniel's blog, he writes in depth analysis of various languages and their different constructs.


com_channels

  • mail(dlinsin@gmail.com)
  • jabber(dlinsin@gmail.com)
  • skype(dlinsin)

recent_postings

loading...