2010-02-18

To compare, or not to compare? That is a question for you

Since I don't want to sleep yet, but don't have anything to do either, I thought I might as well start with the new, totally random single-topic approach to the Vala Journal.

A while ago (in the first attempt on this journal thingy to be precise), I noted a proposed feature of "complex conditionals". Since there hasn't been much response, it may be better to dedicate a post to it.

It all boils down to a single new added possibility - chain more relational operations in a single expression. Example: if you need to compare a variable to two values, you currently need to do this:

if (1 < a && a < 5) {}

Well, that doesn't look too bad, you're right. What about this?

if (0 < a && a < b && b < c && c < d && d < 255) {
    // do something
} else {
    error ("Invalid data");
}

I don't know about you, but I think this is just evil.
Now, with the complex conditionals thingy, this would be possible:

if (1 < a < 5) {}

if (0 < a < b < c < d < 255) {
    // do something
} else {
    error ("Invalid data");
}

Muuuuuch nicer. :)

However, Jürg suggested that there are more possible approaches to this problem. For example a range syntax:

if (a in [1..5]) {}

I don't find this possibility particularly bad, but it wouldn't allow any fancier chaining, like in my second example.

So, questions for you: What do you thing about it? Which one do you prefer? Do you have any other suggestions?

EDIT:
Ahh, forgot to link the bug report.
https://bugzilla.gnome.org/show_bug.cgi?id=606480

10 comments:

  1. By the way.. As most of you probably figured out, I'm not a native English speaker. If there happens to be any grammar nazi in the crowd (or at least someone who knows English well), I'd appreciate if you pointed out any grammar or stylistic mistakes I make. Thank you :)

    ReplyDelete
  2. I really like the first option (1 < a < 5), but also the last one could be useful in this form,

    if (a in [1..5,7,10..16]) {}

    ReplyDelete
  3. It's a false dilemma. I'd say both features would be worth considering.

    ReplyDelete
  4. Any chance to get an rss feed of this blog?

    ReplyDelete
  5. @Felix: This blog has both Atom and RSS feeds:

    http://valajournal.blogspot.com/feeds/posts/default

    http://valajournal.blogspot.com/feeds/posts/default?alt=rss

    ReplyDelete
  6. 1 < a < 15 is a winner: it looks exactly like unambiguous existing mathematical notation, it works well with variables while still being totally clear: a >= b > c.

    a in [1..5] is probably clear enough for most people, but its not a widespread existing notation like normal mathematical infix notation is in the above.

    a in [b..c] means. . .?

    The in operator sees some pretty good use in python, but its for collection membership, and is a very wasteful approach when used like
    a in range(1,5)
    , so this doesn't really need any special language facilitation in Vala in the form of a in [1..5] if an "in" keyword could see some better action as a reusable operator for collection-like objects or something.

    Finally, language feature orthogonality is a good thing, nothing is better than reading someone else's code and knowing exactly what it does: for goodness sake don't just implement every possible alternative.

    ReplyDelete
  7. Chained expressions added as experimental feature in latest 0.8.0 vala release :)

    ReplyDelete