The question I try to answer is: Should there be several different ways of saying the same thing in a given language, or just few? Or maybe just one? It depends on the details of the specific situation of course. But my general belief is that it is BAD to have multiple ways, if there's no good reasons for that. I will try to illustrate this point with examples from Groovy.
According to http://docs.groovy-lang.org/latest/html/documentation/index.html#_closures you can write this in Groovy:
def code = { 123 }
assert code() == 123
assert code.call() == 123
In other words you can evaluate a closure by placing "()" after it, or by placing ".call()" after it. My question: Do we need two (or more) different ways of doing that? How does it help?
One way it could help is that the first way is shorter, and shorter is good, right? But then why do we need the second way? Maybe there is a valid reason in this particular case and if so then this is clearly a good thing. But if there's no good reason for multiple different ways of doing the exact same thing, it is bad.
It is like you had two break-pedals in your car, just in case. That might make you feel safer, but in fact the multitude of pedals would get you confused. You'd end up pressing the gas-pedal to stop the car. Or it's like driving a bicycle. You can drive in a normal way like most people do. Or you can lift your hands up in the air and shout "Look Mom, no hands!". So there are two different ways of driving a bicycle. But is that a good thing? I think it's clearly safer if you never even knew about that second way.
Second example: Calling a Groovy method that takes a closure as its last argument. It can be done in (at least?) three different ways. Let's first define the method:
def myMethod (aString, aClosure) {
aClosure(aString)
}
We can now pass a closure to it as an argument in at least three different ways, which all have the same effect:
1.
myMethod ('madness', {arg -> println arg})
Above two arguments are passed to myMethod() separated by comma, like you do in most other programming languages. But in Groovy the above can also be written like this:
2.
myMethod ('madness')
{ arg -> println arg
}
{ arg -> println arg
}
That works because IF the last argument is a closure, it can be placed outside the parenthesis. You can omit the comma then too. Clever yes. But is that enough of a reason to have this 2nd way, when the first example already works fine, and works like most other programming languages, without needing clever rules about the "type of the last argument"?
3.
myMethod 'madness',
{ arg -> println arg
}
{ arg -> println arg
}
Above shows you can call a method without ANY parenthesis at all. But then, you must put the comma back in. Clever? Maybe too clever.
// equivalent to: turn(left).then(right)
turn left then right
That saves us four parenthesis and looks truly impressively clever. From the same document we can learn the rule "If your command chain contains an odd number of elements, the chain will be composed of method / arguments, and will finish by a final property access".
In the same document there are many other examples of clever ways of writing one thing in different ways. They are intended to show how you can use Groovy to create Domain Specific Languages. But by now I think I'd prefer a simple general purpose language instead, without myriad rules about how statements can mean different things based on whether you have even or odd number of elements.
So let's get back to why having many different ways of writing the same thing is bad. You could say it doesn't matter because you don't need to learn them all, just learn one and use that. But you do need to learn them all if you want to read code written by someone else. And often being able to read code is as important as being able to write it.
Multiple different ways of doing things are bad because those multiple ways are different in each programming language. It's as if every car-make would have a completely different type of dashboard and set of controls, the pedals in different order etc. That would be dangerous, right? Cars are powerful machines that can get people killed in accidents. Programming languages are even more powerful and dangerous, they run nuclear plants! We should strive to make them less dangerous, while still keeping them powerful.
I do like Groovy the language. Its one flaw for me is that it tries to be a language for creating Domain Specific Languages, but doesn't quite get there either. If I really want my own domain to have its own language I think I'll use Xtext for that.
Groovy probably isn't the worst offender in its many ways of doing the same thing. Maybe Perl is. Here's an example of FIVE different ways you can iterate through a list in Perl: http://www.wellho.net/resources/ex.php?item=p208/back. To be able to read Perl -code you have to learn them all.
© 2015 Panu Viljamaa. All rights reserved