-
Apr 22nd, 2015, 12:21 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Discussion on how to share data between private methods
Hi!
I had a discussion with colleagues today about a class we all are working on. It is a validator class that uses dependent on two business objects
The purpose of the class is to validate sample data (list of testpoint objects). Today the class has one public method Validate() which takes two properties
1) tolerance as string
2) measurements as List(of TestPoints)
The method Validate then creates two private business objects which are set as private member variables. The method then calls 3 private methods that performs differnt kinds of analyzes on the measurements and then finally the Validate method returns a List(of TestPoints) with added information about each TestPoint wether or not it passed or failed validation. All private methods uses 1 or 2 of the business objects.
The argument is this:
* Some of us think that there is no point in declaring private member variables, but we should just take whatever we need in the Validate method parameters, and then if this data is needed in the private methods, then pass it along to them via parameters. We feel it is meaningless to use private fields to share this data between the different private methods, because it is well... just not right. Basically we feel the validation class shouldn't hold any state because it doesn't have to. The only rason it is not static is because we want to use DI to inject it and moq to mock it for unit tests.
* SOme other of us feel it is much more convenient to share this info through private member fields, so we don't have to mess with parameters on internal private methods, and the fields are not accessible from outside of the class anyway. And every call to Validate() create fresh objects, so there is no risk of "old" data tampering with the results if someone wants to call Validate() 10 times on the same object with the same input. The output will still be the same since the state is renewed each time.
What do you think? What way of programming has the least code smell? Any benefits of either method compared to the other? Are both equally good/bad? Any good patterns/practices to lean on?
I have done some googling, but didn't come up with much
/Henrik
-
Apr 22nd, 2015, 12:50 PM
#2
Re: Discussion on how to share data between private methods
Originally Posted by MrNorth
Hi!
We feel it is meaningless to use private fields to share this data between the different private methods, because it is well... just not right. Basically we feel the validation class shouldn't hold any state because it doesn't have to.
/Henrik
If the object has state, even if it's private state, then don't fight it. You could make the argument that a class "shouldn't hold state because it doesn't have to" for nearly all cases - if state is natural for this then use it.
-
Apr 23rd, 2015, 12:48 AM
#3
Thread Starter
Frenzied Member
Re: Discussion on how to share data between private methods
Originally Posted by David Anton
If the object has state, even if it's private state, then don't fight it. You could make the argument that a class "shouldn't hold state because it doesn't have to" for nearly all cases - if state is natural for this then use it.
That sounds reasonable. We ended up with a mix. We put two business objects in the constructor, and then we assigned private member variables to whatever felt the best. Everyone is happy now.
/Henrik
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|