c# - How to choose between one big method with passive overloads and a bunch of small overloads, each one doing a small amount of work? -
There are two ways of implementing surcharges The first is to do everything in a method / constructor and call it with other surcharges , Which makes long method body . Second, the minimum is to be made in each surcharge, thus sometimes a code is difficult to navigate and to understand what surcharges what it does. For example, if two surcharge classes are cat
:
public cat (string name, int weight, color main collar); Public cat (name of string);
There are two ways to apply this:
first type
public cat (string name, int weight, color) Main color) {// Getting started all this.name = name; If (weight.HasValue) this.weight = weight.Value; // Here is a bug (see the TM at @ ATM): The main caller may be empty. This.colors = new list & lt; Color & gt; (New [] {main color}};} public cat (name of string): this is (name, blank, empty) {// nothing is to be done: everything is overloaded}
second type
public cat (name of string) {// at least start this.name = name; This.colors = new list & lt; Colors & gt; ; ();} Public Cat (string name, int weight, color main caller): this (name) {// do the rest, do not overload, if (weight.HasValue) this.weight = weight.Value ; This.colors.Add (mainColor);}
I think this is one of the examples where no single, radical Answer all the cases Agrees that it shall cover. I always look at the personal case and make decisions based on all available factors.
One aspect is that in the past there are many IFS. There is also a bug in your code: You have to add a null
value to the list of colors; To fix that bug, you need even more. I like a constructor can easily be dirty. There are innumerable signs of iOS that in many cases the reasoning is quite different, so for each of those cases, different constructors have the right understanding.
However, in such cases where there are not many people ifs, logic is the same for everyone, so now it is understood to call a single constructor which is an argument and it is good By the way, there is only one place to maintain it.
Another aspect is that in your example, the first one leaves weight
uninitialised this is not a bad thing because fortunately in C # The default is the initial forecast; But I would consider it a bad form if the field for the announcement of weight
was made to start some non-zero and only some consultants would overwrite that default with another value Constructor parameters and / or this (...)
calls are a better place to document the default value for that field. (Preferably the manufacturer parameter, because even client programmer can see the default value , but obviously C # 4 is required.) Of course, all It is also possible to get started using field field starters and leave blank constructor (empty), which is a reasonable strategy if you do not have any logic.
So yes, as you said, you do not want the system to get the bodies for a very long time, but you also do not want the code to be very difficult to navigate, then for any situation, A balance must be stopped between the two.
Comments
Post a Comment