So Clean Code says you should separate each task to a single function (and add these functions a correct name).
I like the idea, but I've faced this problem so far: you receive a parameter which you want to use also on the topmost function, but you need it on the lowest level as well, so you have to pass the parameter to every intermediate function, which is not cool.
Here's my code:
onUpdate = function(currentSeconds){
updateRoute(currentSeconds);
updateAlerts(currentSeconds);
};
updateAlerts = function(currentSeconds){
for(var i = 0; i < alertMarkers.length; ++i)
oneAlertUpdate(currentSeconds, alertMarkers[i], alertAll[i]);
}
oneAlertUpdate = function(currentSeconds, alertMarker, alert){
updateAlertMarker(alertMarker, alert[entryIndex]);
changeMarkerOpacityIfNeeded(alertMarker, currentSeconds);
}
}
changeMarkerOpacityIfNeeded = function(alertMarker, currentSeconds){
//set opacity based on currentSeconds
var opacity = alertMarker.opacityTime < currentSeconds ? 1 : 0.5;
if(alertMarker.getOpacity() != opacity)
alertMarker.setOpacity(opacity);
}
- the parameter here is currentSeconds it has to be passed, but it's only used on the lowest level
- please note that I don't use currentSeconds directly in the topmost function, but it's crucial to be passed there, I'll write the reason if you say it's relevant