Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Parameter Dilemma

Tags:

c#

I always come to a halt when creating classes. I'm fairly intermediate to architecture so bear with me. Here's my dilemma. I come to a point eventually in a class where I have to ask myself, "Does this method need parameters when I can just get what I need from the class's property or private backing field?".

Now for public methods, obviously you should most likely have parameters because someone's going to be consuming your class and using these methods whether they're in an instance class or static class. That debate is not about public methods because that is obvious to me. That if you have a public method(s) inside your class, even though lets say you can get one of the values you need from a property or private field in that class instead of requiring a param to that method and using the param, you should still require params and use the params or at least don't count them out when you can specify some params. At least this is how I view public methods because who knows how someone else might use that method and they need to know what needs to be passed and they need to pass the actual data to the method as they are outside your class.

The issue comes when for example I'm creating and using private methods or something of that sort. Lets say I create a custom control (.cs). Its job is to run a bunch of methods that I've broken out the logic for this control and create some strings of HTML to output to the browser. Well, I create a property that is public so that whoever is using this control can feed it a certain object. And that certain object is what half of the methods in my class use to help generate that HTML. Because it's a property, any of the methods in my custom class can use it. So it completely bypasses any need to create parameters in some of those methods because I can just get it form the property. But then I get to a point where I'm creating an awful lot of parameterless methods because I'm getting objects from the backing fields or combination of backing fields and properties. Or I might be just getting them from a few propewhen I am able to get what I need other ways inside this class? But then something says to you no, that's bad man, you do need parameters once in a while dude...at least a combo of parameters and using some backing fields or properties in your method, etc. but don't always discount parameters even if those params might be some internals passed to it (fields or properties). But then again if I'm gonna be passing internals as params where's the fine line between just accessing and using those fields or properties not through the method as params but directly inside the method itself.

But that bothers me because then I question why I need method parameters in cases where I can get the value elsewhere.

Can someone explain the fine line here and help me come to a conclusion on the line between using a lot of backing fields in your methods or properties rather than some params you specify in the methods..even though some of those params may still be passed a value from a backing field or property when another method calls it?

I hope this makes any inkling of sense but I can't be the only one who comes across this dilemma. Maybe I'm the only one who thinks about this sh** and I over think I don't know. But the main problem I have personally with OOP is that there are so many damn ways to get what you need (constructor, property, backing field, method).

like image 672
PositiveGuy Avatar asked Jul 14 '09 01:07

PositiveGuy


1 Answers

less coffee, fewer words ;-)

to summarize

when to use parameters in methods?

short answer

when the information needed is not available already in the object, in accordance with the Don't Repeat Yourself principle

parameterless methods are fine, there's no need to tell an object something that it already knows

like image 190
Steven A. Lowe Avatar answered Sep 20 '22 21:09

Steven A. Lowe