开发者

What is the value in having several, equally abstract, syntactic variations for coding something?

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 part开发者_如何学Pythonicularly lacking a reasonable defense.


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.


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)
  {

  }


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜