I have a large data structure that is about to be persisted to the database. Before that can happen I have to validate it and then update a bunch of it's properties based on various specific conditions.
Which style is more readable, having a bunch of if/else statements which determine which functions will execute under specific conditions or a simple list of functions to execute which contain within themselves the condition under which they should execute?
The first gives me indentation hell and nested ifs, the second looks clean but gives the impression that the list of functions always executes and requires viewing each function to see the circumstances under which each function will run.
Example of if/else style:
function myFunc(myThing){
if(myThing.myProp === 'someValue'){
doStuff();
if(myThing.myProp2 === 1){
doAThing();
} else {
doADifferentThing();
}
} else {
doOtherStuff();
}
}
Example of conditions nested in functions:
function myFunc(myThing){
doStuff(myThing);
doAThing(myThing);
doADifferentThing(myThing);
doOtherStuff(myThing);
}
function doStuff(myThing){
if(myThing.myProp === 'someValue'){
//Do stuff
}
}
function doAThing(myThing){
if(myThing.myProp === 'someValue' && myThing.myProp2 === 1){
//Do a thing
}
}
function doADifferentThing(myThing){
if(myThing.myProp === 'someValue' && myThing.myProp2 !== 1){
//Do a different thing
}
}
function doOtherStuff(myThing){
if(myThing.myProp !== 'someValue'){
//Do other stuff
}
}
These examples aren't perfect, but hopefully they convey what I'm asking.