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?