Reputation: 54183
I often find myself programming like this:
public class SomeClass implements SomeInterface
{
private someData;
public SomeClass(Parameters And Stuff)
{
}
private void a()
{
e();
f();
g();
}
private void b()
{
h();
i();
}
private void c()
{
j();
}
private void d()
{
...
might call even more private methods...
}
private void e()
{
...
might call even more private methods...
}
private void g()
{
...
might call even more private methods...
}
.......
private void j()
{
...
might call even more private methods...
}
@Override
public void interfaceMethod(Some Parameters)
{
a();
b();
c();
d();
}
}
The real method names are meaningful and have return values and parameters.
But the point is, there are only 1 or few methods hat the user calls, which is good, but, in order to try to keep the implementation clean, I break it down into a whole lot of private methods.
Is this a good idea? If someone else looks at my code, unless they follow the execution trail, it might be a little confusing?
Is this programming style acceptable? Is there maybe a cleaner clearer way to deal with this type of situation?
Thanks
Upvotes: 2
Views: 1016
Reputation: 818
Off course is a good practice, each time you create one of those private method your are giving a MEANING to this lines with his method name, there are some considerations:
Upvotes: 0
Reputation: 94499
This practice is 10x better than its inverse (1000 line methods). Just make sure you adhere to the DRY principle and keep an eye out for areas in the code where an object could make life easier. You can usually identify these areas by keeping tabs on the number of arguments passed to the methods or the same arguments being passed to multiple methods. This is a clear indicator a class instance variable or object is required.
Another quote from Bob Martin's book Clean Code is:
The first rule is that methods should be small. The second rule is that methods should be smaller than that.
Upvotes: 1
Reputation: 41210
This really comes down to coding style rather than any hard and fast rule.
The thing with these sort of methods is to ask yourself:
Is the code reusable - if so it would make a good method
Is the code very long - if so I should consider splitting it
Can the code be made more readable by splitting it - if so I should consider splitting it.
On the other hand though:
Is the code not re-usable? If I will only ever call it from one place is it worth having it as a method?
Is this method trivial, is it actually worth having separately or should it be merged into another?
Does separating the code maker it clearer/cleaner - or would it actually be simpler to merge it?
Judging the correct way to weight these sort of questions is what differentiates an experienced programmer with a clean architecture from a novice programmer with a mess. Unfortunately I can't give you a hard-and fast answer though as it is more art than science.
Upvotes: 5
Reputation: 698
As long as the method names are properly descriptive, this is a good practice.
Upvotes: 1