views:

116

answers:

8

I am on a weird kick of seeing how few lines I can make my code. Is there a way to condense this to inline case statements?

    switch (FIZZBUZZ)
    {
      case "Fizz":
        {
          //Do one process
          break;
        }
      case "Buzz":
        {
          //Do one process
          break;
        }
      case "FizzBuzz":
        {
          //Do one process
          break;
        }
    }

to look something like this:

    switch (FIZZBUZZ)
    {
      case "Fizz": //Do one process
      case "Buzz": //Do one process
      case "FizzBuzz": //Do one process
    }
+5  A: 

If you want to condense things you could just put things on one line (let's imagine that "do one process is a call to Console.WriteLine):

switch (FIZZBUZZ)
{
    case "Fizz": Console.WriteLine("Fizz"); break;
    case "Buzz": Console.WriteLine("Buzz"); break;
    case "FizzBuzz": Console.WriteLine("FizzBuzz"); break;
}

If you want to get fancy you could create a map of strings to actions like this:

var map = new Dictionary<String, Action>
{
    { "Fizz", () => Console.WriteLine("Fizz") },
    { "Buzz", () => Console.WriteLine("Fizz") },
    { "FizzBuzz", () => Console.WriteLine("FizzBuzz") }
};

And then you could invoke the method like this:

map[FIZZBUZZ].Invoke(); // or this: map[FIZZBUZZ]();
Andrew Hare
Well but I have "handleFizz" and "handleBuzz" for the two calls. and then call them both for fizzbuzz. I would be repeating code this way. As much as I like to condense, I like efficiency. :)
Jim
@Jim - I like correctness, readability, and maintainability. I am glad that in the fizzbuzz example at least, our interests may coincide. :-)
Jeffrey L Whitledge
+1  A: 

You always have to have a break statement in order to leave the switch other than that you can do it as you mention

  switch (FIZZBUZZ)
    {
      case "Fizz": /*Do one process*/break;
      case "Buzz": /*Do one process*/break;
      case "FizzBuzz": /*Do one process*/break;
    }
Alex Krupnov
Beat me to it! :)
Paul Hadfield
A: 

Well an easy way would be:

switch (FIZZBUZZ)
{
     case "Fizz": Console.WriteLine("Fizz"); break;
     case "Buzz": Console.WriteLine("Buzz"); break;
     case "FizzBuzz": Console.WriteLine("FizzBuzz"); break;
}

Which is only one line each. But there's multiple statements per line...

ho1
A: 

Well if you're really interested in fewest lines of code you can write:

switch (FIZZBUZZ) { case "Fizz": /* Do one process */ break; case "Buzz": /* Do one process */ break; case "FizzBuzz": /* Do one process */ break; }

I wouldn't recommend it though.

It's hard to tell exactly what you're asking though - are you trying to fall through between cases, or just remove braces?

Jon Skeet
@Jon Skeet - I actually did that to a coworker who irritated me once. Wrote an entire class on a single line.
Joel Etherton
I am trying to remove braces.
Jim
@Jim: other than the braces around the switch block itself, you don't *need* the other braces, so just remove them. Though if you're creating variables in any of those cases, it is suggested to have them to ensure scope safety.
Jimmy Hoffa
A: 

I don't know of any way to do this other than the obvious:

switch (FIZZBUZZ) 
    { 
      case "Fizz": { //Do one process } break;
      case "Buzz": { //Do one process  } break;
      case "FizzBuzz": { //Do one process  } break;
    }
Bill W
A: 

You can put as much code on one line as you like with C#.

As Andrew says.

Personally my preference is to leave white space as it allows easier reading of the code but then again I am the only dev here who comments his code or writes methods and functions small enough to be able to quickly scan the code to see exactly what it does. :)

Peter
A: 

You don't need the curly braces for the case statements, but do you need a break statement for each case. Otherwise, you can't really do much

Duracell
A: 

With the assumption that this is purely esoteric and that you will not be tempted to use this in a production system, you could abuse expression trees:

FIZZBUZZ.Switch(Fizz => DoSomething(),
                Buzz => DoSomethingElse(),
                FizzBuzz => DoSomethingElseStill());

Where Switch is an extension method:

public static void Switch(this string @this, params Expression<Action>[] cases)
{
    Expression<Action> matchingAction = cases.SingleOrDefault(@case => @case.Parameters[0].Name == @this);
    if (matchingAction == null) return; // no matching action

    matchingAction.Compile()();
}
Paul Ruane