c# - How to deal with passing parameters to methods which call other methods? -


First of all, I'm sorry for the obscure title, I find it difficult to think of a clear title Feel free to change the title of your description, feel free to have a better address.

In my code I often have a situation like this:

  public class processor {public Zero UpdateBatch (list> myType> mteps, int some id (Foo Extra, Int BarID) {Primary item (different items in my type) {If (some condition) (method (item, some id, additional); // Some other actions ...}}} Private zero method (myType , Int someId, Foo Extra) {if (string.isNullOrEmpty (myType.Prop) {Create (some, extra);}} (Int someId, foo extra) {var some = unitOfWork.Somes.AddObject (new something {Prop1 = extra.Bar})}}  

The one I wanted to show is that I However, some methods do not need all the parameters, but some follow up practices.

I was thinking how people deal with this. Do you make (personal) qualities? Or all To store property values, create a new square and pass that object around it? Like code? Or ...?

First of all, the answer is: One method should never be taken into criteria which does nothing with it. But if Method1 call Method2 , and Method2 requires a parameter that is reached or generated in Method1 If it does not happen, then by means of its logic, it is Method1 .

The variable should give at least the necessary scope. So unless we are talking about the value that would naturally be the property of the class (public or private), then you should probably stick to the personal example missing from the method.

Options are generally:

  1. Pass the value as a variable through method signature (gives less access, which is good, and what you do
  2. Expose the value for all your methods with another personal method (usually if some processing is required, or if you want to wrap, Private methods as a private member of the class (which is fine)

Now, let's talk about your statements By checking you can write:

I pass all the parameters for each method

This is not true You only UpdateBatch to method , you pass 3 out of 3, and after that you just create method from 2 of those 3 .

You also say:

Although some methods do not require all the parameters, but some follow-up methods

This is partly true

  1. Look at the method method. It takes 3 parameters and uses all of them, because if myType.Prop is empty or empty, then it create , Which takes the other two parameters. Look at the
  2. create method This method seems to be taking an unused parameter, some ID since this method is some ID So it should be removed from its parameter list. Once you remove it from create , you can remove it from the method , because method apart from it Do not use except for passing .

Comments