Skip to content

Instantly share code, notes, and snippets.

@JayBazuzi
Created January 2, 2014 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayBazuzi/8228363 to your computer and use it in GitHub Desktop.
Save JayBazuzi/8228363 to your computer and use it in GitHub Desktop.
Replaced all conditionals with polymorphism
interface I
{
string Get(int i);
}
private class Fizz : I
{
public string Get(int i)
{
return "Fizz";
}
}
class Buzz : I
{
public string Get(int i)
{
return "Buzz";
}
}
class FizzBuzz : I
{
public string Get(int i)
{
return "FizzBuzz";
}
}
class Number : I
{
public string Get(int i)
{
return i.ToString();
}
}
public string GetFizzBuzz(int i)
{
return new I[] {
new FizzBuzz(), //15
new Number(), // 1
new Number(), // 2
new Fizz(), // 3
new Number(), // 4
new Buzz(), // 5
new Fizz(), // 6
new Number(), // 7
new Number(), // 8
new Fizz(), // 9
new Buzz(), //10
new Number(), //11
new Fizz(), //12
new Number(), //13
new Number(), //14
}[i % 15].Get(i);
}
@sukima
Copy link

sukima commented May 11, 2016

Huh, I really like this. I didn't think I would but I do!

I don't think this would be universally applicable to all conditionals (thought I wish it were) considering the new I[] {…}[i % 15].Get(i) is akin to hard coding instead of algorithmicly determining the outcome.

Then again the counter argument is that this is more configuration over computation and allows for more customization. I need to meditate on this 💭

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment