An Argument Against Requiring IIFEs

From Yanfly.moe Wiki
Revision as of 21:42, 7 November 2021 by Yanfly (talk | contribs)
Jump to navigation Jump to search

Welcome to the wiki! This is where you can find resources from Yanfly.moe, Ækashics.moe,
VisuStella, Caz Wolf, Fallen Angel Olivia, Atelier Irina, and other affiliated content creators.


Yanfly.png This is an article written by Yanfly.

Introduction

Nah IIFE.jpg

This is a programming article, so if you don't understand JavaScript, don't care about JavaScript, or don't care about Immediately-Invoked Function Expressions (IIFE's), this article will probably mean nothing to you.

Now that that's out of the way, let's make sure we're all on the same page here. An Immediately-Invoked Function Expressions (IIFE) is the following:

(function () {
    ...
})();

It's a private scope that's created within the code that will run immediately upon its code being read. This has applications, yes, and from personal experience, there are times where its useful, too.

However, there's been a trend in the RPG Maker community, especially amongst plugin developers, that argue that entire plugins need to be wrapped around in an IIFE. I personally don't believe that. In fact, doing such a thing will often lead to problems later down the road especially if a plugin library gets bigger and bigger.


The Common Arguments In Favor of IIFE's

Here are the things I've seen people argue in favor of IIFE's:

  1. Not polluting the global namespace
  2. Keeping code private and unreadable from NodeJS's dev tools

In regards to the first: Not polluting the global namespace, uh, I'm not sure about you, but that's almost never gonna be a major issue especially when programmers become more experienced. Very few experienced programmers actually declare commonly used variable names inside the global namespace unless they have a reason to do it. The majority of programmers already declare their own global namespace object and place their declared variables inside of that instead.

In regards to the second point: Keeping code private and unreadable from NodeJS's dev tools, this is meaningless unless you have the code obscured from legibility, too.