Jaa


C#: How about borrowing some Ruby conditional expressions

After using Ruby for some time I have become quiet attached to its conditional statement syntax. In particular I like statement modifiers which allow you to tag conditional statements at the end of a normal statement.

If C# supported this then in C# terms this'd mean instead of writing

 if (!File.Exist(fileName)) throw new FileNotExistException(fileName));

You could have written

 throw new FileNotExistException(fileName)) if (!File.Exist(fileName));

If things go as it seems this could actually be the future of C#.  I know this does not look that intuitive at first, but once I got used to it I started using it at all places. A quick glance in my Ruby sources reveals code like

 Utility.showHelp if ARGV.length < 2
ARGV.each do |srcFileName|
    next if srcFileName =~ /^-/ #skip arguments starting with a -
    ...
end

I liked the unless keyword as well. This is a negated version of if used in Ruby. If C# supported this then we could have written

 unless (File.Exist(fileName)) throw new FileNotExistException(fileName)); 

or better still

 throw new FileNotExistException(fileName) unless (File.Exist(fileName)); 

What I felt was not that intuitive is that in Ruby if is not a statment but is an expression similar to the ?: conditional expression in C/C++/C#. So for conditional assignment of values in Ruby you can write code as in

 ageGroup = if age < 2
             "Infant"
           elsif age < 19
             "Teen"
           elsif age < 30
             "Middle aged"
           else
             "old"
           end 

In C# this'd become

 string ageGroup; if (age < 2)    ageGroup =  "Infant"; else if (age < 19)     ageGroup =  "Teen"; else if (age < 30)     ageGroup =  "Middle aged"; else     ageGroup =  "old"; 

Comments

  • Anonymous
    October 09, 2005
    its conditional statement syntax

    borrowed from Perl, actually. The good thing about this syntax is that it enables you to put the active code up front instead of hiding it behind an IF statement. In other words, it's better readable.
  • Anonymous
    October 09, 2005
    A spin on the last one..

    var ageGroup = if { (age < 2) return "Infant";
    else if (age < 19) return "Teen";
    else if (age < 30) return "Middle aged";
    else return "old"; }

    (I hope the formatting is not messed up)
  • Anonymous
    October 09, 2005
    Considering that Perl has been around before Ruby, I guess that the credits for these kind of conditional statements belong to Perl.
    Although as far as readability is concerned I don't think that it is an improvement. You always have to scan the whole statement until the end in order determine in which cases it will be executed. If the condition is in front you can decide much earlier.
  • Anonymous
    October 09, 2005
    Absolutely these are inherited from Perl. I have looked into Perl but never did any serious coding in it. I meant that I got used to the syntax from Ruby and not that these are original Ruby features.

    As wkasdo has put and also IMO its more readable (I know its subjective) if the active statement comes up front and is not hidden behind some if.
  • Anonymous
    October 09, 2005
    The comment has been removed
  • Anonymous
    October 10, 2005
    I've done a bit of coding in Perl, and readability in that language is a real problem. While it gives you good expressiveness to be able to state things 'n' ways, this also contributes to readability because a reader of the code needs to be able to digest all the different forms of 'if' and stop to figure out what is going on. If there is basically the "main" way of doing it, the code is simpler, IMO.
  • Anonymous
    October 10, 2005
    Now zzz is getting me out of sleep. So what about using Lambda expressions from C#3.0 (
    http://blogs.msdn.com/abhinaba/archive/2005/09/17/469568.aspx)

    Func<int> lambda =

    age => { if (age < 2) return "Infant";
    else if (age < 19) return "Teen";
    else if (age < 30) return "Middle aged";
    else return "old"; };

    var ageGroup = lambda(30);
  • Anonymous
    October 10, 2005
    The comment has been removed
  • Anonymous
    October 10, 2005
    Yes C/C++/C# has ?: I highlighted that as in
    "What I felt was not that intuitive is that in Ruby if is not a statment but is an expression similar to the ?: conditional expression in C/C++/C#"
  • Anonymous
    November 06, 2014
    I also like to see some popular Ruby features to be supported in C#.