Final evaluation and tips wanted for plugin

Hey guys,

This is a follow up from a topic I made a few days ago, and id like a final evaluation of my plugin - perhaps about the order of the functions, some final nitpicks, any useful advice for on the road, or anything really.

What the plugin does is replace the common event event command - instead being able to be called by common event name, instead of common event ID.

@Nolonar @Aloe Guvner Heres an alert for you both since you were involved in the previous topic. if you have nothing to add, no problem!

Code:
"use strict"

function findCommonEvent(eventName) {
    for (let i = 1; i < $dataCommonEvents.length; i++) {
        let event = $dataCommonEvents[i];
        if (event.name === eventName)
            return $dataCommonEvents[i];
    }
    return false;
};

function doStuff(eventName, THIS) {
    let ID = findCommonEvent(eventName);
    if (ID) {
        let eventId = THIS.isOnCurrentMap() ? THIS._eventId : 0;
        THIS.setupChild(ID.list, eventId);
    }
};

//function CEVENTS(NAME, THIS) {
  //for(let i = 1; i < $dataCommonEvents.length; i++) {
    //if ($dataCommonEvents[i].name === NAME) {
      //let commonEvent = $dataCommonEvents[i];
      //if (commonEvent) {
          //let eventId = THIS.isOnCurrentMap() ? THIS._eventId : 0;
          //THIS.setupChild(commonEvent.list, eventId);
      //}
      //break;
    //}
  //}
//};

var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
    _Game_Interpreter_pluginCommand.call(this, command, args);
    if (command !== "CommonEvent") return;
    doStuff(String(args[0]), this)
};
Inside findCommonEvent you can simply return event, because event is already set to $dataCommonEvents (it's shorter and therefore faster to type).

You should change the name of doStuff, because it's unclear what it does.
I know it can be a pain to decide how to name each and every variable, but when you need to fix something or add features to your old code, you'll be very happy if you can tell what your code does without having to read each line of code.
With that said, I took the time to understand what your code does, and I've determined that the best name for doStuff would be callCommonEvent.

Also, I noticed that in your doStuff function, you have a variable ID.
To be fair, when I see "ID", I think of a number or a text meant for identifying something. In this case, ID is actually a common event, so I'd call it event or commonEvent instead. You're less likely to mistake it for something else.

Next, about THIS. There are 2 things I'd like to suggest.

1. You won't actually need THIS, and can use this instead if you call your function differently.
Currently, you're calling doStuff(String(args[0]), this) (from here on out, I'll assume you'll rename it to callCommonEvent).
You can call it like this instead: callCommonEvent.call(this, String(args[0]))
The call function belongs to functions, and is used precisely for this kind of situation, where you need to define the this object used within the function.
So instead of function callCommonEvent(eventName, THIS), you can now have function callCommonEvent(eventName), which is nicer (and don't forget to replace THIS with this).
By the way, call was implemented in 1997, so I'm quite certain it'll work in MV 1.5.2😤

2. Since you'll be getting rid of THIS, the following suggestion is just for learning:
Try to avoid using the same name as something else but with different capitalization, like THIS or This. Not all programming languages are case-sensitive; relying on it can quickly become very confusing. If you really need a variable with a similar name, you can use _this or $this (avoid $this in this case, since $ is used to denote RMMV objects). Some programmers even use that, which is quite clever in my opinion.

Finally, at the end of your plugin you use String(args[0]). There's nothing wrong about that. I just wanted to tell you that args[0] in this case is already a string, so you don't need the String() casting.


If there's anything you didn't quite understand (or don't quite agree with), feel free to ask for more details.
@Nolonar Thanks for the tips, I dont see anything i dont agree with, it all seems useful advice! And I really appreciate you taking your time again to explain things to me.

I do have a few other insecurities tho, that ive seen with other plugins, that i dont fully grasp:
1) It seems that alot of plugins put all their code inside a (function(){})(), whats the benefit of that, instead of just leaving it without a function?
2) I see this alot in plugins
Code:
var Imported = Imported || {};
Imported.variablestuff = true;
and im not entirely sure what the point of it is.
3) Im a bit unsure about ordering your functions inside a plugin, does the order matter at all? and if so what would be the best order?
4) I put everything in the old function in '//' to make it unusable, but i wonder - is there an easier way to "get rid" code you may want to use for reference, and may need later on?

Also this is how it looks right now with your input:
Code:
"use strict"

function findCommonEvent(eventName) {
  for (let i = 1; i < $dataCommonEvents.length; i++) {
    let cEvent = $dataCommonEvents[i];
    if (cEvent.name === eventName)
    return cEvent;
  }
  return false;
};

function callCommonEvent(eventName) {
  let cEvent = findCommonEvent(eventName);
  if (cEvent) {
    let eventId = this.isOnCurrentMap() ? this._eventId : 0;
    this.setupChild(cEvent.list, eventId);
  }
};

//function CEVENTS(NAME, THIS) {
  //for(let i = 1; i < $dataCommonEvents.length; i++) {
    //if ($dataCommonEvents[i].name === NAME) {
      //let commonEvent = $dataCommonEvents[i];
      //if (commonEvent) {
          //let eventId = THIS.isOnCurrentMap() ? THIS._eventId : 0;
          //THIS.setupChild(commonEvent.list, eventId);
      //}
      //break;
    //}
  //}
//};

var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
  _Game_Interpreter_pluginCommand.call(this, command, args);
  if (command !== "CommonEvent") return;
  callCommonEvent.call(this, args[0])
};
1) It seems that alot of plugins put all their code inside a (function(){})(), whats the benefit of that, instead of just leaving it without a function?
It's used for scoping.

Normally, when you write your plugin like this:
Code:
var a = 1;
var f = function() {
    var b = 2;
}
... then b only exists inside f (local scope), but a (and also f) exists globally (global scope), so other plugins can access it too.
If another plugin uses a, it can conflict with yours by overwriting a when you don't expect it, which can cause your plugin to malfunction for no apparent reason.
To avoid this, you can put your plugin inside an anonymous self executing function. That way, your variables only exist within that function (as long as you define them inside the function), and no other plugin could possibly ever interfere. It's called anonymous because we define a function without a name, and it's called self executing because it runs as soon as it's defined.
It's made of two parts, the regular function part: function(arg) { /* code */ } (though we usually don't need arg), and the self executing part: (function)(arg) (the first bracket is around the function, the second bracket indicates that we're calling the function, and like I said before, we usually don't need arg).


2) I see this alot in plugins
Code:
var Imported = Imported || {};
Imported.variablestuff = true;
and im not entirely sure what the point of it is.
In this code, Imported is a so-called namespace (note: JavaScript doesn't actually have namespaces, it's just how programmers simulate one).
A namespace is like a variable, simply a name, but unlike variables they don't actually have any practical use. Their only use is to ensure that different people implementing similar stuff don't interfere with one another.

Usually, the programmer would use a name that they believe nobody else would use. Normally it's their name or initials, or the name of their company. For instance, Yanfly uses YEP (Yanfly Engine Plugins), GALV uses GALV, and I use NOL or NOLONAR.
That way, Yanfly could define a YEP.Vector, and GALV could define a GALV.Vector, and neither would interfere with the other.

In this case, the namespace is called Imported, so it's likely meant for compatibility with other plugins.
For example, Yanfly could implement an Imported.YEP_Vector, and if it exists, I would use it, otherwise I would use whatever already exists in RMMV.
To be honest, I'm still relatively new in the RPG Maker community, so I'm not entirely sure how the other plugin devs use the Imported namespace exactly...

As for the code:
Code:
var Imported = Imported || {};
... it's a common way to create a new object if it doesn't already exist.

It's a bit complicated to explain, so to make it more easy, consider the following code:
Code:
var a = b || {};
The variable a is set to whatever is stored in b, and if b doesn't exist (it's either false, 0, null, or undefined), then it's set to {} (an empty object).
In our original example, a and b are the same object (Imported), so it simply creates a new empty object for Imported if it doesn't already exist.

It's shorter than
Code:
if (Imported !== undefined) Imported = {};


3) Im a bit unsure about ordering your functions inside a plugin, does the order matter at all? and if so what would be the best order?
It depends on how you declare your functions, but in general, it doesn't matter much.

If you declare your functions like this:
Code:
var f = function() {
    // code
}
... then you have to declare f before you call it, otherwise it would be undefined and your plugin would crash.

If you declare it like this instead:
Code:
function f() {
    // code
}
... then you could call f before it's declared within the same function scope. For instance:
Code:
function f_1() {
    // f_2 already exists here

    function f_2() {
    }

    // f_2 exists here
}

// f_2 doesn't exist here anymore

Note that when I say "must be declared before it's called" I'm talking about the time when the function is called, not the position in the code.
In the case of your plugin, all the functions would've already been declared long before the first one is ever called, so you could order the functions in any way you like, even if they were declared as var f = function() {}; instead of function f() {};


By the way, the main advantage of declaring functions using var, is that the function can be used like a variable. You could for instance do this:
Code:
function Calculate(left, right, operator) {
    var f;
    if (operator === "+") {
        f = function(a, b) { return a + b; }
    }
    if (operator === "-") {
        f = function(a, b) { return a - b; }
    }

    var result = f(left, right);
}
This is just an example. If you ever want to write a calculator, I suggest you solve it differently 😉
The above example wouldn't work if you declared function f(a, b) {} instead, because both function f would exist inside the entire function Calculate scope and possibly overwrite each other. The behavior is actually undefined, so nobody knows what will happen exactly. Even if we found out, there's no guarantee it will still behave the same way on another platform or in the next version.


4) I put everything in the old function in '//' to make it unusable, but i wonder - is there an easier way to "get rid" code you may want to use for reference, and may need later on?
There exists a better way, but I'm not sure I'd call it "easier".
The better way involves setting up a versioning server, then committing your code to it. The server will always keep all changes you've made, so if you realize your code no longer works, you can look at all the previous versions from the server to see what went wrong and where. The server itself only keeps a copy of the first file committed, then all differences from each additional commit. That way, it doesn't need to keep a full copy of each file, which reduces the amount of space needed to store them.

SVN (Apache Subversion) and Git are two fairly well known examples of versioning systems.
The easiest way to set this up would be to buy a NAS that supports SVN or Git (I personally use Synology NAS with SVN package installed).


If that's not an option, you could try to create a .js file where you dump all your old code, so your actual plugin files stay nice and clean. It's up to your own preference, really.


Also this is how it looks right now with your input:
Looks ok to me now.

One little advice: You can comment multiple lines at once by using /* and */, which will comment out everything in between.
Example:
Code:
/* This is a comment
that spans multiple lines
and only ends after this: */

/*
It also works
like this
*/

/* or like this */

It's probably easier to do than adding // in front of every line.
@Nolonar Damn dude, I hope you realise how much you helped me out here! I will be able to go a long way with what ive learned from you. Im in your debt, thanks a ton! 👍😀👍
In this case, the namespace is called Imported, so it's likely meant for compatibility with other plugins.
For example, Yanfly could implement an Imported.YEP_Vector, and if it exists, I would use it, otherwise I would use whatever already exists in RMMV.
To be honest, I'm still relatively new in the RPG Maker community, so I'm not entirely sure how the other plugin devs use the Imported namespace exactly...

I'll just add that you're correct about this, for example, if I'm patching incompatibilities between two plugins then I'll use it to check whether plugins are installed or not and change the code as appropriate.

Original thread: https://forums.rpgmakerweb.com/threads/final-evaluation-and-tips-wanted-for-plugin.95626/