Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-30-2007, 12:48 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Post Clean Coding - Common Sense

Hello everyone. I'm Chris Zakuto, I script a ton in GS2. Whenever I go onto another server, I find the owners begging me to fix something, so I go in to fix it, and oh no! I see messy code, which happens to give me a headache.

I'll explain how to make code easier to read by yourself, and easier to read and edit by others aswell.

Part I -- Spaces
This is one of the simplest things to do, and it's very easy to get used to. Just add spaces! For example, let's say we're setting a variable.
PHP Code:
x=screenwidth/2-(width/2); 
That's okay, works the same, but it much cleaner if you do it like this.
PHP Code:
screenwidth - ( width ); 
All I did was add a space after the variable name (x), a space after the equals, a space after the screenwidth, a space after the division sign, etc.
Very simple and very easy to do. Try it out in your next script and see how much better it looks.
Another thing I do is put spaces before and after parenthesis "(" and ")". Check these out.
PHP Code:
function onCreated(){
if(var==
true){
dosomething("bob","fish","toothpaste");
}

Fix it by adding spaces after the parenthesis and after a comma. (and also after the if)
PHP Code:
function onCreated()
{
if ( var == 
true )
{
dosomething"bob""fish""toothpaste" );
}

Much cleaner (would look even better with formatting).

Part II - Separate Lines
No, this actually makes your script look better. It's not just some way to increase line count (which doesn't actually matter at all...)
For each brace "{" and "}" give it it's own line. This looks much better, and is a huge step closer to clean coding. For example
PHP Code:
if (true){
milkacow();}else{
toobad();} 
That's much too crowded.
PHP Code:
if ( true )
{
milkacow();
}
else
{
toobad();

More lines, but that doesn't matter. Much cleaner code, especially in longer scripts.
Another thing I hate is an event that has a single line of code and has no brackets.
PHP Code:
function onCreated()godosomething(); 
Can be corrected easily
PHP Code:
function onCreated()
{
  
godosomething();

Part III - Formatting
Now that you have everything else down, this is probably the most important part, and all it takes is the tab button every once in a while. Take a look at this code.
PHP Code:
function onCreated()
{
if (
true )
{
if ( 
godosomething() == )
{
dosomethingelse();
}
}

Eww, ugly. Check this out.
PHP Code:
function onCreated()
{
  if ( 
true )
  {
    if ( 
godosomething() == )
    {
      
dosomethingelse();
    }
  }

That's the last time you'll ever be caught searching for where you forgot to put that end brace! All it takes is a tab. Whenever you create a new opening brace, immediatly make the closing brace. I always do that, and it always works for me. I never have too many/too few braces in my programs.

Part IV - Naming Functions
This is really just something that makes your code look nicer. I've used several examples of functions here, such as dosomethingelse(), godosomething(), and milkacow(). These are a little ugly, but are incredibly easy to fix.
What you do is take the first word of a function (I'll use the function godosomethingelse()), and then add the rest of the words with their first capitalized. For example, that godosomethingelse() can be split into:
go do something else
now change it to this
go Do Something Else
then put them together and you have
goDoSomethingElse();
Much nicer.

Hope you all enjoyed this guide. Leave comments as you wish, but please don't flame.
Chris Zakuto
__________________
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 05:32 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.