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 07-07-2009, 07:26 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
GraalScript2 (GS2) Coding Conventions

GraalScript2 (GS2) Coding Conventions

This is a set of coding conventions and rules for use in GS2 programming. It is an adaptation of Crockford's document, http://javascript.crockford.com/code.html.

Over a script's lifetime, it will be handled by many people, making it very important to clearly communicate its structure and characteristics, making it less likely to break when modified in the never-too-distant future.


Indentation

The unit of indentation is 2 spaces. The code editor in Remote Control (RC) already handles this by changing a tab-stop to 2 spaces automatically.


Line Length

Avoid lines longer than 80 characters. If a statement won't fit on a single line, it may be better to break it up. Place the break after an operator, ideally after a comma. The next line should be indented 4 spaces.


Comments

Be generous with comments. It is useful to leave information that explains how your script works for others (possibly yourself) who will need to understand what you have done.

The comments should be well-written and clear, just like the code they are explaining. Occasional humor might be appreciated as well. Frustrations will not.

It is important comments be kept up-to-date. Incorrect comments can make programs harder to read and understand.

Make comments meaningful. Focus on what is not immediately visible. Don't waste the reader's time with stuff like:
PHP Code:
0// Set i to zero. 
Generally use line comments. Save block comments for formal documentation and for commenting out.


Variable Declarations

Variables should be declared before used. GS2 does not require this, but doing so makes the program easier to read.

The variable declarations should be the first statements in the function body.

It is preferred that each variable be given its own line and comment.
PHP Code:
temp.currentEntry// currently selected table entry
temp.level// current level
temp.size// size of table 
Avoid using global variables. Variables that don't need to be accessed outside of the function should always have a temp. preceding them.

Always use the prefix even after originally declaring the variable for clarity.


Function Declarations
  • There should be no space between the name of a function and the ( of its parameter list.
  • There should be one space between the ) and the { that begins the statement body.
  • The body itself is indented two spaces.
  • The } is aligned with the line containing the beginning of the declaration of the function.
PHP Code:
function doAwesome(ab) {
  
temp.1;
  return(
temp.x+1);

Never use an inner function because it is not scoped and can be accessed outside of the outer function, for example:
PHP Code:
function outer() {
  function 
inner() {
    return(
123);
  }
  
  
outer2();
}

function 
outer2() {
  echo (
inner()); // echos 123

Using an inner function like that just makes things confusing in terms of scope, because it actually becomes a global function.

If you are declaring an anonymous function (closure), there should be one space between the word function and the (.
If the space is left out, then it may seem that the function's name is 'function', which is incorrect.
PHP Code:
temp.distance = function (x1y1x2y2) {
  return( ((
x2 x1)^+ (y2 y1)^2)^.5 );
};
echo(
temp.distance(2211)); 
Anonymous functions can unfortunately not be executed in the same statement they are declared in.


Names

Names should be formed from the 26 upper and lower case letters (A .. Z, a .. z) and the 10 digits (0 .. 9) and _.
Avoid other characters.

Do not use _ as the first character of a name. It is sometimes used to indicate privacy, but it does not actually provide privacy. Avoid conventions that demonstrate a lack of competence.
  • Variables and functions should start with a lower case letter.
  • Constants should be all upper case.
  • Classes should start with an upper case letter

Statements

Simple Statements
Each line should contain at most one statement. Put a ; at the end of every simple statement.
Note that when declaring a variable as a function, it is still an assignment statement and must end with a semicolon.

Compound Statements
These are statements that contain lists of statements enclosed in { }.
  • The enclosed statements should be indented two more spaces.
  • The { should be at the end of the line that begins the compound statement.
  • The } should begin a line and be indented to align with the beginning of the line containing the matching {.
  • Braces should be used around all statements, even single statements, when they are part of a control structure, such as an if or for statement. This makes it easier to add statements without accidentally introducing bugs.

return Statement

return is a statement, not a function, therefore it should not use ( ) around the value.


if Statement

The if class of statements should have the following form:

PHP Code:
if (condition) {
  
statements
}

if (
condition) {
  
statements
} else {
  
statements
}

if (
condition) {
  
statements
} else if (condition) {
  
statements
} else {
  
statements


for Statement

A for class of statements should have the following form:
PHP Code:
for (initializationconditionupdate) {
  
statements
}

for (
variable : array) {
  if (
filter) {
    
statements
  
}

The first form should be used with loops of a predeterminable number of iterations.


while Statement

A while statement should have the following form:
PHP Code:
while (condition) {
  
statements


do Statement

A do statement should have the following form:
PHP Code:
do {
  
statements
} while (condition); 
Unlike the other compound statements, the do statement always ends with a ;.


switch Statement

A switch statement should have the following form:
PHP Code:
switch (expression) {
case 
expression:
  
statements
default:
  
statements

Each case is aligned with the switch. This avoids over-indentation.

Each group of statements (except the default) should end with break or return. Do not fall through.


Whitespace

Blank lines improve readability by setting off sections of code that are logically related.

Blank spaces should be used in the following circumstances:
  • A keyword followed by ( should be separated by a space.
    PHP Code:
    while (true) { 
  • Each ; in the control part of a for statement should be followed with a space.
  • Whitespace should follow every ,

Bonus Suggestions

{}:
Use {} instead of new[0]:
PHP Code:
temp.someArray = {}; 
This keeps thing clear by avoiding using a constructor-specific syntax for creating a data structure.

==:
Always use == instead of = when comparing variables:
PHP Code:
if (== b) { 
Confusing Pluses and Minuses:
Be careful to not follow a + with + or ++. This pattern can be confusing. Insert parenthesis between them to make your intention clear:
PHP Code:
total subtotal + +myInput.value
is better written as:
PHP Code:
total subtotal + (+myInput.value); 
so that the + + is not misread as ++.

Last edited by WhiteDragon; 07-07-2009 at 04:44 PM..
Reply With Quote
  #2  
Old 07-07-2009, 11:22 AM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
Looks nice, stickied.
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”
Reply With Quote
  #3  
Old 07-07-2009, 11:58 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 WhiteDragon View Post
Variable Declarations

Variables should be declared before used. GS2 does not require this, but doing so makes the program easier to read.

The variable declarations should be the first statements in the function body.

It is preferred that each variable be given its own line and comment.
PHP Code:
temp.currentEntry// currently selected table entry
temp.level// current level
temp.size// size of table 
There was a debate somewhere in the forums a while back about this. I don't understand why you'd do something like that when you can instead do this:

PHP Code:
/*
    Variables:
    
    temp.level - current level
    temp.size - size of table
*/ 
Why make the engine do more work by even parsing the text to see if it should do anything with it? Comments work just as well.
Quote:
Always use the prefix even after originally declaring the variable for clarity.
This is personal preference, though generally leads to cleaner coding. I don't do it myself, but I name my variables in a way that it is easy to know where they are coming from.
Quote:
  • Classes should start with an upper case letter
It is general Graal style to have classes in all lowercase letters. I don't know of a single server that has classes with uppercase letters. Classes are not objects (like this rule was meant for). There's no reason to capitalize them.
Quote:
  • The { should be at the end of the line that begins the compound statement.
This is personal preference.
Quote:
return is a function, not a statement, therefore it should use ( ) around the value, otherwise we are using GS1 syntax.
I'm like 99percent certain this is incorrect. Someone please let me know if I'm wrong. return is not a function in any language (with similar syntax to GS2) that I know of (such as Java).
Quote:
Each case is aligned with the switch. This avoids over-indentation.
I disagree completely. It ruins the general indentation style for different blocks of code. Over-indentation is not a problem. Not indenting only hurts readability.
Quote:
Each group of statements (except the default) should end with break or return. Do not fall through.
Why? There are times when it is perfectly okay to fall through.

PHP Code:
switch (player.chat) {
  case 
"/clear":
  case 
"clear":
    
// clear messages
  
break;


I appreciate the effort you put into this, but it's focused way too much on what personal preference should be rather than clean coding standards.
__________________
Reply With Quote
  #4  
Old 07-07-2009, 04:19 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by cbk1994 View Post
PHP Code:
/*
    Variables:
    
    temp.level - current level
    temp.size - size of table
*/ 
Why make the engine do more work by even parsing the text to see if it should do anything with it? Comments work just as well.
Because comments like that lack a formal means of writing (as opposed to phpdoc or javadoc) and therefore could be written in varying ways, increasing variations between different coder's scripts.

When trying to say "I'm going to use this variable later", declaring beforehand it is a very natural way to do so, and avoids these inconsistencies.

I really don't think that it is a performance hit to do this during actual runtime as the script is compiled (with a YACC/Bison parser), not interpreted, therefore it would be optimized already.

Also trying to make little performance over-optimizations can lead to very nasty could in general and is a good practice to avoid when there are really no actual benefits to reap.


Quote:
It is general Graal style to have classes in all lowercase letters. I don't know of a single server that has classes with uppercase letters. Classes are not objects (like this rule was meant for). There's no reason to capitalize them.
Classic's Dev Server does, and the coding was done by other people besides myself.

However, this is a fair point, but I don't see a reason as to not capitalize them besides increasing clarity and distinguishability between classes / objects / weapons.

Also, technically classes can be instantiated as objects with the import syntax.


Quote:
I'm like 99percent certain this is incorrect. Someone please let me know if I'm wrong. return is not a function in any language (with similar syntax to GS2) that I know of (such as Java).
I guess we'll have to wait on Stefan for that one since he's probably the only one who actually knows. I recall someone telling me this though.


Quote:
I disagree completely. It ruins the general indentation style for different blocks of code. Over-indentation is not a problem. Not indenting only hurts readability.
It doesn't break the indentation style of indenting statements inside of a complex statement.

case is part of the structure of the switch so it does not break the indentation rule.

Also, indentation can surely be a problem when it requires excessive amounts of side-scrolling to read/alter code.


Quote:
Why? There are times when it is perfectly okay to fall through.
As Wikipedia states, omitting break;s is often a big source of bugs and usually the purpose is better achieved using another statement.

This is the reason why languages like C# have started to prevent people from omitting break;s.


Quote:
This is personal preference
Quote:
This is personal preference.
Quote:
it's focused way too much on what personal preference
This is a coding conventions guide. Its focus is to promote readable and maintainable code. Both of those words are subjective and most of the things stated in the conventions can not be evaluated objectively.

Although many of the things are personal preference, none of them are illogical.

This is the reason I decided to put this on the forums rather than the wiki per say, because many of this things are arguable, and are bound to be argued on. However, until there is an objective ruling on one of these things that clearly puts one above another, I believe it's important to have a firm stance on one of the options.
Reply With Quote
  #5  
Old 07-07-2009, 04:30 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 WhiteDragon View Post
return Function

return is a function, not a statement, therefore it should use ( ) around the value, otherwise we are using GS1 syntax.
No, it is not. return; is a part of the language syntax, just like in most other scripted languages. Similarly, it is break; and continue;, not break(); and continue();.

Incidentally, I seem to have covered quite a lot of this already.
__________________
Skyld
Reply With Quote
  #6  
Old 07-07-2009, 04:43 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by Skyld View Post
No, it is not. return; is a part of the language syntax, just like in most other scripted languages. Similarly, it is break; and continue;, not break(); and continue();.

Incidentally, I seem to have covered quite a lot of this already.
Alright, I assume you know the inner-workings of the engine fairly well so I'll change that.

And, I didn't even know that document existed. Why wasn't it sticked?
Reply With Quote
  #7  
Old 07-07-2009, 04:51 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 WhiteDragon View Post
And, I didn't even know that document existed. Why wasn't it sticked?
I have no idea I guess it's in the Advice thread somewhere.
__________________
Skyld
Reply With Quote
  #8  
Old 07-07-2009, 06:29 PM
LoneAngelIbesu LoneAngelIbesu is offline
master of infinite loops
LoneAngelIbesu's Avatar
Join Date: May 2007
Location: Toldeo, Ohio
Posts: 1,049
LoneAngelIbesu has a spectacular aura aboutLoneAngelIbesu has a spectacular aura about
Send a message via AIM to LoneAngelIbesu
IIRC, I prefer this one over Skyld's. Though, it's for the petty reason that Skyld is a fan of placing the opening brace on a new line.

You should edit the post to include things like not doing things out of code blocks, and what-not.
__________________
"We are all in the gutter, but some of us are looking at the stars."
— Oscar Wilde, Lady Windermere's Fan
Reply With Quote
  #9  
Old 07-07-2009, 06:41 PM
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
Quote:
Originally Posted by WhiteDragon View Post
Each group of statements (except the default) should end with break or return. Do not fall through.
Didn't Zero just write a guide to switch statements that said pretty much showed efficient ways to not end each statement with a break?

Also, I like to indent my cases two spaces in, just like my if statements.
PHP Code:
switch (expression) {
  case 
expression:
    
statements
  
default:
    
statements

Reply With Quote
  #10  
Old 07-07-2009, 06:43 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 DustyPorViva View Post
Didn't Zero just write a guide to switch statements that said pretty much showed efficient ways to not end each statement with a break?
http://forums.graalonline.com/forums...ad.php?t=76294

I still don't understand why it's not stickied.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #11  
Old 07-07-2009, 06:52 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by DustyPorViva View Post
Didn't Zero just write a guide to switch statements that said pretty much showed efficient ways to not end each statement with a break?
That guide just showed how to use the switch statement.

I'm saying using switch like that (usually) only leads to confusing and unfriendly code.

It would have appropriate uses when designing an algorithm where that sort of structure is integral to the script.

However, normally, when coding, falling through should be avoided.
Reply With Quote
  #12  
Old 07-08-2009, 08:50 PM
[email protected] sid.gottlieb@googlemail.com is offline
Banned
Join Date: Mar 2008
Posts: 861
sid.gottlieb@googlemail.com will become famous soon enough
Quote:
Originally Posted by xXziroXx View Post
http://forums.graalonline.com/forums...ad.php?t=76294

I still don't understand why it's not stickied.
I learned from this awhile ago, didn't say thanks. Thanks!
Reply With Quote
  #13  
Old 02-11-2010, 02:04 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
Requesting unstick. This contains suggestions such as "avoid falling through", "variables should be declared", and other statements that are pretty blatantly wrong.
__________________
Reply With Quote
  #14  
Old 02-11-2010, 02:58 PM
12171217 12171217 is offline
Banned
Join Date: Jan 2009
Posts: 453
12171217 has a spectacular aura about
Personally, I don't think this is much of a clean-coding guide or whatever the Hell it's supposed to be so much as your personal preference in guide form. A lot of this stuff is very arguable, and what gives you the right to make your personal preference a standard, as this has been stickied?
Reply With Quote
  #15  
Old 02-11-2010, 05:10 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Is it really that bothersome? It was written 6 months ago, and it's basically just the page he linked translated for GS2.
__________________
Quote:
Reply With Quote
  #16  
Old 02-11-2010, 07:07 PM
Immolate Immolate is offline
Indigo
Join Date: Dec 2009
Posts: 322
Immolate is on a distinguished road
Quote:
Originally Posted by cbk1994
It is general Graal style to have classes in all lowercase letters. I don't know of a single server that has classes with uppercase letters. Classes are not objects (like this rule was meant for). There's no reason to capitalize them.
There's no reason to capitalise them because you can't capitalise them. When you do, the capitals get decapitalised.

Click image for larger version

Name:	proof.jpg
Views:	227
Size:	58.1 KB
ID:	50395

Notice the window title compared to the RC output and class window?

Note: Sorry, I have nothing to do
Reply With Quote
  #17  
Old 02-11-2010, 07:41 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by cbk1994 View Post
Requesting unstick. This contains suggestions such as "avoid falling through", "variables should be declared", and other statements that are pretty blatantly wrong.
I don't mind if this gets unstuck, but I still believe everything I wrote in here is more or less proper.

Could you elaborate perhaps?

Regarding the class capitalizing, I would change that but I can't edit this anymore.

Quote:
Personally, I don't think this is much of a clean-coding guide or whatever the Hell it's supposed to be so much as your personal preference in guide form. A lot of this stuff is very arguable, and what gives you the right to make your personal preference a standard, as this has been stickied?
It isn't a standard. I'm trying to appeal to people's intuitions about rules that have a logical backing.

If there a rule that work better for you, by all means use them. I'm also open to arguing any of the suggestions I provided.
Reply With Quote
  #18  
Old 02-11-2010, 08:26 PM
Loriel Loriel is offline
Somewhat rusty
Loriel's Avatar
Join Date: Mar 2001
Posts: 5,059
Loriel is a name known to allLoriel is a name known to allLoriel is a name known to allLoriel is a name known to all
I guess you could do worse than declaring variables especially considering Graal's crazy variable scopes, and while I do not do it in javascript either I can see the point considering var in a nested scope does not do what you would think it does.

And I guess falling through on switch cases is okay as long as you put // FALL THROUGH or something.
Reply With Quote
  #19  
Old 02-11-2010, 08:52 PM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
Even though most of the languages I use these days don't require it, I tend to declare all the variables I'm going to use at the beginning of a function, thanks to spending a lot of time with C.

That has it's uses, though, other than just being used to it.
Reply With Quote
  #20  
Old 02-12-2010, 12:09 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 WhiteDragon View Post
I don't mind if this gets unstuck, but I still believe everything I wrote in here is more or less proper.

Could you elaborate perhaps?

Regarding the class capitalizing, I would change that but I can't edit this anymore.


It isn't a standard. I'm trying to appeal to people's intuitions about rules that have a logical backing.

If there a rule that work better for you, by all means use them. I'm also open to arguing any of the suggestions I provided.
For the thread to be stuck pretty much says it is the "correct" way to do things. A few points:
  • Variable declarations serve absolutely no purpose to the engine, and can't possibly have any other effect except slowing it down, even if ever so slightly. Saying that all variables should be declared before being initialized is incorrect and personal preference. It is probably better to just use comments for listing variables.
  • The indentation of your switch statement is wacky. This is just my personal preference, but yours is being promoted as some kind of standard.
    PHP Code:
    switch (variable) {
      case 
    "value":
        
    // whatever
      
    break;
      
      case 
    "value2":
        
    // whatever2
      
    break;

    switch statements don't really have a universal format, though.
  • Falling through in switch statements isn't a problem unless you work on a server with really poor scripters.
  • 'default' needs to end with a break or return as well. Keep in mind it doesn't have to be at the end of the list.
  • Apparently classes can't even start with an uppercase letter, and even if they could/can, that would be different than what 99% of servers are doing now.

The thread is well-intentioned, but I don't like how it tries to set the "right" way to script based on someone's ideas. I wouldn't have a problem if the thread was reposted/edited with the controversial/incorrect stuff removed. There are also some things that need further explanation, such as the return statement. The way it's worded now it could be seen that something like:
PHP Code:
return (((2) + (2)) ^ .5); 
is wrong, when it's clearly not.
__________________
Reply With Quote
  #21  
Old 02-12-2010, 12:47 AM
12171217 12171217 is offline
Banned
Join Date: Jan 2009
Posts: 453
12171217 has a spectacular aura about
yea :0
Reply With Quote
  #22  
Old 02-12-2010, 01:01 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by cbk1994 View Post
For the thread to be stuck pretty much says it is the "correct" way to do things. A few points:
  • Variable declarations serve absolutely no purpose to the engine, and can't possibly have any other effect except slowing it down, even if ever so slightly. Saying that all variables should be declared before being initialized is incorrect and personal preference. It is probably better to just use comments for listing variables.
  • The indentation of your switch statement is wacky. This is just my personal preference, but yours is being promoted as some kind of standard.
    PHP Code:
    switch (variable) {
      case 
    "value":
        
    // whatever
      
    break;
      
      case 
    "value2":
        
    // whatever2
      
    break;

    switch statements don't really have a universal format, though.
  • Falling through in switch statements isn't a problem unless you work on a server with really poor scripters.
  • 'default' needs to end with a break or return as well. Keep in mind it doesn't have to be at the end of the list.
  • Apparently classes can't even start with an uppercase letter, and even if they could/can, that would be different than what 99% of servers are doing now.

The thread is well-intentioned, but I don't like how it tries to set the "right" way to script based on someone's ideas. I wouldn't have a problem if the thread was reposted/edited with the controversial/incorrect stuff removed. There are also some things that need further explanation, such as the return statement. The way it's worded now it could be seen that something like:
PHP Code:
return (((2) + (2)) ^ .5); 
is wrong, when it's clearly not.
Hi Chris,

For the thread to be stuck just means that people should look at it. Some guidance is better than none. (We have all seen some of the scripts out there.)

Regarding variable declarations, this is my rational behind the rule:
It makes it very clear how GS2 handles variables, and scope. As there is no block scope in GS2, this code code could be misleading:
PHP Code:
for (temp.0temp.2temp.i++) {
  echo(
temp.i);

With the above code, one (perhaps who has used C, C++, C#, Java, or the likes) may assume that temp.i would not be accessible outside of the for loop. However, this code makes it clear:
PHP Code:
temp.i;
for (
temp.0temp.2temp.i++) {
  echo(
temp.i);

Notating the script with comments would not have this same effect.
Also, naturally, when trying to make a standard for defining your variables, why not use the one that already exists within the syntax of the language? Speed is a non-reason against this point.


Regarding the switch indentation, the style 1) prevents over-indentation, and 2) matches the indentation style of all the other statements (that is, indent all compound statements within the outer statement; the cases being part of the switch statement).


Regarding falling through, sorry, but are you calling me and many of famous professional programmers & scripters "poor"? (This includes the language designers of C#, Go, Pascal, Ruby, Ada, Eiffel, and more.) Just because a construct exists in a language doesn't warrant over-usage of it. The switch statement can be really, really misleading to even mature scripters, and totally foreign to newbie scripters.


For a break;/return; in default:, that sounds extremely weird, as default is normally only evaluated when all the other cases are exhausted (apparently I don't know switches in GS2 that well either), but I'll confirm when I get home tonight.


I would have edited the class rule by now but can't.


The wording on the return statement section is a fair point and I would edit it if I could.


Thanks for commenting Chris, and I hope you agree that a thread including this information is better than no thread at all.
Reply With Quote
  #23  
Old 02-12-2010, 01:10 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
Graal really needs some coding conventions. The finer points can be debated at other times. I want this thread stickied.
__________________
Reply With Quote
  #24  
Old 02-12-2010, 01:11 AM
12171217 12171217 is offline
Banned
Join Date: Jan 2009
Posts: 453
12171217 has a spectacular aura about
From what I remember reading, in GS2, the only proper time to use the switch statement is when you want to take advantage of falling through, as it's a less efficient than standard if-than-else.
Reply With Quote
  #25  
Old 02-12-2010, 01:15 AM
benpoke103 benpoke103 is offline
Zvarri!
benpoke103's Avatar
Join Date: Jun 2002
Posts: 332
benpoke103 will become famous soon enough
Quote:
Originally Posted by Inverness View Post
Graal really needs some coding conventions. The finer points can be debated at other times. I want this thread stickied.
Seconded.
__________________
Need support? Here's how to reach me.

Forum PM (Preferred)
#graaldt @ Freenode
Reply With Quote
  #26  
Old 02-12-2010, 01:39 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by 12171217 View Post
From what I remember reading, in GS2, the only proper time to use the switch statement is when you want to take advantage of falling through, as it's a less efficient than standard if-than-else.
Hey Downsider,
Although I don't claim to have any knowledge of the specific Bison language parser Stefan created nor any of its specific optimizations, in terms of switch statements in general:
A switch statement can always perform at least as well as a logically equivalent if statement.
A switch statement can perform better than an if statement when the range of values of the switch statement are sufficiently close enough to get compiled to a branch table.

I doubt that optimization has been made in GS2 though, so they are most likely equivalent in terms of performance.
Reply With Quote
  #27  
Old 02-12-2010, 06:31 AM
12171217 12171217 is offline
Banned
Join Date: Jan 2009
Posts: 453
12171217 has a spectacular aura about
I still remember reading that.

And lol @ gs2 being compared to a typical bytecode language. This is Graal. It's atypical in every way, especially the client.

dunno. you seem conceited, especially the "hi chris" "hi downsider" and the whole long-post-obviously-trying-to-intimidate deal. Bit upsetting.
Reply With Quote
  #28  
Old 02-12-2010, 06:56 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by 12171217 View Post
dunno. you seem conceited, especially the "hi chris" "hi downsider" and the whole long-post-obviously-trying-to-intimidate deal. Bit upsetting.
Alright, here is my point of view: my thread gets unsticked (I don't even know who did it) for a reason that I totally don't agree with (minor disagreements), with no alternative put up, and I try to back up my logic.

I'm sorry if I upset you but I'm honestly trying to do something good here.
Reply With Quote
  #29  
Old 02-12-2010, 07:11 AM
12171217 12171217 is offline
Banned
Join Date: Jan 2009
Posts: 453
12171217 has a spectacular aura about
I'm upset.
Reply With Quote
  #30  
Old 02-12-2010, 08:46 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
I don't know, I think he's made some very good points.
And by all means, GS2 is not a bad language, Downsider, for what it's trying to do. I doubt Switch statements would be any slower than if-else logic.

WhiteDragon is someone who obviously has considerable experience and knowledge in computer science and while finer points of style boil down mostly to aesthetics and popular convention, the points he's made here are completely legitimate and helpful even if you may disagree with them.

This should be stickied.
Reply With Quote
  #31  
Old 02-12-2010, 10:36 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by WhiteDragon View Post
PHP Code:
temp.distance = function (x1y1x2y2) {
  return( ((
x2 x1)^+ (y2 y1)^2)^.5 );
};
echo(
temp.distance(2211)); 
Anonymous functions can unfortunately not be executed in the same statement they are declared in.
I may have been gone for a while (and not to nitpick or jump on the bandwagon...), but this is wrong. It is not as pretty as javascript to pull it off, but it can be done.

Quote:
Originally Posted by Douglas Crockford on switch Fall Through
Someone once wrote to me once suggesting that JSLint [his javascript "checker"] should give a warning when a case falls through into another case. He pointed out that this is a very common source of errors, and it is a difficult error to see in the code. I answered that that was all true, but that the benefit of compactness obtained by falling through more than compensated for the chance of error.

Then next day, he reported that there was an error in JSLint. It is misidentifying an error. I investigated and it turned out that I had a case that was falling through. In that monent, I achieved enlightenment. I no longer use intentional fall throughs. That discipline makes it much easier to find the unintentional fall throughs.
Just because Crockford messes up doesn't mean no one should be allowed to benefit from the usefulness of switch. S'all I'm sayin'.

A few more things: javascript is a language in which appending the bracket at the end of a function is highly recommended because it has a sort of auto-complete where it puts a semicolon at the end of any line that a parser thinks is a complete statement. Placing a bracket at the end of a line avoids any confusion. GraalScript does not have this issue. Go hog wild, throw that bracket anywhere you like (where syntax allows).

The argument that X language uses Y means that Y is right for Z language is absolute nonsense. R is a statistical programming language that is C-like and return is a function in R.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #32  
Old 02-12-2010, 01:57 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
This thread right now is too messy to be stickied. You've admitted there are things you would change if you could.

I would rather see a new thread stickied promoting coding standards, not personal preference.
__________________
Reply With Quote
  #33  
Old 02-12-2010, 05:35 PM
Loriel Loriel is offline
Somewhat rusty
Loriel's Avatar
Join Date: Mar 2001
Posts: 5,059
Loriel is a name known to allLoriel is a name known to allLoriel is a name known to allLoriel is a name known to all
Quote:
Originally Posted by cbk1994 View Post
For the thread to be stuck pretty much says it is the "correct" way to do things. A few points:
  • Variable declarations serve absolutely no purpose to the engine, and can't possibly have any other effect except slowing it down, even if ever so slightly. Saying that all variables should be declared before being initialized is incorrect and personal preference. It is probably better to just use comments for listing variables.
Most things mentioned in style guides serve no purpose in the engine and are personal preference. The idea is to point out what most people's personal preference is, or should be.

Quote:
  • The indentation of your switch statement is wacky.
That is how pretty much everybody indents switch statements.

Quote:
There are also some things that need further explanation, such as the return statement. The way it's worded now it could be seen that something like:
PHP Code:
return (((2) + (2)) ^ .5); 
is wrong, when it's clearly not.
It clearly says "should", it is not ambiguous.
Reply With Quote
  #34  
Old 02-12-2010, 05:58 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by Tolnaftate2004 View Post
I may have been gone for a while (and not to nitpick or jump on the bandwagon...), but this is wrong. It is not as pretty as javascript to pull it off, but it can be done.
No, you can definitely not call the function in the same line you define it, like so:
PHP Code:
temp.example = (function () {
  
temp.2;
  return 
temp.i*5;
})(); 
That would fail. I'm pretty sure this is a result of YACC not truly treating functions (and other structures such as arrays) as first-class, when they should be and would make the grammar considerably less complex that whatever is currently there, most likely.
Quote:
Originally Posted by Tolnaftate2004 View Post
Just because Crockford messes up doesn't mean no one should be allowed to benefit from the usefulness of switch. S'all I'm sayin'.
Crockford isn't the only one I'm citing. A number of languages (see the giant list I posted a few posts back) have entirely removed the fall-through construct because of its hazard.
Quote:
Originally Posted by Tolnaftate2004 View Post
A few more things: javascript is a language in which appending the bracket at the end of a function is highly recommended because it has a sort of auto-complete where it puts a semicolon at the end of any line that a parser thinks is a complete statement. Placing a bracket at the end of a line avoids any confusion. GraalScript does not have this issue. Go hog wild, throw that bracket anywhere you like (where syntax allows).
I'm not sure exactly what you are referring to here.
Quote:
Originally Posted by Tolnaftate2004 View Post
The argument that X language uses Y means that Y is right for Z language is absolute nonsense. R is a statistical programming language that is C-like and return is a function in R.
It isn't nonsense if languages X and Z have similar core assumptions.



Quote:
Originally Posted by cbk1994 View Post
This thread right now is too messy to be stickied.
What are you talking about?
Quote:
Originally Posted by cbk1994 View Post
You've admitted there are things you would change if you could.
I wasn't claiming that what I wrote was perfect and I certainly would have changed everything by now, but I don't have the permissions to.
Quote:
Originally Posted by cbk1994 View Post
I would rather see a new thread stickied promoting coding standards, not personal preference.
Then go write one and stop defacing mine.
Reply With Quote
  #35  
Old 02-12-2010, 05:58 PM
12171217 12171217 is offline
Banned
Join Date: Jan 2009
Posts: 453
12171217 has a spectacular aura about
Quote:
Originally Posted by coreys View Post
I don't know, I think he's made some very good points.
And by all means, GS2 is not a bad language, Downsider, for what it's trying to do. I doubt Switch statements would be any slower than if-else logic.

WhiteDragon is someone who obviously has considerable experience and knowledge in computer science and while finer points of style boil down mostly to aesthetics and popular convention, the points he's made here are completely legitimate and helpful even if you may disagree with them.

This should be stickied.
I've never said GS2 is a bad language, just said that history has shown Graalscript has never held the form of a typical language, as far as GS2's syntax being a bit atypical (Is it TorqueScript?) and it's set of features is far more complete than most scripting languages designed for a single game.

Also, I've put no thought in saying that switch statements are slower than if-then-else, simply sharing what I've previously read in the past.
Reply With Quote
  #36  
Old 02-12-2010, 07:37 PM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
The base syntax is TorqueScript, I think (I've heard it places, I could always be wrong), with lots of functions and objects for use with Graal.
Reply With Quote
  #37  
Old 02-12-2010, 09:15 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 cbk1994 View Post
This thread right now is too messy to be stickied. You've admitted there are things you would change if you could.

I would rather see a new thread stickied promoting coding standards, not personal preference.
If you don't like the current one go make your own instead of tearing down other people's threads.

You seem to come in here requesting it to be unstickied to try to flex your internet muscles or something instead of making suggestions to the author for improvement. And because of that you've annoyed WhiteDragon, myself, and someone else I won't name with your behavior, nice job.

Of course this post is probably full of biases.
__________________

Last edited by Inverness; 02-12-2010 at 09:26 PM..
Reply With Quote
  #38  
Old 02-12-2010, 09:20 PM
12171217 12171217 is offline
Banned
Join Date: Jan 2009
Posts: 453
12171217 has a spectacular aura about
Quote:
Originally Posted by coreys View Post
The base syntax is TorqueScript, I think (I've heard it places, I could always be wrong), with lots of functions and objects for use with Graal.
I remember reading in a certain release note that it had rewritten the TorqueScript bytecode interpreter from scratch and thus didn't require a lisence or something.

Would be interesting for someone to clear this up.
Reply With Quote
  #39  
Old 02-12-2010, 11:37 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by WhiteDragon View Post
No, you can definitely not call the function in the same line you define it, like so:
PHP Code:
temp.example = (function () {
  
temp.2;
  return 
temp.i*5;
})(); 
That would fail. I'm pretty sure this is a result of YACC not truly treating functions (and other structures such as arrays) as first-class, when they should be and would make the grammar considerably less complex that whatever is currently there, most likely.
PHP Code:
temp.example = (temp.suckawhat = function () {
  
temp.2;
  return 
temp.i*5;
})(); 
Like I said, not so pretty...
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #40  
Old 02-12-2010, 11:41 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by Tolnaftate2004 View Post
PHP Code:
temp.example = (temp.suckawhat = function () {
  
temp.2;
  return 
temp.i*5;
})(); 
Like I said, not so pretty...
Although that may be valid syntax (can't check right now), I don't see how that would run the function. My guess is that it would just execute:
Storing the function in a temp.suckawhat, which would evaluate to 0(?), then 0 would be stored in temp.example.

If that actually works I may have to hurt someone though since it literally makes no sense.
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 11:02 PM.


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