Global member and passage parameters

I have an ASP.NET project in which I have a method with 10 local variables. This method calls about 10 other methods. 3 of these methods need all variables. Is it considered good practice to turn all of these variables into global members, and then they should not be passed as parameters?

+3
source share
7 answers

create a structure and pass the structure instead of passing these 10 parameters

For instance:

public struct user { public string FirstName; public string LastName; public string zilionotherproperties; public bool SearchByLastNameOnly; public datetime date1; } 
+3
source

If you want to convey a complex state around, pack it into an object - i.e.

 public class Foo { public string Key {get;set;} public decimal Quantity {get;set;} // etc } 

And let the methods take this object as an argument. Then you just instantiate it and pass it.

Global - big no no; ASP.NET is very multithreaded - it would be a nightmare. The status of the request may be possible, but a little random.

+8
source

Are these variables related to each other, or are they all, or perhaps several groups? If so, encapsulate them in a type. Thus, you can have half of your variables related to the user, and half related to the requested operation, and suddenly your method taking 10 variables takes only 2.

Making things global is almost always the wrong way.

+4
source

Well, it totally depends on what you mean by "global members."

If, given that you are writing an ASP.NET application, you mean session / application based cache values, then it depends. There are performance implications, so you need to evaluate if this affects your application.

If you mean static variables, then no. Static - for each application and, therefore, for all users of your web application, and not just for one person. A static theme is not a good idea, as one user can swim between threads throughout his life in the application.

+2
source

If you have methods that really act on a large number of variables, for example, as you mentioned, you can also consider creating a class that should act as a data container. After filling in, you can pass the class to functions that require data instead of ten parameters.

I can’t recall the exact example, but in the Microsoft book, Framework Design Guide, they explicitly describe a scenario similar to yours, as well as how they followed the same approach in the .NET Framework.

In addition, if you need to pass this many parameters, take a step back and make sure that this code does not need to be refactored. There are legitimate cases where a method requires a lot of data, but I use long method signatures as a sign that I need to look inside the method to make sure that it does only what it needs.

+2
source

Just be sure to keep up with boxing. If you go through 10 types of links, it comes down to personal preference.

However, if you pass 10 types of values, if you must declare them as member variables in the class, they will be boxed, then the recipients will have to be unpacked by the receiver.

If you leave them limited by local variables in the method stack (passing them as parameters), they will remain purely on the stack, and not be put in the heap.

+1
source

For purely mechanical refactoring, packaging together (as suggested) is probably the best solution.

However, you have a large series of dependent methods, each of which acts on the general condition (at least 10 values). It looks like you should develop a class to handle this operation.

The class will encapsulate behavior and the corresponding state, and not just a bag of properties (see. Anemic domain model ).

+1
source

All Articles