2

I am currently reading up on Ruby. I think it is a nice language, but I am a bit bothered by having so many equivalent ways, that are only slightly different in syntax, for coding the same action. For example, the unless conditional statement, which is fully equivalent to writing if !conditional.

To me, this does not add any expressive power, just makes it more taxing to follow other people's code. Is there a benefit that I am missing (other than catering to different tastes, which I don't find convincing, since people don't usually reject a language because some syntactic keywords didn't match their taste)?

There are more examples of this in probably every language. I am only using this example because I though it particularly lacking a reasonable defense.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Anas Elghafari
  • 1,062
  • 1
  • 10
  • 20
  • 1
    This has bothered me, too. I hope I get to see a good rationale of TIMTOWTDI, because those I heard so far are pretty much "I like it this way" and "Higher propability things exist under the name you expect, rather than only the one the language designer liked". –  Feb 13 '11 at 09:13
  • "the unless conditional statement, which is fully equivalent to writing if !conditional." This complaint should be levied against Perl first. TIMTOWTDI originated with Perl – the Tin Man Feb 13 '11 at 10:03
  • This is not really a languag flame war. I tagged the question Ruby, because I am encoutering this sytactic redundancy in Ruby as I learn it. I am not currently learning Perl. In any case, what does it matter where it started? Many languages have syntactic redundancies, and I am wondering what the benefit is? – Anas Elghafari Feb 13 '11 at 11:54
  • While I agree on the most part with your observation (see my own Ruby pet hate list: http://stackoverflow.com/questions/3299002/why-ruby-has-so-many-redundancies), I often find `unless` really helpful. It takes some time to quit C-ish mindset though. – Mladen Jablanović Feb 13 '11 at 13:56

4 Answers4

3

Catering to different tastes is the likely explanation. This philosophy is adapted from Perl and it’s known as TIMTOWTDI (There's more than one way to do it).

This does have advantages, for example it facilitates the creation of rich domain-specific languages within Ruby, since existing syntax constructs can be combined in new, interesting ways.

But this technique also has many detractors, and Python in particular strives for the opposite:

There should be one-- and preferably only one --obvious way to do it.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
1

Perl also has an 'unless' construct, and I believe it stems from Larry Wall's background as a linguist.

Branching constructs are easier to understand if we can mentally determine the semantics quickly, for example, take a double negative like this:

  if (!$isDisabled)
  {

  }

causes my feeble brain to skip a beat "so, if not disabled...wait....right, so if enabled?"

unless can make this a little more like natural language

  unless($isDisabled)
  {

  }
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • 1
    If you can't mentally strip a double negative, you have no business using booleans ;) Also, they can be avoided by always using the positive form (`$enabled`) as the variable name - good advice I've seen here on SO a while ago, avoids double negatives unless you're writing large conditionals (and those can propably be deMorgan'd). –  Feb 13 '11 at 09:16
  • 6
    you're not wrong, and I too never fail to avoid not using negative inversions of logic I didn't not want to be never misunderstood. – Paul Dixon Feb 13 '11 at 09:20
  • 3
    Well, my background is partly linguistics as well, yet I have to admit that I find myself translating the `unless` to `if` when I am trying to understand code. I think it's because I am trying to figure out the triggering condition, rather than non-triggering condition. Isn't this what people usually do (i.e. try to understand conditional statements in terms of when they are triggered)? – Anas Elghafari Feb 13 '11 at 09:25
1

Different syntactic variations can be more convenient and easy to read at different times. It is a useful tool for the developer to be able to choose the most readable and obvious syntax for a particular task.

This can be shown for your unless example:

With a simple boolean expression there is no real advantage of using unless or if, although there may be a personal preference depending on how good a particular developer thinks it looks:

if !my_bool_var
    #
end

unless my_bool_var
    #
end

These are equally easy to read (and write).

However, with more complex boolean expressions, the if form has disadvantages:

if !((a + b + c) < (x-a)*b && my_bool_var || ((x - a)*b)/(c+1) > 2)
    #
end

unless (a + b + c) < (x-a)*b && my_bool_var || ((x - a)*b)/(c+1) > 2
    #
end

Here, the unless form is a bit easier to read because we know for sure that the not applies to the whole expression, whereas with the if form, we must visually match the brackets, which can be hard in such complex expressions, although syntax highlighting in editors helps this somewhat.

david4dev
  • 4,854
  • 2
  • 28
  • 35
0

In general, it is something normal to provide developers with many ways to achieve the same thing, and of course it would be some kind of developers preference to choose a way over another. But you have to know that a smart compiler would convert all the ways to the same result when it converts code to its binary executive format.

Ken D
  • 5,880
  • 2
  • 36
  • 58
  • Is this missing the question or am I? I see words such as "expressive power" in the question, not "PERFORMANCE!!!1!". –  Feb 13 '11 at 09:14
  • 1
    Your answer is basically "it doesn't matter because the compiler spits out the same code" (which has two advantages, I imagine: equal performance for all options and simplicity of the output code in case it runs on a VM). OP asked about a rationale to have these redundancies in the input language in the first place. –  Feb 13 '11 at 09:19