Orignal code was like this:
I changed it to below:
I moved the method meth to another class and named it MyClassHelper.
It works fine . Is it a correct way of coding?
Another comment to your code, when you have split code to the helper function, remember that this function only do a very common function, it will not process any particular problems, you must make it as general as you can. All particular problems (if need for run helper) must pre-process then pass into helper function via it's arguments.
As I see in your code, the function 'doSomeThing()' after you split the helper, this function does not have any code you just call the helper, it will be not a good practice.
Your code should be like this:
Interesting spelling; I think you mean superclass.
Nguyen Tuyen wrote:. . . a supper class A, with method called run()
Although many people say parent‑child, those are maybe not the best terms to use. A subclass is a specialised kind of superclass, and you cannot usually say that a child is a specialised type of their parent(s). The Java® terminology is superclass‑subclass, but maybe what the C# people say is better: base class‑derived class.
and all child of it . . .
As a rule of thumb, consider whether a method is a function. That means all its input information comes from its parameters (or constants) and all the information it produces goes into its return value. In which case it is a 1368 in the most dubious classification of methods ever seen. In which case you can consider making it static. If you now have static methods which are used in more than one place, yes, consider creating a utility class to encapsulate them.
If you now have static methods which are used in more than one place, yes, consider creating a utility class to encapsulate them.
Rest of the things is correct. But the static methods which I have are used only once. In old code there was a class with big methods and code was not looking clean. I moved most of the code to another helper class and called the methods of helper class from the class. Now the code looks more cleaner. Is it a correct way?
As a general rule of thumb, if the code looks cleaner it is better
Thanks. So can the name of Helper class still be some Helper only in this case. (when the static methods are actually being used only once in code)
Randall Twede says
some general rules about this.
a class should do one thing.
even more so a method should do one thing.
try to keep your classes and methods small and self-contained.
In case a class should do only one kind of thing should there be multiple helpers in this case doing one kind of thing each?
If this is object code (ie non-static) then any sub methods created as part of this breakdown will be non-static except in rare cases where you identify common helper code (code likey to be usable elsewhere) which would then be removed and placed in a proper utility class. I would not (in general) make methods static in my model/processing classes simply because they could be.
Stephan van Hulst wrote:
Campbell Ritchie wrote:Utility classes don't represent anything at all; which is why you don't usually instantiate them.
I'd go one step further and say that you should make it impossible to instantiate them, by giving it a single private constructor that does nothing.
Thanks. What issue can occur if we do not keep a private constructor..
For the same reason, you never override any Object methods in utility classes. If you cannot create any instances, you can never call equals(), nor hashCode(), nor toString(), so overloading them is pointless.
Note it would have been possible to design the Math class as an ordinary concrete class:-But that isn't how it was designed. I suspect it is because in C/C++ mathematical functions are implemented as “naked” functions in the math.h header file and the easiest way to implement such multiple functions is as a utility class. It certainly makes it easier to implement functions like max and min.
. . . and forgot to explain that properly. That is of course indirect access to the toString method. There is no need for the Math class to print its details, because it hasn't got any program state of its own.
A few minutes ago, I wrote:. . . Unnecessary access to methods:-. . .