Max
Max

Reputation: 13388

Method in method in method c#

Is it bad to have a lot of methods referring to each other, like the following example?

public void MainMethod()
{
   GetProducts();  
}

public void GetProducts()
{
   var _products = new Products();
   var productlist = _products.GetProductList;
   GetExcelFile(productlist);
}

public void GetExcelFile(List<product> productlist)
{
   var _getExcelFile = new GetExcelFile();
   var excelfile = _getExcelFile.GetExcelFileFromProductList(productlist);

   //create excel file and so on...
}

So I am creating a new method for every little action. It would be just as easy to call GetProducts from the MainMethod and do all actions in that method, but I think that isn't the way to create re-usable code.

The following tells me that there should be no more than 7 statements in a method:

Only 7 statements in a method

So the advantages of using methods with a minimal amount of code:

The disadvantages of using methods with a minimal amount of code:

My question:

Should my methods be bigger, or should I keep creating small methods, that do little and refer to a lot of other methods?

Upvotes: 2

Views: 145

Answers (4)

Dennisch
Dennisch

Reputation: 7553

I don't really agree with '7 statements in a method'. A method can have dozens of statements, as long as the method performs a single function and the specific logic will only be used in one place, I don't really see the point of cutting it into parts just because some guyideline says so, it should be seperated based on what makes sense.

Re-use of code is good if it makes sense, but I don't think you should make everything everywhere re-usable, when you have no plans in the near future to actually re-use it. It increases the development time needlessly, it often makes the software more complex and harder to interpret then it needs to be, and (at least in my company) the majority of the code never gets re-used, and even if it is it still needs modifications in new products. Only the most generic parts of our codebase actually gets used in several applications without modifications for each product.

And I think that's fine.

Upvotes: 0

Paolo Falabella
Paolo Falabella

Reputation: 25874

The guideline is generally a good one, not so much for reuse, but for readability of your code.

Your example, though, is not a good one. What you're doing is basically creating a long list of methods where each one stops when you feel it's too long and calls another one to perform the rest of the operations.

I would follow more this kind of approach, where reading the main method tells you the "story" that your code needs to tell by steps and the detail of each step is in smaller methods.

public void MainMethod()
{
   var productlist=GetProducts();  
   string excelfile=GetExcelFile(productlist);

   // do something in the excel file
}

public List<product> GetProducts()
{
   var _products = new Products();
   return _products.GetProductList;
}

public string GetExcelFile(List<product> productlist)
{
   var _getExcelFile = new GetExcelFile();
   var excelfile = _getExcelFile.GetExcelFileFromProductList(productlist);

   return excelfile; 
}

Upvotes: 2

Steven
Steven

Reputation: 172835

The guideline is right. Methods should be small and you are doing the right thing be giving not only each operation its own method, but a well defined name. If those methods have a clear name, one responsibility and a clear intention (and don't forget to separate commands from queries), your code will not be spagetti. On top of that, try to order methods like a news article: most important methods on top of the file, methods with the most detail on the bottom. This way anyone else can start reading at the top and stop reading when they're bored (or have enough information).

I can advice you to get a copy of Robert Martin's Clean Code. There's no one in the industry who describes this more clearly than he does.

Upvotes: 3

No Idea For Name
No Idea For Name

Reputation: 11597

this is mostly opinion based question, but i'll tell you one thing:

if you're not going to use a method from more then one place, it might be better not to create a method for that.

you can use regions for clarity and you might not want a method that is larger then a full page, but not every 2-3 commands should get a method.

Upvotes: -1

Related Questions