class method vs extension method

aronmatthew

Well-known member
Joined
Aug 5, 2019
Messages
88
Programming Experience
Beginner
Let's say for example we want a method to get inputs from a many text controls. My question is if it is possible to create a private method somewhere that does not show in intelli sense without having to use a separate class method with a long list of parameters. Or robust hard code that just assumes the identifies of the controls. The goal here is to avoid many form methods that show in intelli sense and also extension methods that take too many parameters.
 
If you read through some of the clean code and refactoring books, the common solution to methods or functions that take a lot of parameters is to put all the parameters into a data structure and just pass that single data structure. That data structure can be a struct, class, an array, a hash table, a list, a tree, etc.

Also some of the programming books will note that a method or function that has a lot of parameters might be a code smell of breaking the single responsibility principle. It might be time to breakup that big method or function into smaller pieces which take smaller sets of parameters, and just chain the functions together.

As a side note, normally, methods that are not in scope are not shown by Intellisense (and most other IDE's that have some form of symbol documentation). You can potentially have one private method that is the version that takes many parameters, but have multiple public overloads of the method that take less parameters. Yes, this will against your requirement of not showing many methods, but in my opinion, there is less cognitive overload seeing a single method with many overloads versus seeing a single method with many parameters. If good overloads are picked, you have a high probability of passing the minimum parameters needed with just using Intellisense and filling in the blanks. Compare that to trying to call a method with a ton of parameters and you wondering "what is this parameter? is it required or optional?"
 
Last edited:
Rather than working with text controls directly, consider binding data to them using a class that implements INotifyPropertyChanged.

For validation, create rules for the class using the NuGet package FluentValidation. By validating with FluentValidation, an instance of the class is passed to a validator, which in turn indicates if the object is valid. If not valid, a list of errors is returned.

See FluentValidation tips C# and FluentValidation Rule Sets.

Here is a sample for learning, which shows validation errors in a text box, while for a real app consider using an ErrorProvider. If not, Windows Forms, the links above will assist in ASP.NET Core.

AA1.png
 
If you read through some of the clean code and refactoring books, the common solution to methods or functions that take a lot of parameters is to put all the parameters into a data structure and just pass that single data structure. That data structure can be a struct, class, an array, a hash table, a list, a tree, etc.

Also some of the programming books will note that a method or function that has a lot of parameters might be a code smell of breaking the single responsibility principle. It might be time to breakup that big method or function into smaller pieces which take smaller sets of parameters, and just chain the functions together.

As a side note, normally, methods that are not in scope are not shown by Intellisense (and most other IDE's that have some form of symbol documentation). You can potentially have one private method that is the version that takes many parameters, but have multiple public overloads of the method that take less parameters. Yes, this will against your requirement of not showing many methods, but in my opinion, there is less cognitive overload seeing a single method with many overloads versus seeing a single method with many parameters. If good overloads are picked, you have a high probability of passing the minimum parameters needed with just using Intellisense and filling in the blanks. Compare that to trying to call a method with a ton of parameters and you wondering "what is this parameter? is it required or optional?"
This is similar to just passing a control container such as group box. However in this case I suppose PropertyInfo methods could be used. maybe another class that inherits from the form such as privateForm. Then in theory it could be possible to cast to this form and call the private method.
 
It looks like you didn't specify one of your other requirements: that this method or extension method with many parameters is common code between multiple forms.

Anyway, it sounds to me that this would be a perfect application of an interface. Every form that needs to use this common code would implement the interface. The common code then just has a single paramter: the interface. Every form then just passes it's this object.
 
Last edited:
Back
Top Bottom