Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #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
  #2  
Old 03-30-2007, 12:56 AM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Pretty much reinforcing some of what I've written in http://forums.graalonline.com/forums...ad.php?t=61805, there are a few more guidelines that I have previously written there to help keep code clean too.

Nice work, though.
__________________
Skyld
Reply With Quote
  #3  
Old 03-30-2007, 01:04 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
Quote:
Originally Posted by Skyld View Post
Pretty much reinforcing some of what I've written in http://forums.graalonline.com/forums...ad.php?t=61805, there are a few more guidelines that I have previously written there to help keep code clean too.

Nice work, though.
Thanks, if I'd have seen that I wouldn't have posted this ... though you did post that in 2005, so unless you're searching for it you wouldn't find it.
__________________
Reply With Quote
  #4  
Old 03-30-2007, 01:28 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
The only problem I have with adding spacing just for personal looks is the fact I hate adding all that width and having to scroll horizontally.
Reply With Quote
  #5  
Old 03-30-2007, 01:30 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
Quote:
Originally Posted by DustyPorViva View Post
The only problem I have with adding spacing just for personal looks is the fact I hate adding all that width and having to scroll horizontally.
If you maximize the screen there are few times you have to do this ... most things can be split up.

PHP Code:
temp.blah = {
"test",
"test2",
"test3"
}; 
PHP Code:
if ( true &&
cats dogs &&
player.account == "cbk1994" )

__________________
Reply With Quote
  #6  
Old 03-30-2007, 02:03 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Ya, I do it mainly with arrays, but I hate doing it with if statements.
Reply With Quote
  #7  
Old 03-30-2007, 04:50 AM
Falcor Falcor is offline
Darth Cucumber
Falcor's Avatar
Join Date: Mar 2001
Location: At School
Posts: 2,874
Falcor is on a distinguished road
Send a message via ICQ to Falcor Send a message via AIM to Falcor Send a message via MSN to Falcor Send a message via Yahoo to Falcor
Real coders just do it however they like it then run their code through a beautifier when they are done

see emacs/vim
__________________

subliminal message: 1+1=3
Reply With Quote
  #8  
Old 03-30-2007, 06:57 AM
Kristi Kristi is offline
Bowie's Deciple
Kristi's Avatar
Join Date: Dec 2003
Location: Boston, MA
Posts: 748
Kristi has a spectacular aura aboutKristi has a spectacular aura about
Send a message via AIM to Kristi Send a message via MSN to Kristi
Umm what?
PHP Code:
  function whatever(hi,hi) {
     if(
this==that) {
        
blah;
        
blah
        
if(true)
           
cool;
     } elseif(
yourmom) {
        
stuff;
        
morestuff;
     } else 
somethingcoolhere;
  } 
Looks way nicer then

PHP Code:
function whatever(hi,hi)
{
  if (
this==that
  {
    
blah;
    
blah
    
if(true)
      
cool;
  }
  elseif (
yourmom
  {
    
stuff;
    
morestuff;
  } 
  else
  {
    
somethingcoolhere;
  }

But that is a preference thing. I personally think what you say is better looks more obnoxious.

also, you space too much, you can use spacing to help visualize order of operations. and padding the conditional statements is a waste.

PHP Code:
something/somethingelse*3;
something y^3;
= ( (x1-x2)^+ (y1-y2)^)^.5;
if(
trueis better then if( true )
if(
== yis better then if( == 
also, single line functions are okay as wrappers. Declare them at the top, looks nice.
PHP Code:
//dont know why you would ever need to wrap showimg but an example
function onShowImg(ind,img,ix,iyshowimg(ind,img,ix,iy);

function 
whatever() {
  
stuffhappens;
  
scheduleEvent(5,"ShowImg",201,"possums.png",x+2,y+1);

And i am going to agree with dusty on multilining if statements. if it needs to be that obnoxious, store booleans before the comparision.
__________________
Reply With Quote
  #9  
Old 03-30-2007, 07:07 AM
killerogue killerogue is offline
Registered Omega
killerogue's Avatar
Join Date: Apr 2006
Location: United States
Posts: 1,920
killerogue is on a distinguished road
Send a message via AIM to killerogue Send a message via MSN to killerogue
Quote:
Originally Posted by Kristi View Post
stuff

Yeah, Hell, it really is based all off preference. For me it's more of a mood thing, sometimes I think padding looks nice, but no padding is more compact and a bit cleaner.
__________________


REMEMBER, IF YOU REP ME, LEAVE A NAME!

Quote:
Originally Posted by haunter View Post
Graal admins don't die. They go to hell and regroup.
Quote:
Originally Posted by Inverness View Post
Without scripters, your graphics and levels wouldn't do anything but sit there and look pretty.
Reply With Quote
  #10  
Old 03-30-2007, 08:29 AM
Chandler Chandler is offline
Banned
Join Date: Jan 2007
Posts: 656
Chandler will become famous soon enough
Style like a legend:
PHP Code:
function onCreated()
  
this.doMode();
function 
doMode(whichMode)
  {
  if (
temp.whichMode)
    
this.hasFound true;
   else
    
this.hasFound false;
  } 
Reply With Quote
  #11  
Old 03-30-2007, 08:43 AM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by Chandler View Post
Style like a legend:
PHP Code:
function onCreated()
  
this.doMode();
function 
doMode(whichMode)
  {
  if (
temp.whichMode)
    
this.hasFound true;
   else
    
this.hasFound false;
  } 
No. Ideally you should not be omitting brackets.
__________________
Skyld
Reply With Quote
  #12  
Old 03-30-2007, 09:56 AM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
I prefer my way.


PHP Code:
function whatever(hihi)
{
  if(
this==that) {
    
blah;
    
blah
    
if(truecool;
  }
  else if(
yourmom) {
    
stuff;
    
morestuff;
  } else 
somethingcoolhere;

__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #13  
Old 03-30-2007, 11:04 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Lol that's a mix of styles
Reply With Quote
  #14  
Old 03-30-2007, 12:08 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
I follow these two simple things:

* New line for bracket ONLY at new functions
* Spacing in variables (such as: var = pie + (1*4)), and in function paramaters (such as blah(p1, p2, p3))
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #15  
Old 03-30-2007, 04:41 PM
Grey Grey is offline
Classic Developer
Grey's Avatar
Join Date: Mar 2007
Location: Wales, UK
Posts: 134
Grey will become famous soon enough
Send a message via AIM to Grey
Quote:
Originally Posted by xXziroXx View Post
I prefer my way.

PHP Code:
  if(this==that) {
    
blah;
    
blah //zomg theoretical error
    
if(truecool;
  } 
With functions I always use brackets, with statements I tend to omit them if there is only one command by force of habit, but I do usually indent it still and move it to the next line. If there is an else following the if however I always use brackets. Personally I just find it looks a lot nicer that way. As far as spacing goes I agree with HR.

PHP Code:
function checkStyle() {
  if (
this.style == true) {
    
increaseAwesome();
  } else {
    
decreaseAwesome();
  }
  if (
noelse == true)
    
omit true;

Reply With Quote
  #16  
Old 03-30-2007, 06:32 PM
Chandler Chandler is offline
Banned
Join Date: Jan 2007
Posts: 656
Chandler will become famous soon enough
Quote:
Originally Posted by Skyld View Post
No. Ideally you should not be omitting brackets.
One to their own I suppose
Reply With Quote
  #17  
Old 03-30-2007, 06:41 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by Grey View Post
With functions I always use brackets, with statements I tend to omit them if there is only one command by force of habit, but I do usually indent it still and move it to the next line. If there is an else following the if however I always use brackets. Personally I just find it looks a lot nicer that way. As far as spacing goes I agree with HR.

PHP Code:
function checkStyle() {
  if (
this.style == true) {
    
increaseAwesome();
  } else {
    
decreaseAwesome();
  }
  if (
noelse == true)
    
omit true;

PHP Code:
function checkStyle()
{
  if (
this.style)
  {
    
this.increaseAwesome();
  }
    else
  {
    
this.decreaseAwesome();
  }

  if (
this.variable)
  {
    
this.otherVariable true;
  }

Quote:
Originally Posted by Chandler
One to their own I suppose
I guess, however I promote clean styling for a reason: to promote editability, understandability and just overall cleanliness. Leaving brackets out is hardly efficient; say you want to add something later into your conditional code block, you'll then need to add brackets which you could have just done to start with!
__________________
Skyld
Reply With Quote
  #18  
Old 03-30-2007, 07:31 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by Skyld View Post
Leaving brackets out is hardly efficient; say you want to add something later into your conditional code block, you'll then need to add brackets which you could have just done to start with!
Row count ++
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #19  
Old 03-30-2007, 07:49 PM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Quote:
Originally Posted by xXziroXx View Post
Row count ++

legibility ++
Reply With Quote
  #20  
Old 03-30-2007, 09:01 PM
godofwarares godofwarares is offline
Webmaster
godofwarares's Avatar
Join Date: Dec 2006
Location: Florida
Posts: 552
godofwarares is on a distinguished road
Send a message via ICQ to godofwarares Send a message via AIM to godofwarares Send a message via MSN to godofwarares Send a message via Yahoo to godofwarares
Bleh, I code like this:

PHP Code:
function doSomething()
{
     
stuff();
     
moreStuff();
     
     if (
== 2)
     {
          
doEvenMoreStuff();
     } else {
          
ohWell();
     }

__________________
What signature? I see no signature?
Reply With Quote
  #21  
Old 03-30-2007, 11:19 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Heres a sample of my coding, I'm extremely picky about the formatting.
PHP Code:
public function loadRefs(obj) {
  
temp.i;
  
temp.e;
  
temp.vars;
  
temp.count;
  
temp.ref;
  
  
vars obj.getVarNames();
  
count 0;
  for (
0vars.size(); ++) {
    if (
obj.(@ vars[i]).type() == 1) {
      if (
MudControl.isRefStr(obj.(@ vars[i]))) {
        
ref obj.(@ vars[i]);
        
obj.(@ vars[i]) = MudControl.parseRef(ref);
        
obj.(@ vars[i]).addRef(obj);
        
count ++;
      }
    }
    else if (
obj.(@ vars[i]).type() == 3) {
      for (
0obj.(@ vars[i]).size(); ++) {
        if (
obj.(@ vars[i])[e].type() == 1) {
          if (
MudControl.isRefStr(obj.(@ vars[i])[e])) {
            
ref obj.(@ vars[i])[e];
            
obj.(@ vars[i])[e] = MudControl.parseRef(ref);
            
obj.(@ vars[i])[e].addRef(obj);
            
count ++;
          }
        }
      }
    }
  }
  echo(
format("References Loaded (%s): %s %s"countobj.mudtypeobj.mudid));

__________________
Reply With Quote
  #22  
Old 03-30-2007, 11:20 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by Inverness View Post
Heres a sample of my coding, I'm extremely picky about the formatting.
PHP Code:
public function loadRefs(obj) {
  
temp.i;
  
temp.e;
  
temp.vars;
  
temp.count;
  
temp.ref;
  
  
vars obj.getVarNames();
  
count 0;
  for (
0vars.size(); ++) {
    if (
obj.(@ vars[i]).type() == 1) {
      if (
this.isRefStr(obj.(@ vars[i]))) {
        
ref obj.(@ vars[i]);
        
obj.(@ vars[i]) = parseRef(ref);
        
obj.(@ vars[i]).addRef(obj);
        
count ++;
      }
    }
    else if (
obj.(@ vars[i]).type() == 3) {
      for (
0obj.(@ vars[i]).size(); ++) {
        if (
obj.(@ vars[i])[e].type() == 1) {
          if (
this.isRefStr(obj.(@ vars[i])[e])) {
            
ref obj.(@ vars[i])[e];
            
obj.(@ vars[i])[e] = parseRef(ref);
            
obj.(@ vars[i])[e].addRef(obj);
            
count ++;
          }
        }
      }
    }
  }
  echo(
format("References Loaded (%s): %s %s"countobj.mudtypeobj.mudid));

Your styling is practically the same as mine
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #23  
Old 03-30-2007, 11:21 PM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
A general guideline is to keep the formatting as similar to the rest of the project as possible. It might be a good idea to write a general guideline that all developers abide to.
Here's how the Angband source code is styled:
PHP Code:
written by Robert RuehlmannBen Harrison, and Gwidon SNaskrent

There are lots of things that are commonly considered good style 
for Angband codeThe Vanilla Angband source code is the general guideline.

Here are some of the "rules":

    * 
Don't use floating point calculations.
    * Don'
break savefile compatibility (if not absolutely necessary).
    * 
No C++ codeThat also means no '//' comments.
    * 
Put system dependent code between #ifdef xyz ... #endif /* xyz */ .
    
No "magic numbers". Use #defines. The #defines should be in defines.h rather than the source file, unless they're very local (such as dungeon generation parameters). 

And some code-style guidelines:

    * 
CommentscommentscommentsMulti-line comments should look like:

      
/*
       * A multi-line
       * comment.
       */

      
instead of:

      
/* A multi-line
       * comment */

    
Indentation with tabsBut generally avoid getting lines over 80 characters.
    * 
Put curly braces ('{''}'on an extra line with the same indentation level as the previous lineExample:

      if (
value == 0)
      {
          
do_something();
      }

    * 
Put empty lines between code-blocksExample:

      
/* Do something */
      
do_something();

      
/* Do something else */
      
do_something_else();

    * 
Spaces around the mathematicalcomparison, and assignment operators ('+''-''*''/''=''!=''==''>', ...).
    * 
Spaces between C-identifiers like 'if''while''for', and 'return' and the opening brackets ('if (foo)''while (bar)', ...).
    * 
No spaces between function names and brackets and between brackets and function arguments (function(12instead of function ( 1)).
    * If 
the precedence of operations is ambiguous then put brackets around themAlso try to insert parentheses whenever necessary for readabilityegaround (foo RF3_SOME_DEFINE). There is usually no ambiguity and no macro resolutionbut this is an aesthetic detail.
    * If 
function takes no arguments then it should be declared with a void argument list. Exampleint foo(voidinstead of int foo().
    * Function 
declaration and definition should look the same.
    * 
Don't assume that functions declared without a return type return int. Specify the type explicitly. Example: int foo(void) instead of foo(void). 
Reply With Quote
  #24  
Old 03-30-2007, 11:22 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by xXziroXx View Post
Your styling is practically the same as mine


Note how I use temp variables. It is something I began doing only recently and it makes the script look much more organized rather than using temp. repeatedly before all the temp variable names.
__________________
Reply With Quote
  #25  
Old 03-30-2007, 11:25 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by Inverness View Post


Note how I use temp variables. It is something I began doing only recently and it makes the script look much more organized rather than using temp. repeatedly before all the temp variable names.
I use temp variables all the time too!
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #26  
Old 03-30-2007, 11:26 PM
Rapidwolve Rapidwolve is offline
Registered User
Join Date: Jul 2006
Posts: 1,241
Rapidwolve is an unknown quantity at this point
What does
PHP Code:
prefix.varname
do? Is there really a point to it? does it declare it or something
Reply With Quote
  #27  
Old 03-30-2007, 11:28 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Rapidwolve View Post
What does
PHP Code:
prefix.varname
do? Is there really a point to it? does it declare it or something
Yes, I use that for temp variables only, it declares the variable as a temp so it can be referenced without the use of temp.

When you have variables defined in function parameters they're declared as temp variables and you're able to reference them with or without the temp. before the variable name, this is the same idea.

I place all the temp declarations at the beginning so I know what all I'm using and can make notes for other people using it.
__________________
Reply With Quote
  #28  
Old 03-30-2007, 11:31 PM
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
Quote:
Originally Posted by Rapidwolve View Post
What does
PHP Code:
prefix.varname
do? Is there really a point to it? does it declare it or something
Declares the variable, so it's value is "" not NULL.
__________________
Reply With Quote
  #29  
Old 03-30-2007, 11:40 PM
Kristi Kristi is offline
Bowie's Deciple
Kristi's Avatar
Join Date: Dec 2003
Location: Boston, MA
Posts: 748
Kristi has a spectacular aura aboutKristi has a spectacular aura about
Send a message via AIM to Kristi Send a message via MSN to Kristi
Quote:
Originally Posted by Inverness View Post


Note how I use temp variables. It is something I began doing only recently and it makes the script look much more organized rather than using temp. repeatedly before all the temp variable names.
Um, you can't do that, when you say vars = something, you're actually assinging it to vars, not temp.vars
PHP Code:
function onCreated() {
  
temp.hello;
  
hello 2;
  
sendtorc("TEST:" temp.hello);
  
sendtorc("TEST2:" hello);

Yields the following:
The best idler is the (Server): TEST:
The best idler is the (Server): TEST2:2

note that temp.hello is still null. The only way to drop the temp is if the var was passed as a paremeter, it will take presedence over an already global var.

On another note, ziro, grey, and inver all have good styling imho
__________________
Reply With Quote
  #30  
Old 03-30-2007, 11:42 PM
Riot Riot is offline
Delteria Management
Join Date: Nov 2003
Location: Seminole County, Florida
Posts: 280
Riot is on a distinguished road
Quote:
Originally Posted by Kristi View Post
Um, you can't do that, when you say vars = something, you're actually assinging it to vars, not test vars

PHP Code:
function onCreated() {
  
temp.hello;
  
hello 2;
  
sendtorc("TEST:" temp.hello);
  
sendtorc("TEST2:" hello);

Yields the following:
The best idler is the (Server): TEST:
The best idler is the (Server): TEST2:2

note that temp.hello is still null.

On another note, ziro, grey, and inver all have good styling imho
I was about to comment on this too:
PHP Code:
function onCreated() {
  
temp.i;
  
  
34;
  echo(
"onCreated(): i =" SPC i SPC "temp.i=" SPC temp.i);
  
test();
  
  
this.setTimer(1);
}

function 
onTimeout() {
  echo(
"onTimeout(): i =" SPC i);
}

function 
test() {
  echo(
"test(): i =" SPC i);

Returns:
HTML Code:
onCreated(): i = 34 temp.i= 
test(): i = 34
onTimeout(): i = 34

Last edited by Riot; 03-30-2007 at 11:43 PM.. Reason: stupid word filter
Reply With Quote
  #31  
Old 03-30-2007, 11:46 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Ah, no wonder I had that array problem that time, oh well, it can be easily be fixed.
PHP Code:
function onCreated() {
  
temp.rawr 0;
  
rawr 3;
  echo(
"1:" rawr);
  echo(
"2:" temp.rawr);

Returns 3 in both cases, problem solved.

Edit:
It doesn't look pretty anymore though
__________________
Reply With Quote
  #32  
Old 03-30-2007, 11:52 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by Inverness View Post
Ah, no wonder I had that array problem that time, oh well, it can be easily be fixed.
PHP Code:
function onCreated() {
  
temp.rawr 0;
  
rawr 3;
  echo(
"1:" rawr);
  echo(
"2:" temp.rawr);

Returns 3 in both cases, problem solved.

Edit:
It doesn't look pretty anymore though
I'm not sure why you are trying to manage the variables anyway. The engine's meant to do that for you, and if your functions are structured sensibly, it should be obvious which variable names you are using.
__________________
Skyld
Reply With Quote
  #33  
Old 03-30-2007, 11:53 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Uh, I do this for the simple fact that I'm damn tired of typing temp. before every variable name, no other reason. I also like it better without temp., its more pretty like that. Also shortens the code in the long run.
__________________
Reply With Quote
  #34  
Old 03-30-2007, 11:54 PM
napo_p2p napo_p2p is offline
oh snaps
napo_p2p's Avatar
Join Date: Sep 2003
Location: Pismo Beach, California
Posts: 2,118
napo_p2p has a spectacular aura aboutnapo_p2p has a spectacular aura about
Send a message via AIM to napo_p2p Send a message via MSN to napo_p2p
Quote:
Originally Posted by Skyld View Post
I'm not sure why you are trying to manage the variables anyway. The engine's meant to do that for you, and if your functions are structured sensibly, it should be obvious which variable names you are using.
I think it's more of a styling thing. So he doesn't have to write temp. all of the time.

EDIT:
Ding Ding Ding!
__________________
Scito hoc super omnia.
Haec vita est tua una sola.
Dum vita superest, utere maxime quoque puncto, momento, et hora quae habes.
Tempus neminem non manet.
Noli manere tempus.
Carpe Diem

Seize the Day.
Reply With Quote
  #35  
Old 03-30-2007, 11:54 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by Inverness View Post
Uh, I do this for the simple fact that I'm damn tired of typing temp. before every variable name, no other reason. I also like it better without temp., its more pretty like that.
Hm, in my opinion, it is better to always prefix variables. That way, there is no confusion as to which scope you are working in.

I try to write my scripts for people who have to work with them as well as for myself.
__________________
Skyld
Reply With Quote
  #36  
Old 03-30-2007, 11:55 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Skyld View Post
Hm, in my opinion, it is better to always prefix variables. That way, there is no confusion as to which scope you are working in.

I try to write my scripts for people who have to work with them as well as for myself.
For myself, I only do this no-prefixing thing for temp variables, like how local variables in Java usually go without a prefix. So there is no confusion for myself. But also, all the temp variables are declared at the beginning so you know them already.

Edit:
I would like it if un-prefixed variables defaulted to temp. rather than a global if no previous temp. or this. variables were found.
__________________
Reply With Quote
  #37  
Old 03-30-2007, 11:59 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by Inverness View Post
For myself, I only do this no-prefixing thing for temp variables, like how local variables in Java usually go without a prefix. So there is no confusion for myself. But also, all the temp variables are declared at the beginning so you know them already.
My point is not so much whether it's solely clear to you, but also to other people who are reading the script who might not be as good at scripting as you or I. A lot of people have difficulty getting their heads around variable declarations, and even more people have difficulty identifying differences between scopes. If everything temporary is prefixed, there's no confusion. If you list things as temp. and then don't use temp. in the rest of the function, that seems slightly misleading. :|

There's more to styling than whether it looks pretty or not; it's about catering for other people who may wish to edit the script or read it. It's for that reason that we prefer people to style scripts before posting asking for help with them here.
__________________
Skyld
Reply With Quote
  #38  
Old 03-31-2007, 12:02 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
You have a good point.
Though on Aeon, Napo and myself are the only major scripters and hes easily skilled enough to not be confused in any way.
Though I suppose when I release some of my stuff to public I will restyle it...
However until then nobody needs to be lookin' at my stuff :P

/sign for un-prefixed variables going temp and not global.
__________________
Reply With Quote
  #39  
Old 03-31-2007, 03:39 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
Clean coding looks prettier anyway
__________________
Reply With Quote
  #40  
Old 03-31-2007, 05:46 AM
Rapidwolve Rapidwolve is offline
Registered User
Join Date: Jul 2006
Posts: 1,241
Rapidwolve is an unknown quantity at this point
I recently have been practicing better organization and clean coding in my scripts, heres a snippet.

PHP Code:
public function Hurt(damageattackerhurtani){
  
temp.conditionsok = ((this.clientr.hp && this.clientr.dead == false && level.nopk == false && this.client.nopk == false) ? true false);
   if (
temp.conditionsok){
       if (
temp.damage => this.clientr.hp) {
        
this.clientr.hp 0;
        
this.Kill(temp.attacker); 
        
this.showHp(7);
        return;
       }
       if (
temp.hurtanithis.setani("hurt"null);
          
clientr.lastattacker temp.attacker;
          
this.clientr.hp -= abs(temp.damage);
          
this.showHp(7);
    }
    return;

Reply With Quote
Reply


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 12:25 AM.


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