Post: Quick and easy ways to clean up your script.
10-19-2014, 07:51 PM #1
Taylor
Former Black Knight.
(adsbygoogle = window.adsbygoogle || []).push({});
Hello Gsc Fanatics

I wrote this thread because I was sick of seeing poorly written functions being posted each day. They are sloppy and bad to look at.




  1. Toggles


    • Cluttered way.
          
      godmode_toggle()
      {
      if(self.godmode == false)
      {
      self EnableInvulnerability();
      self iPrintln("Godmode Enabled");
      self.godmode = true;
      }
      else
      {
      self DisableInvulnerability();
      self iPrintln("Godmode Disabled");
      self.godmode = false;
      }
      }



    • Clean way. You must login or register to view this content.
          
      godmode_toggle()
      {
      if(!isDefined(self.isGod) || !self.isGod)
      self EnableInvulnerability();
      else
      self DisableInvulnerability();

      self.isGod = !self.isGod;
      self iPrintLn("Godmode " + self.isGod ? "Enabled" : "Disabled");
      }

      Explanation: I use this method myself and it is overall better. It may look like more code however the toggle function can be re-used with multiple functions. So instead of writing the same two lines over and over again you have one function that you call that does it for you. As well as the toggle function itself being smaller.


  2. If statements and for loops.


    • Cluttered way.
          
      if(self.menuOpen == true)
      {
      for(i = 0; i < level.players.size; i++)
      {
      level.players[i] iPrintln("Menu is open");
      }
      }

    • Clean way. You must login or register to view this content.
          
      if(self.menuOpen)
      {
      for(i = 0; i < level.players.size; i++)
      level.players[i] iPrintln("Menu is open");
      }


      OR

          
      if(self.menuOpen)
      self iPrintln("Menu is open");

      Explanation: When using if statements and for loops, you do not need to add braces to your code IF AND ONLY IF the code that you would put between the braces is one line of code. Also you don't need self.menuOpen == true, self.menuOpen by itself works just as fine. If will read the variable as true because if(self.menuOpen) is like saying if(true). Likewise with variables that are false. You don't need self.menuOpen == false, if(!self.menuOpen) will read the variable as false.


  3. Function calling


    • Cluttered way.
          
      self thread function();

      function()
      {
      self iPrintln("I am NOT a looped function");
      }

    • Clean way. You must login or register to view this content.
          
      self function();

      function()
      {
      self iPrintln("I am NOT a looped function");
      }


      OR

          
      function();

      function()
      {
      self iPrintln("I am NOT a looped function");
      }

      Explanation: When a function does NOT contain a CONTINUOUS for or while loop, then you do NOT need to call it as a thread. You can just call the function normally on an entity you wish to call it on. However, in the last example that is clean I bet you noticed I did not define an entity. In this case, it is because if I call a function without an entity definition, it defaults to the entity that the scope is being called on.




If you would like for me to touch base on any other ways to optimize your code then please leave me a reply stating what you want an explanation for.

Thanks for reading!
- Taylor

[/font]
Last edited by Taylor ; 10-21-2014 at 12:12 PM.

The following 22 users say thank you to Taylor for this useful post:

-Numb, Chris, Cien, DJ_KILLA1337, DoraTheKiller97, Exelo, Father Luckeyy, Full-Evil, Geo, HiddenHour, ikilzu, iRnZ, KranK, Magnate13Snake, MCabCon, seb5594, ShutTheCrunchUp, SyGnUs, ViRuzModzHD, Fatality, xK ELITE GaminG, Zambie
10-20-2014, 12:59 AM #11
Chris
Former Staff
Originally posted by Temp View Post
the first toggle looks better to look at than the "clean" way stare


But with the clean way, it's easy for others to understand the coding because it's simply stated right there in the code. If someone can't read your code; then there's a problem.
10-20-2014, 02:11 AM #12
Originally posted by Taylor View Post
OP


shorten functions:

    TaylorIsL33T (text)
{
self iPrintlnBold(text) ; //remove the space
}


Just something else you could add Winky Winky
10-20-2014, 02:36 AM #13
Taylor
Former Black Knight.
Originally posted by primetime43 View Post
If you know how to properly code, you don't need this. stare


For the people that don't, they need this. :p
10-20-2014, 02:38 AM #14
Taylor
Former Black Knight.
Originally posted by Temp View Post
the first toggle looks better to look at than the "clean" way stare


Originally posted by Nat
But with the clean way, it's easy for others to understand the coding because it's simply stated right there in the code. If someone can't read your code; then there's a problem.


Read the explanation for the clean way. If you look it explains that the function "toggle" that takes the two parameters can be used on multiple functions. Thus limiting how many times you repeat code.
10-20-2014, 02:39 AM #15
Taylor
Former Black Knight.
Originally posted by ViiPeR View Post
shorten functions:

    TaylorIsL33T (text)
{
self iPrintlnBold(text) ; //remove the space
}


Just something else you could add Winky Winky


Already made a function that is similar. :p It parses the string to see if there is a color in it, if not it adds a random color. :p

    
printf(str)
{
colors = strTok("0;1;2;3;4;5;6", ";");
finalString = "";
randomColor = RandomInt(6);

for(i = 0; i < str.size; i++)
{
for(x = 0; x < colors.size; x++)
if(str[i] == "^" && str[i+1] == colors[x])
break;
else
finalString = str;
}

if(finalString != "")
self iPrintln("^"+randomColor+finalString);
else
self iPrintln(str);

}

The following user thanked Taylor for this useful post:

Uk_ViiPeR
10-20-2014, 02:50 AM #16
primetime43
Knowledge is power Tiphat
Originally posted by Taylor View Post
For the people that don't, they need this. :p


Well then they are most likely copying and pasting if they don't know how to.
Therefore they shouldn't be coding. :p

The following user thanked primetime43 for this useful post:

TheFallen
10-20-2014, 07:04 AM #17
Shark
Retired.
Originally posted by Taylor View Post
Hello Gsc Fanatics

I wrote this thread because I was sick of seeing poorly written functions being posted each day. They are sloppy and bad to look at.




  1. Toggles


    • Cluttered way.
          
      godmode_toggle()
      {
      if(self.godmode == false)
      {
      self EnableInvulnerability();
      self iPrintln("Godmode Enabled");
      self.godmode = true;
      }
      else
      {
      self DisableInvulnerability();
      self iPrintln("Godmode Disabled");
      self.godmode = false;
      }
      }



    • Clean way. You must login or register to view this content.
          
      godmode_toggle()
      {
      if(!isDefined(self.isGod))
      self EnableInvulnerability();
      else
      self DisableInvulnerability();

      toggle("Godmode", self.isGod);
      }
      toggle(text, var)
      {
      if(!isDefined(var))
      {
      var = true;
      self iPrintln(text+" Enabled");
      }
      else
      {
      var = undefined;
      self iPrintln(text+" Disabled");
      }
      }

      Explanation: I use this method myself and it is overall better. It may look like more code however the toggle function can be re-used with multiple functions. So instead of writing the same two lines over and over again you have one function that you call that does it for you. As well as the toggle function itself being smaller.


  2. If statements and for loops.


    • Cluttered way.
          
      if(self.menuOpen == true)
      {
      for(i = 0; i < level.players.size; i++)
      {
      level.players[i] iPrintln("Menu is open");
      }
      }

    • Clean way. You must login or register to view this content.
          
      if(self.menuOpen)
      {
      for(i = 0; i < level.players.size; i++)
      level.players[i] iPrintln("Menu is open");
      }


      OR

          
      if(self.menuOpen)
      self iPrintln("Menu is open");

      Explanation: When using if statements and for loops, you do not need to add braces to your code IF AND ONLY IF the code that you would put between the braces is one line of code. Also you don't need self.menuOpen == true, self.menuOpen by itself works just as fine. If will read the variable as true because if(self.menuOpen) is like saying if(true). Likewise with variables that are false. You don't need self.menuOpen == false, if(!self.menuOpen) will read the variable as false.


  3. Function calling


    • Cluttered way.
          
      self thread function();

      function()
      {
      self iPrintln("I am NOT a looped function");
      }

    • Clean way. You must login or register to view this content.
          
      self function();

      function()
      {
      self iPrintln("I am NOT a looped function");
      }


      OR

          
      function();

      function()
      {
      self iPrintln("I am NOT a looped function");
      }

      Explanation: When a function does NOT contain a CONTINUOUS for or while loop, then you do NOT need to call it as a thread. You can just call the function normally on an entity you wish to call it on. However, in the last example that is clean I bet you noticed I did not define an entity. In this case, it is because if I call a function without an entity definition, it defaults to the entity that the scope is being called on.




If you would like for me to touch base on any other ways to optimize your code then please leave me a reply stating what you want an explanation for.

Thanks for reading!
- Taylor

[/font]


why does this get mentioned in the ngu shoutbox and not my thread, it includes how to code neatly and more Sad Awesome
10-20-2014, 10:31 AM #18
Taylor
Former Black Knight.
Originally posted by Shark View Post
why does this get mentioned in the ngu shoutbox and not my thread, it includes how to code neatly and more Sad Awesome


To be fair, you don't know clean code that well. You make good stuff, but do a lot of unneeded things. :p

The following user thanked Taylor for this useful post:

TheFallen
10-20-2014, 10:31 AM #19
Taylor
Former Black Knight.
Originally posted by primetime43 View Post
Well then they are most likely copying and pasting if they don't know how to.
Therefore they shouldn't be coding. :p


Well, you of all people know how the community is anymore. tears It's kind of sad actually.

The following 2 users say thank you to Taylor for this useful post:

TheFallen, Tipton_Modz

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo