tags:

views:

304

answers:

2

Hi, I'm trying to learn some C# over the weekend and am following the 15 exercises found here: http://www.jobsnake.com/seek/articles/index.cgi?openarticle&8533

Yesterday I asked a similar question for the Fibonacci sequence and received some great responses which introduced me to elements of C# which I'd not encountered before: http://stackoverflow.com/questions/406446/refactoring-fibonacci-algorithm

Today I would like to see how a C# Jedi would refactor the following code:

static string Reynolds(int d, int v, int rho, int mu)
{
 int number = (d*v*rho) / mu;
 if (number < 2100) return "Laminar Flow";
 else if (number < 2100 && number < 4000) return "Transient Flow";
 else return "Turbulent Flow";
}

So more simple than yesterday, but is there any nice way to deal with the multiple conditionals?

Regards,

Chris

+3  A: 

Using the C# ternary operator,

   static string Reynolds(int d, int v, int rho, int mu)
    {        
       int number = (d*v*rho) / mu; 
       return number <  2100? "Laminar Flow":
              number >= 4000? "Turbulent Flow":
                              "Transient Flow";
    }

or, (to give my sarcastic side a bit of free rein), if you find this not readable, you could add comments, to make it more readable,

static string Reynolds(int d, int v, int rho, int mu)
{        
   int number = (d*v*rho) / mu; 
   return /*IF*/ number <  2100? /*Then*/ "Laminar Flow":
          /*IF*/ number >= 4000? /*Then*/ "Turbulent Flow":
                                 /*Else*/ "Transient Flow";
}

Does anyone really think that helps?

Charles Bretana
This is compact but not very readable. Although, he didn't ask for that. :)
BobbyShaftoe
Ick ick ick. Chained ternaries == the devil.
Juliet
I think this kind of code should not be written.
Cyril Gupta
This is just the kind of functional programming example one sees in OCaml or Haskell. Why should it not be written in C#?
Justice
funny I think it's much more readable than multiple nested if else clauses...
Charles Bretana
These seems more readable to me, and I don't know C#
Technical Bard
I don't think the comments make it more readable. It's not a bad solution. It's a style preference. I prefer to have the nested ifs, sometimes I may decide I want to add extra code. I use ternaries of course, I rarely use nested ternaries.
BobbyShaftoe
+5  A: 

I think there is an error in your code but I made an assumption. Your second "if" would never evaluate tree as number < 2000 already makes the first branch true.

I would create an ENUM:

enum FlowType
{
    Laminar
    , Transient
    , Turbulent
};


static FlowType Reynolds(int d, int v, int rho, int mu)
{
    int n = (d*v*rho) / mu;

    if(n < 2000)
    {
        return FlowType.Laminar;
    }
    else if(n < 4000)
    {
        return FlowType.Transient;
    }
    else
    {
        return FlowType.Turbulent;
    }
}
BobbyShaftoe
The "n >= 2000" is redundant, you can omit it without changing the semantics of the code.
Juliet
As coded, you need to change the return type. You coded the method as if you are returning values of type FlowType, but the method signature states that you are returning strings.Also, the test n >= 2000 is not necessary.
Jason
I was in between edits. Also, as for the unncessary >= 2000 check, the problem was there was an error in the original and had forgot to remove that when correcting the original.
BobbyShaftoe