Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

void method with arguments AND return value methods without arguments? [closed]

Tags:

methods

c#

object

I'm developing software in C# and I remember that I read an article with the following statement:

Design the methods of a class like this:

  • use a void method with argument(s) in order to change state of the class instance. method will do some changes on the data of the class
  • use return value with a method without any arguments in order to retrieve data from the class

I can't find the article anymore and I'm wondering about the benefits and drawbacks of such a convention in order to improve the quality and maintainability of the code I'm writing.

My question is: do you know any references/articles for this question? Does it make sense to follow these statements?

like image 969
faronel Avatar asked May 01 '26 17:05

faronel


2 Answers

C# has properties which I think makes these suggestions less practical. The big point to take away is that data inside a class should be encapsulated (the outside world cannot access it directly) and abstracted (the details are hidden). This is one of the primary purposes of a class. For this reason I think this advice would make more sense for a language like C++ that doesn't have properties.

One of the problems though is that people would write a class with private fields then have to write a getter method and a setter method for each private field and this results in redundant boiler plate code. Properties help streamline this strategy while still maintaining encapsulation and abstraction. For example,

UserInfo user = new UserInfo();
user.Username = "foo";
Console.WriteLine(user.Password);

In the above example I have set the username to foo and retreived the password for the user. Exactly HOW I set and retreived the information is hidden. It may be saved in an .xml file right now but later I decide to change to saving it in a database or directly in memory. The outside world that uses this classes is never the wiser. This is one of the many beauties of OOP.

The two bullets in your question can respond to a getter and a setter of a property. If I want to retrieve data from my class without any arguments it makes more sense to have a property. Likewise if I want to change the state of my object I can use a setter property. I can perform validation on the input, make it thread-safe, add logging, everything I could do with a method that takes a single argument.

like image 180
Despertar Avatar answered May 03 '26 06:05

Despertar


I suppose the bullet points are referring to Command Query Responsibility Separation. In practice you could still need to pass some argument to a return method in order to filter the results for instance. Using voids to change the state is sensible, and also making your return methods so that they don't change the state is good too, but really the number of arguments you pass in is not decided by whether you have a getter or setter method. It is really a factor of what a method needs to know to get it's job done.

The amount of arguments that you pass in should be kept to a minimum however. If you have more than one argument you should really consider if your method is doing more than one thing. I suppose the message is, "Have your methods do one thing and do it well". You can get loads of information about this from Uncle Bob, Bob Martin's Clean Coders series is a great source of information on this subject.

like image 28
Mark Dickinson Avatar answered May 03 '26 07:05

Mark Dickinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!