I’m working with javascript for quite a long time, but it happens to me often that I come up with the following code:
function1 () {
// do stuff
...
end_function();
}
function2 () {
// do stuff
...
end_function();
}
function3 () {
// do stuff
...
end_function();
}
I know this is in conflict with the DRY principles, but unfortunately, I don’t know what to do with it to remove the repetitions. Does anyone know the answer?
It would be in conflict with DRY if you code the
end_functiondirectly into the other functions. Ultimately you do need some syntax to invoke the code that you abstracted into its own function.If functions are grouped together into an Object, I suppose you could enumerate the properties of the object, and wrap each function, but that’s probably only worth it you have many of these functions.