If createWorld()
is really long and I need to split it, I can split it to createLight()
, createEarth()
, createPlants()
, and createAnimals()
.
So, naturally I do:
function createLight(){
//work 1
}
function createEarth(){
//work 2
}
function createPlants(){
//work 3
}
function createAnimals(){
//work 3
}
function createWorld(){
createLight()
createEarth()
createPlants()
createAnimals()
}
But I see a lot of newer developers do what I am tentatively calling "the Stairstep antipattern". It goes something like:
function createWorld(){
//create light
//work1
createEarth()
}
function createEarth(){
//work 2
createPlants()
}
function createPlants(){
//work 3
createAnimals()
}
function createAnimals(){
//work 4
}
I am sure this is worse, but I don't think that my opinion alone can sway my colleagues. I think that if one step is createButterfly()
then that function has no business calling createGrasshopper()
at the end just because that's the "next step".
And even if you name it createButterflyThenCallCreateGrasshopper()
, it's still bad design because you can't test the createButterfly code well, and when you have several steps it gets hard to see where the functions are called.
I call it the Stairstep antipattern because I think of it like this:
|
|createWorld(){
|
|createEarth(){
|
|createPlants(){
|createAnimals()
Am I correct in thinking that this is a bad design? Why is this a bad design?
CreateWorld
is public. Based on the names of the method those can be easily extracted in own classes. And then only one class will have responsibility to combine them together in correct order – Fabio Dec 06 '16 at 15:30//work
chunks? Like in the case of an error? If so, then the second pattern is not so bad, because any early return short-circuits the child functions, which probably depend on success from the parents functions to do their work. – GHP Dec 07 '16 at 14:02