I call this type of code the “ninja code”.

You can’t see where it is.

It’s dangerous.

And it can strike you at any time and you won’t even know what happened.

Such is the danger of the ninja code 🙂

But semi-kidding aside – what is it?

It’s the code inside a function that contains side effects.

How To Recognize It?

You can’t.

And that is the problem.

Or to be more specific – you can’t recognize it without digging deep and analyzing every single line of code separately.

This approach is beyond impractical and simply cannot work.

The only way to deal with it is to prevent it from ever existing.

Spaghetti Code Example

Once I found an innocent-looking function named “loginUser()”.

What did I expect it to do? Well, just the regular stuff:

  • getting user token
  • maybe refreshing it if it expired
  • the usual stuff…

What I did not expect it to do?

  • creating a user on a different platform
  • creating and saving default documents on Amazon S3
  • bunch of other stuff

These things on their own are not wrong or a mistake. For various reasons they were app-specific and were necessary…


They were completely wrong to be included in the “loginUser()” function.

Those were the side effects. (or the ninja code as I like to call it)

Why Is This Wrong?

There are 3 main reasons:

1) It makes the code unreadable

Simply put – you can no longer trust your own eyes anymore.

Just because the code/function says one thing – doesn’t mean it’s not doing something completely different.

This makes all the names irrelevant and until you check what the function (and the function’s functions) are doing, you cannot know for sure.

2) It breaks the rules of precise naming

In my previous article, I explained the importance of precise naming.

This function is not properly named. If it was, it would go something like this: loginUserAndCreateUserOnAnotherPlatformAndCreateDefaultDocuments()

As you can see, this makes no sense.

3) Function is doing too much

According to some best practices (and common sense), the function should do only one thing. And this doesn’t mean you just name it as if it’s doing only one thing.

The function should actually do only one thing.

Immutability: Do Not Change That Variable!!!

Another thing I see fairly often is that functions change outside variable values.

If you want to make your life miserable then definitely do that.

Besides, this is very hard to debug.

Imagine we use the same example as before – our loginUser() function. And (for whatever reason) code changes some outside variable value.

loginUser() {
  // some code before
  posts[5].title = ‘try to debug this if you can’;
  // some code after

Now you have a “beautiful” situation where:

  • sometimes a random post changes its title and you have no idea why
  • you already checked all post related functions and found nothing
  • you are confused and slowly going crazy


I hope this was a sufficient warning for you to never make these mistakes again.

Beware of the ninja or the ninja will get you.
(and your entire team)

P.S. -> read this article if you want to double your salary