views:

27

answers:

2

May I ask should the error catching code be placed at the lowest level or at the top as I am not sure what is the best practice? I prefer placing at the bottom, example a, as

Example a

   public static void Main(string[] args)
    {
        string operation = args[0];
        int value = Convert.ToInt32(args[1]);
        if (operation == "date")
        {
            DoDate(value);
        }
        else if (operation == "month")
        {
            DoMonth(value);
        }
    }
    public static void DoMonth(int month)
    {
        if (month < 1 || month > 12)
        {
            throw new Exception("");
        }
    }
    public static void DoDate(int date)
    {
        if (date < 1 || date > 31)
        {
            throw new Exception("");
        }
    }

or example b

   public static void Main(string[] args)
    {
        string operation = args[0];
        int value = Convert.ToInt32(args[1]);
        if (operation == "date" && (date < 1 || date > 12))
        {
            throw new Exception("");
        }
        else if (operation == "month" && (month < 1 || month > 31))
        {
            throw new Exception("");
        }
        if (operation == "date")
        {
            DoDate(value);
        }
        else if (operation == "month")
        {
            DoMonth(value);
        }
    }
    public static void DoMonth(int month)
    {
    }
    public static void DoDate(int date)
    {
    }
A: 

there is a concept that will help you understand the consequences for each of your example

separation of concerns

http://en.wikipedia.org/wiki/Separation_of_concerns

Alex Pacurar
A: 

It depends. The technical validation of input should be done in front of the consuming methode. Technical validation means things like: Are there enough parameter? Perhaps: Do they have the right "type"?

The information validation should be done right before the input is interpreted. So you are conform with the OO principles.

DHN