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-19-2014, 08:01 PM #2
Worst
TURN DOWN FOR WHAT
Neg. stare
10-19-2014, 08:03 PM #3
SyGnUs
Give a F*** About Your Lifestyle
Nice. I wasn't fully sure on the function calling myself, thanks for clearing that up.

The following user thanked SyGnUs for this useful post:

Taylor
10-19-2014, 08:13 PM #4
Taylor
Former Black Knight.
Originally posted by SyGnUs View Post
Nice. I wasn't fully sure on the function calling myself, thanks for clearing that up.


See? This is why I wrote this. :p

The following user thanked Taylor for this useful post:

SyGnUs
10-19-2014, 08:15 PM #5
Chris
Former Staff
Now that you made this thread, it actually makes things a lot easier for me. I never knew that functions could be cleaned to such an extent. Thanks for the post.
10-19-2014, 08:17 PM #6
Taylor
Former Black Knight.
Originally posted by Nat
Now that you made this thread, it actually makes things a lot easier for me. I never knew that functions could be cleaned to such an extent. Thanks for the post.


Really? You didn't? I have been doing this since cod4. tears
10-19-2014, 08:17 PM #7
SyGnUs
Give a F*** About Your Lifestyle
Originally posted by Taylor View Post
See? This is why I wrote this. :p


I hope other people take notice to this and follow it. I know I have gotten used to using the cluttered toggle, I knew about your way, but never really tried it. Guess time to try it out and implement it
Last edited by SyGnUs ; 10-19-2014 at 08:19 PM. Reason: ...
10-19-2014, 08:19 PM #8
Taylor
Former Black Knight.
Originally posted by SyGnUs View Post
I hope other people take notice to this and follow it. I know I have gotten used to using the cluttered toggle, I knew about your way, but never really tried it. Guess time to try it out and implement it


Me as well, I think code optimization is important.
10-19-2014, 11:15 PM #9
primetime43
Knowledge is power Tiphat
If you know how to properly code, you don't need this. stare

The following 4 users say thank you to primetime43 for this useful post:

ErasedDev, JLM, Rangers 1690, TheFallen
10-19-2014, 11:30 PM #10
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]


the first toggle looks better to look at than the "clean" way stare

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo