Helping me reduce this Method size

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
Hi guys so I was writting a secret plugin although I meet a problem....this method is like TO MUCH big and have repetetive code in it...so I would like to ask you to show me possible way's to shortening this method?

because I will being honest it's like 51 freaking line's >>"

Emoji_Secret.prototype.updatePictureSprite = function(sprite) {// setting the sprite bitmap. sprite.bitmap = this._imageBitmap;// if spinBitmap is enabled it's will doing rotative movement. if (this.spinBitmap){ sprite.rotation += this.rotationSpeed;// else if rotation = 0 it's not affecting the sprite. } else if (this.rotationSpeed === 0 ){ sprite.rotation = 0;// else the sprite is serve for reference and rotating the sprite in a // specific angle. } else { sprite.rotation = Math.PI / this.radiant; }// Starting Sprite movement.// if static is enabled. if (this.enableStatic){ // if particle are falling. if (this.fallingParticle){ sprite.ay += this.speed * Math.cos(sprite.rotation); // else particle are moving in up movement. } else { sprite.ax += this.speed * Math.sin(sprite.rotation); } // if inverted Axis is enabled for sprite it's go for the right if (this.invertAxis){ sprite.ax -= this.speed * Math.sin(sprite.rotation); // else the sprite will start from the left. } else { sprite.ay += this.speed * Math.cos(sprite.rotation); } // if static is not enabled. } else { // if particle are falling. if (this.fallingParticle){ sprite.ay += this.speed; // else particle are moving in up movement. } else { sprite.ay -= this.speed; } // if inverted Axis is enabled for sprite it's go for the right if (this.invertAxis){ sprite.ax -= this.speed; // else the sprite will start from the left. } else { sprite.ax += this.speed; } }// FadeOut the sprite. sprite.opacity -= this.speed; };thanks for any further answer
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
While you could trim this down by using ternary operators (var a = expression ? b : c;), there is nothing inherently wrong with a long function. Most of the time, simple ifs are actually faster, than using short, fancier ways.


/edit: That said, i would probably split the function at the outermost if and add 2 more. "updatePictureSpriteStatic" and "updatePictureSpriteNoStatic" (only examples, name them however you want). This makes your code easier to organize and also makes it easier to extend the class from another plugin, if it's make public.
 
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
True lol I think it's would be better to split it in two or 3 for avoid super long method...

Well Personally I think long method are not alway's easy to read honestly and need organisation haha
 

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
Each on of the inner if statements can be a method.
 

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

Latest Threads

Latest Posts

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,847
Messages
1,016,968
Members
137,561
Latest member
JaCrispy85
Top