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, 10:55 AM #20
Shark
Retired.
Originally posted by Taylor View Post
To be fair, you don't know clean code that well. You make good stuff, but do a lot of unneeded things. :p


unneeded things? such as?

everything in my code must have a purpose, if not why else would I have it tears
also we all have our different coding styles I suppose...

The following user thanked Shark for this useful post:

ItzMatriix
10-20-2014, 11:18 AM #21
Taylor
Former Black Knight.
Originally posted by Shark View Post
unneeded things? such as?

everything in my code must have a purpose, if not why else would I have it tears
also we all have our different coding styles I suppose...


Well, because you write a function that can be done in 2 lines or something. :p
10-20-2014, 01:28 PM #22
seb5594
Proud Former Admin
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]


Thanks for sharing this but that's more your way of coding anything clean.
Anyone is having a different taste of clean coding Happy
I like the way of your toggles ^^

The following user thanked seb5594 for this useful post:

TheFallen
10-20-2014, 02:09 PM #23
Taylor
Former Black Knight.
Originally posted by seb5594 View Post
Thanks for sharing this but that's more your way of coding anything clean.
Anyone is having a different taste of clean coding Happy
I like the way of your toggles ^^


I know there are more. :p However this is a good and easy way to organize it.
10-20-2014, 11:03 PM #24
forflah123
Who’s Jim Erased?
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]



that is still very messy
10-21-2014, 01:45 AM #25
Taylor
Former Black Knight.
Originally posted by forflah123 View Post
that is still very messy


It's less code in the long run. Since you can re-use a lot of it.
10-21-2014, 02:44 AM #26
TheFallen
Former Dark Night
Originally posted by Taylor View Post
It's less code in the long run. Since you can re-use a lot of it.


It's not always about less code though, it's about efficiency and readability.
10-21-2014, 08:47 AM #27
Originally posted by TheFallen View Post
It's not always about less code though, it's about efficiency and readability.


I would agree with you, except one thing. no one can really decide what pure clean code is.. people all have their own way of coding. I wouldn't recommend this, as I still prefer the normal coding way I use aka "Cluttered way", but that's my style of coding.
10-21-2014, 10:12 AM #28
dtx12
Do a barrel roll!
Originally posted by ScaRzModZ View Post
I would agree with you, except one thing. no one can really decide what pure clean code is.. people all have their own way of coding. I wouldn't recommend this, as I still prefer the normal coding way I use aka "Cluttered way", but that's my style of coding.


In any case GSC has support only for shitty coding style(procedural), it doesn't support OOP by design.
Also as for first post, sometimes I do things using another way:
    
godmode_toggle()
{
if(!self.isGod)
self EnableInvulnerability();
else
self DisableInvulnerability();
self.isGod = !self.isGod;
self toggle("Godmode", self.isGod);
}
toggle(text, isEnabled)
{
self iPrintLn(text + isEnabled ? " Enabled" : " Disabled");
}
Last edited by dtx12 ; 10-21-2014 at 10:22 AM.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo