September 6, 2009
Very geek post

I saw the following code in golliewaggles theme:


( public class GollieWaggles(Liquid rum){
    this.liquid = rum;
}

public String addFood(String foodType){
    if(foodType.equals("FRIES_AND_COKE")){
      return "Burp";
    }
    else if(foodType.equals("BROCCOLI")){
      return "Insert fart noise here";
    }
    else(){
      return "Insert more rum";
      }
    }

public void fillFreeTime(int amountOfTime){

  switch (amountOfTime) {
   case 100: System.out.println("Start eclipse");
   case  30: System.out.println("Start nwn HG");
   case  10: System.out.println("Check guild forums");
   default : System.out.println("Stare at wife :)");
  }
} )

Well, I could not resit and give a python version and try to get it as small as possible (I changed the class name :P):


class Mandel:

    def __init__(self, alcohol="rum&coke"):
       self.alcohol = alcohol

def add_food(food):
    try:
        print {"fires_and_code":"burp", 
                "broccoli":"Insert fart noise here"}[food]
    except KeyError:
        print "Insert more rum"

def fill_free_time(int):
    try:
        print {100:"Start eclipse", 
                 30:"Start nwn HG",
                 10:"Check guild forums"}[int]
    except KeyError:
        print "Stare at Bea :)" # she will not understand

If I was being honest I would have not done the if statements with a dictionary and a try and catch exception, but I wanted it to be sorter than the Java one (yes, I’m a cheeky fucker :D). I’d love to see it in other languages, if anyone can be bothered :P