Winforms.Net Regarding the use of small methods

I am working on developing an application using winforms in C #. During development, I want to use many small methods, for example. resetPerticularCombo (), so the code remains as clean as possible. But the problem is that methods for 3-5 lines of code can lead to too many method calls, I heard that when compiling Visual Studio 2008 will take care of this thing by inserting the code.

My request is that if I rely on this function and suggest using small helper methods, or should I use the attachment myself?

+6
source share
3 answers

You should not worry that you have many method calls until you have proven that this is a problem. It is very unlikely (IMO) that this will really cause a problem, and if it gives you more readable code, this is the most important thing.

But check performance all the time to make sure it is acceptable. Find out where the bottleneck in your code, be it profiling or other methods, and consider micro-optimization only at those points where it will be of the greatest importance.

In my experience, microoptimization using the “use of smaller methods” is almost always meaningless compared to higher level changes (for example, moving from a list search to a dictionary search, etc.).

+7
source

I agree with John Skeet.

As for enterprise applications, I can tell you the following

I lead a small development team (5 developers). We have an application with a size of about 500 thousand words.

We always try to find the most specific problem that the method should have. Thus, we got a lot of small and "self-evident" methods. As a result, we have many methods, and this never leads to a problem.

In most cases, bottlenecks are in access to resources, such as SQL Server, Files, etc. Or lack of asynchrony.

In addition, you have performance that you could profile with ants profilder.

I also like these "optimization" rules that I found somewhere on the Internet.

FirstRuleOfOptimization - Not necessary.

SecondRuleOfOptimization - Not ... yet. R

ThirdRuleOfOptimization - ProfileBeforeOptimizing

If you are developing temporary software (related to graphics or a driver) then this may make a point, but then I would not be sure that .net is the best environment for this.

+2
source

Many small routines are fine, as the other answers already say, but don't forget to consider refactoring similar procedures for readability and maintenance (and not specifically for performance).

resetPerticularCombo() and resetAnotherCombo() may be worth converting to resetCombo(PerticularCombo) and resetCombo(AnotherCombo) if the routines otherwise match.

With anonymous lambdas, this can even be used to consolidate other similar algorithms: (a potentially bad example, but to continue using the OP code) ProcessCombo(PerticularCombo, (c)=>c.Reset()) and ProcessCombo(PerticularCombo, (c)=>c.SelectFirst())

I am a VB.NET programmer, and this code was injected directly into SO without syntax checking.

0
source

Source: https://habr.com/ru/post/892640/


All Articles