Conversation
Cargo cult programming patterns
Show content

In the post about software engineers’ burnout I briefly mentioned cargo cult programming patterns. Recently I got an insipation to introduce one notable example of them.

Callbacks. Normally callbacks serve for “lazy” or asynchronous execution. When precomputing some data has high pure computation costs, may trigger unnecessary invocation of garbage collector on big chunk of data. When part of computation is outsourced to other components/systems which live their life, and can parameterize computation with their own data (a very notable example is the client-server interaction when server can use internally stored data along with client’s data, “agent” callbacks). I.e. when computation triggers long pause at some moment. So asynchronous/“lazy” execution of code can be explained as doing a big working task X (executed by both agents Alice and Bob) in two separate smaller tasks: one for Y thing, one for Z thing, Z depends on Y, Y is executed by agent Bob, Z is executed by agent Alice. So executing “X(Alice, Bob)” is turned into executing “Y(Bob, () -> Notify(Alice, “can-do”, Z))”. And thus while Bob executes Y, Alice can do other things without waiting for Bob. When Bob finishes his task, he notifies Alice that she can do task Z. And thus Alice and Bob do their work in more efficient way, than if she and Bob executed the task X together.

However, I saw misuse of callbacks in code several times. This was in a code like this:

const funcX = () -> {
  const something = () -> {
    return anObject;
  };

  return funcY("somedata", something());
};

Instead of more pure and reasonable code:

const funcX = () -> {
  return funcY("somedata", anObject);
};

The first pattern remained stable in a project, that I worked for. It was even forced on code reviews for some time (until I took a part as individual contributor to a project).

The first chunk of code is a clear example of cargo cult pattern: when programming tool is used not for which engineering problems it solves. But because it seems “cool” on automatism, when original meaning of it is saturated and then new “meaning” is reinforced by some “cool guy” until some point. However, unreasonable complications (which are called “cargo cult patterns”) lead to loosing a stability of project’s coding patterns and then features, leading to architectural collapses and yard of bugs.

#blog #programming #work

0
0
0