OOP Coding Style – Best Practices for Private Method Parameters

object-oriented

After coding for many years as a solo programmer, I have come to feel that most of the time there are many benefits to write private member functions with all of the used member variables included in the parameter list, especially development stage.

This allow me to check at one look what member variables are used and also allow me to supply other values for tests and debugging. Also, a change in code by removing a particular member variable can break many functions. In this case however, the private function remains isolated am I can still call it using other values without fixing the function.

Is this a bad idea afterall, especially in a team environment? Is it like redundant or confusing, or are there better ways?

Best Answer

allow me to check at one look what member variables are used

Using a method signature solely to identify members of a class is a crude, non-OO style of programming. OOP is about higher level abstractions and relationships. If you are having trouble tracking a ridiculous # of members, then you should immediately review your classes to ensure they have a single and easily identifiable purpose. This sounds like you are constructing god objects.

Also, a change in code by removing a particular member variable can break **many** functions.

A great indicator of tight coupling. Decouple your classes. Look at dependency injection..

allow me to supply other values for tests and debugging.

Unclear on meaning here. Especially because you go on to say

In this case however, the private function remains isolated am I can still call it using other values without fixing the function.

Generally, private functions make code more difficult to (unit) test not easier. Are you familiar with Mocking? Are you testing with unit tests, some kind of home brew or other?

Related Topic