Reputation: 323
Essentially what the title says. What advantages and/or disadvantages are there to having multiple small methods in a c# program? Does it slow things down? Does it cause issues? Is it just bad practice?
Upvotes: 1
Views: 4245
Reputation: 3851
This is a very open ended question, in some programs its a good thing in others it would be bad.
The two principles you should be keeping in mind are
DRY - Don't Repeat Yourself
KISS - Keep It Simple Stupid.
In general each method should contain one discrete piece of functionality and only that functionality, however if the option is there to give it in a general form it should be taken rather than having two methods to accomplish similar tasks.
It should be noted that no ethically-trained software engineer would ever consent to write a DestroyBaghdad procedure. Basic professional ethics would instead require him to write a DestroyCity procedure, to which Baghdad could be given as a parameter. ~Nathaniel S. Borenstein
As for efficiency the compiler doesn't mind which way you write it, good coding style is for the benefit of the people who write and maintain it.
Upvotes: 1
Reputation: 7590
Separating your code in multiple methods allows you to reuse them. It also makes your code a lot more organized.
Calling a method doesn't take a lot of resource so you shouldn't worry about performance issues unless you're developing for very sensible, low resources embedded systems for example.
It's generally considered good practice to have a method per logical responsibility.
Upvotes: 1
Reputation: 5294
If you mean breaking up what would be a large chunk of code into multiple smaller methods I would say it is actually good practice.
It aids in making your code reusable in some cases, it also means the code is more manageable and easier to update in the future.
for instance if you had one method that would do something like: Connect to a database execute a query process some calculations on the results
Breaking that down into multiple smaller methods means that you can reuse your database connection code, the execution of the query is kept sepereate and you can also reuse the code for the calculations.
Upvotes: 0
Reputation: 2334
In general, the smaller methods are more preferable, than bigger. But you don't need to make a separate function for every possible logical step, too many one-liners are also bad. A good method should fit in one or two screens. And one more thing: try to follow logic, rather than performance. You should merge (or split) functions only if you experience real performance issues, otherwise you decomposition should follow your logic.
Upvotes: 0
Reputation: 62057
This question is pretty vague, but here are my general thoughts:
More methods will not slow your app down to any noticeable degree. That is optimization at really minute levels and is not worth the time.
Smaller methods are easier to read and ingest. They also are more likely to have only one duty. Both very good things.
Many methods can force a reader to maintain a large "mental stack" as they read through code and have to keep track of a lot of method calls. As long as methods do only one thing, and are named well, this is usually not a problem.
It really depends on your situation, what you're writing, etc. But in general, my rule of thumb is several small methods are better than one big, monolithic method.
Upvotes: 2
Reputation: 174369
It's actually good practice, because your methods have a clear functionality and do only one thing.
Upvotes: 0