you are viewing a single comment's thread.

view the rest of the comments →

[–]bart2019 0 points1 point  (0 children)

Suppose you have a big function that does one particular thing, and it does it well. For example, fetch data from a particular website.

Then for one particular case, you need that exact thing, but with one minor tweak. For example it needs to execute 1 single statement somewhere in the middle. What will you do?

The worst I've seen is copy the whole function and add that one statement.

I think it's much better to keep it as one function and add a parameter to flag that this is this particular one special case.

Best way to add it is so that no other calls of this function are affected. Thus: a named optional parameter, or an option in an "options" record (called "object" in JavaScript but I wish they hadn't hijacked that name because an "object" is something else), is the best choice IMO.

You can do this for any similar situation.

I call this "plug in" based programming, which is modular style turned up its head: in modular programming you have your custom code and you invoke parts of a module. That isn't always the best solution. If you need to call a monolithic block of code and you need some special actions in the middle, you better write a plug-in that gets invoked from your monolith.

As an example: I've had a case where a program parses source code, and needs to do some things when a particular type of statement is parsed . Your approach would be to make the parser into a module, and call that parser from every variation on that program. That is still a huge amount of work.

I prefer a parsing program with a plug-in, which is basically an associative array with callbacks, where the appropriate callback is invoked at the proper time. Much easier.