Question

Sometimes I have to write big code in a class, so what I do is something like this,

Class ABC   //it's a web service class
{
   Public void Method-1() //used "-" for easy to read
   {
       //DoSomething and get something from database
       Method-2(pass parameters that i got from database);  
   }

   Public void Method-2(parameters)
   {
       DoSomething again and get data from another database. and some other source
       do some processing by calling web services (just as example)
       Method-3(parameter);
   }

   Public void Method-3(parameters)
   {
       DoSomething again and get data from another database. and some other source
       do some processing by calling web services (just as example)
       Method-4(parameter);
   }

   // and it keeps going
}

Another way

Class ABC   //it's a web service class
{
   Public void Method-1() //used "-" for easy to read
   {
       Method-2();
       Method-3();
       Method-4();
       // so on....
    }
}

Is this the right way of doing it and if not then what would be best way of doing it ?

Edit

@Sayse I am trying to get information from different sources and trying to build a big XML file which made me get use 4, 5 foreach loops to get data from sql etc.. so using nested methods

Was it helpful?

Solution

Both ways are good in different cases. If you have single functionalities, you should keep them separate. Second approach - calling method from method should be used when one method is part of 'outer' functionality.

Examples:

repairVehicles() {
    repairCar();
    repairBike();
    repairTrain();
}

... but:

repairCar() {
    ...
    repairEngine();
    ...
}

repairEngine() {
    ...
    takeEngineOut();
    ....
}

takeEngineOut() {
    ...
    unscrewBolts();
    ...
}

OTHER TIPS

There cannot be a straight forward answer to your question.

First of all you should note that one method should perform one functionality. If it is true, then you can call it either way depending on your requirement.

Example:

If you have a base method takes a mathematical expression as input. And that expression contains Add, Subtract, Multiply and divide then you will call it the first way.

public int GetExpressionResult(string someExpression)
   {
         Divide();
         Multiply();
         Addition();
         Subtraction();
        return result;
   }

in the above example the result is dependant on all four methods, so it is fine to call it like this.

now in your example 2 if the methods are totally independant of each other than you should the way you have done.

Conclusion:

There is no hard and fast rule for this, You should call the way your application demands.

As far as I understood your question, what you are describing is basically a pipeline. There is a very interesting blog (in two parts here and here) about how to elegantly tackle situations as yours.

At the end, it depends on what you're trying to do and applies, IMHO, not only to C#.

Your first option should be applied when method<i+1> is a helper for method<i>, or is included in it. I can't find an example for such a scenario.
Your second example, which is far more readable to me, should be applied when you have a long sequence of actions that need to take place. Let say:

void mainMethod()
{
    ConnectToDB(); //if can't connect, log it and exit
    GetUserInfo(...); //if null, log it and exit
    ShowUserInfo(...); 
}

In the example above, it's hard (for me) to imagine a division to methods like in your first scenario.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top