What's wrong with anonymous functions?

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
Triggered by this comment but I didn't want to get into the discussion in that thread.

one thing that bug me...

some plugins that wrap in anonymous function

(function() { // …})();it's okay... but the problem lies when they alias something but using private variable inside that anonymous function. this mean the old method cannot be accessed by other plugins.
Code:
(function() {  var estMyOldFunction = Game_Enemy.prototype.opponentUnits;})();
we cannot access the estMyOldFunction thus the Game_Enemy.prototype.opponentUnits before modified is LOST...at least store the old function inside a module / container wrapper !!!
I don't understand this. The kadokawa plugins all use this:

(function() { // ...)}();and I've written all of my plugins to follow the same format.I think what you're saying is you can't access the original, unmodified version. But if you have a number of plugins that all modify the same function, wouldn't you want the mods to stack? What situation would there be where you have two plugins that modify the same function, where you want one plugin to ignore the mods of the other?
 
Last edited by a moderator:

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
I will explain by yanfly message who explained me why anonymous function are "bad" for MV

Sent 26 October 2015 - 01:42 AM
Anonymous functions are harmful for MV because of the community-setting we have. Because we have more than one scripter, there comes the need for eventual compatibility patches.

For that reason, debugging each other's plugins and whatnot becomes a bit tougher. It's not impossible, but it can be a pain. Anonymous functions cannot be reused unless you use the right scope, cannot be tested in isolation with a unit testing framework, etc. Just because a practice is widely used doesn't mean it's necessarily the right way to approach it (remember when people used to use HTML tables for website layouts?).
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,546
Reaction score
3,717
First Language
Java's Crypt
Primarily Uses
RMMZ
Yes, they meant that if they wanted to make a compatibility plugin between two other plugins, they may need to call the older method of one of them. When you alias a method to a simple var inside the anonymous function, that var can't be accessed from anywhere else.

But there's nothing wrong with anonymous functions, in fact, it's bad NOT to use them. The problem estriole mentioned lies with using something inside it and not exporting it to be accessed outside.

I don't bother exporting my aliased methods because I can make compatibility patches myself.
 
Last edited by a moderator:

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
because everything inside that anonymous function is scopped to that function, including aliases. This is great if you are creating classes, functions, varaibles and so on that cannot be mutated randomly by people trampeling all over the code base.

I use this concept in the form of modules. In ES6 for example when you export something or import something, when compiled down it wraps the code in these types of functions.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
Shouldn't plugins be like black boxes?


Is this ONLY an issue when you're trying to make a compatibility patch between two?


Can anyone give an example of two plugins - actual code (hypothetical is fine) where this would become a problem, and an example of how they could be written to overcome the problem?


I'm just trying to determine whether I should go back and redo all of mine.
 

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
Shouldn't plugins be like black boxes?

Is this ONLY an issue when you're trying to make a compatibility patch between two?

Can anyone give an example of two plugins - actual code (hypothetical is fine) where this would become a problem, and an example of how they could be written to overcome the problem?

I'm just trying to determine whether I should go back and redo all of mine.
Honestly, you should be fine. As far as I can think, the only issues really come up when it comes to compatibility patches. However, the way I make my scripts, all aliases are stored in my own "module" (It's a JS object ( { } )) that I store all plugin parameters and aliases in. This way, the functions I aliased can still be accessed as they were before I changed them. This way, should someone need to make a compatibility patch with one of my plugins, it's easily doable for them, in the event that I'm not able to do it myself. The anonymous function, in my case, isn't really required for most plugins since I don't really create that many variables outside of a function, but I do it to both encourage the use of it, and to follow the standards that are used in MV.
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
two plugin who alias the same update

// original methodSpriteset_Base.prototype.initialize = function() { Sprite.prototype.initialize.call(this); this.setFrame(0, 0, Graphics.width, Graphics.height); this._tone = [0, 0, 0, 0]; this.opaque = true; this.createLowerLayer(); this.createToneChanger(); this.createUpperLayer(); this.update();};// method A called beforevar alias_A = Spriteset_Base.prototype.initialize;Spriteset_Base.prototype.initialize = function() {alias_A.call(this);//your content};// method B called aftervar alias_B = Spriteset_Base.prototype.intialize;Spriteset_Base.prototype.initialize = function() {//your contentalias_B.call(this);};you may encounter bug with the update method.

SINCE if you do method A they will call update BEFORE material is created. 

SO if example I do method A...it's just create something new and don't update without a flag. then it's fine

BUT method B require to always update. then it's will be fine to call it.

Now here begin the problem if we input the plugin.

If method A is call before Method B it's will provoke a crash of no method since ...update is call BEFORE method b is call. so this why Anonymous don't help here since we can't reach the other method alias for do this :

var alias_A = alias_B.prototypeSpriteset_Base.prototype.initialize = function() {alias_A.call(this);//your content};where it's will get the content of B and not provoking bug. Although not the best performance method

EDIT : 

the problem with alias in general are they are hierarchical and  depends of the specific order you call them. The way's to fix this would be the way's you organize your plugin order so method_B would not get a error since it's alias is call before method A so it's have time to get call.
 
Last edited by a moderator:

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
Shouldn't plugins be like black boxes?

Is this ONLY an issue when you're trying to make a compatibility patch between two?

Can anyone give an example of two plugins - actual code (hypothetical is fine) where this would become a problem, and an example of how they could be written to overcome the problem?

I'm just trying to determine whether I should go back and redo all of mine.
This becomes a problem when, for example:

Currencies, it has a shop functionality.

Detailed Shops Script - re-writes shop functionality, look and feel.

These two are incompatible because my "black box" depends on functions and functionality either not present in this or other functions.

For me, I like the concept of modules or anonymous functions because at the end of the day I don't want you walking all over my code. Other people would say this is bad because if I change functionality then there script is screwed. What you do is something like:

(function() { var oldGameInterpreterPrototypeCommand125Method = Game_Interpreter.prototype.command125; Game_Interpreter.prototype.command125 = function() { // Your code ... oldGameInterpreterPrototypeCommand125Method.call(this) }}();By making sure you always always always call the old function you make sure that other peoples scripts still work with your stuff. its when you decide not to call the old method that we have an issue.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Honestly, you should be fine. As far as I can think, the only issues really come up when it comes to compatibility patches. However, the way I make my scripts, all aliases are stored in my own "module" (It's a JS object ( { } )) that I store all plugin parameters and aliases in. This way, the functions I aliased can still be accessed as they were before I changed them. This way, should someone need to make a compatibility patch with one of my plugins, it's easily doable for them, in the event that I'm not able to do it myself. The anonymous function, in my case, isn't really required for most plugins since I don't really create that many variables outside of a function, but I do it to both encourage the use of it, and to follow the standards that are used in MV.
I don't understand. You mean, if you create your own module and you place all the aliased values in it, should it all still be callable in a new plugin written in a different format? Forgive me but I want to understand this as well. Say for example, I write something like this:

(function(){ var Milena = Milena || {}; Milena.params = PluginManager.parameters('DiarySystem'); Milena.DiaryStstem = { someAliasStyle: { Game_Screen: { update: Game_Screen.prototype.update, } } };})();Does this mean that when someone, right before, a plugin, accesses Game_Screen's update, be able to include all the updates you've done with Game_Screen when they alias it too?
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
You should declare "Milena" outside the function, otherwise it won't be accessible by other plugins or the user.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Ha... thanks, now I understand. <3
 

estriole

Veteran
Veteran
Joined
Jun 27, 2012
Messages
1,316
Reaction score
537
First Language
indonesian
Shouldn't plugins be like black boxes?

Is this ONLY an issue when you're trying to make a compatibility patch between two?

Can anyone give an example of two plugins - actual code (hypothetical is fine) where this would become a problem, and an example of how they could be written to overcome the problem?

I'm just trying to determine whether I should go back and redo all of mine.
from what i experienced... the only issue is when we're trying to make compatibility patch. no other issue other than that.

in my opinion... used or not... when we alias we shouldn't remove reference to old method. it's just like overwritting things instead of alias...
 
Last edited by a moderator:

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,546
Reaction score
3,717
First Language
Java's Crypt
Primarily Uses
RMMZ
from what i experienced... the only issue is when we're trying to make compatibility patch. no other issue other than that.

in my opinion... used or not... when we alias we shouldn't remove reference to old method. it's just like overwritting things instead of alias...
Have you ever had a situation where the reference was of any use?
 

estriole

Veteran
Veteran
Joined
Jun 27, 2012
Messages
1,316
Reaction score
537
First Language
indonesian
Have you ever had a situation where the reference was of any use?
yes in fact i have... when writing compatibility patch between my autocolor plugin and yanfly message core.

second one is when writting compatibility patch for Galenmereth Mouse System Ex.

it would be lots harder to make the patch if the old method reference is lost.
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
556
Reaction score
795
First Language
English
Primarily Uses
RMMV
Just so I can understand this a bit better.

For example, with aliasing and creating our black box plugins.

So for example as Zalerinian mentioned.

They store the old aliases  in a js object.

Example:

//Maintain global scopevar module = { //list of aliases to be used. }(function(){ //references to functions from the module //my code here })();We should be doing something like the above?

Also, would it be a good idea to reference our anonymous function as it's own module if we wanted other users to have access to it?

Example:

var myPlugin = (function(){//plugin code})();Or is this a bad idea?
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,848
First Language
English
Shouldn't plugins be like black boxes?

Is this ONLY an issue when you're trying to make a compatibility patch between two?

Can anyone give an example of two plugins - actual code (hypothetical is fine) where this would become a problem, and an example of how they could be written to overcome the problem?

I'm just trying to determine whether I should go back and redo all of mine.
I wouldn't say plugins should be black boxes.

They should be black boxes to people that will be using them and don't need to worry about how it works, but for developers that may need to modify your plugin one way or another, they would need a way to do so.

For example, I don't define anonymous methods.

var TH = TH || {};(function ($) { $.myFunc = function () { // something }})(TH.Namespace);If anyone needed to touch that method later on, they can use

Code:
TH.Namespace.myFunc
to access it.Now if someone didn't have a way to access myFunc, and they actually wanted to add something to it...well, they'd probably be stuck.
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
Also, would it be a good idea to reference our anonymous function as it's own module if we wanted other users to have access to it?

Example:

var myPlugin = (function(){//plugin code})();Or is this a bad idea?
This won't reference the function, but its return value.
 

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
Just so I can understand this a bit better.

For example, with aliasing and creating our black box plugins.

So for example as Zalerinian mentioned.

They store the old aliases  in a js object.

Example:

//Maintain global scopevar module = { //list of aliases to be used. }(function(){ //references to functions from the module //my code here })();We should be doing something like the above?

Also, would it be a good idea to reference our anonymous function as it's own module if we wanted other users to have access to it?

Example:

var myPlugin = (function(){//plugin code})();Or is this a bad idea?
Code:
var myPlugin = function(){//plugin code}; // if your plugin is one function then this is a plugin.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!
one bad plugin combo later and one of my followers is moonwalking off the screen on his own... I didn't even more yet on the new map lol.
time for a new avatar :)

Forum statistics

Threads
106,017
Messages
1,018,356
Members
137,802
Latest member
rencarbali
Top