![]() |
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:
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:
Always use the prefix even after originally declaring the variable for clarity. Function Declarations
PHP Code:
PHP Code:
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:
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.
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 { }.
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:
for Statement A for class of statements should have the following form: PHP Code:
while Statement A while statement should have the following form: PHP Code:
do Statement A do statement should have the following form: PHP Code:
switch Statement A switch statement should have the following form: PHP Code:
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:
Bonus Suggestions {}: Use {} instead of new[0]: PHP Code:
==: Always use == instead of = when comparing variables: PHP Code:
Be careful to not follow a + with + or ++. This pattern can be confusing. Insert parenthesis between them to make your intention clear: PHP Code:
PHP Code:
|
Looks nice, stickied.
|
Quote:
PHP Code:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
PHP Code:
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. |
Quote:
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:
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:
Quote:
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:
This is the reason why languages like C# have started to prevent people from omitting break;s. Quote:
Quote:
Quote:
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. |
Quote:
Incidentally, I seem to have covered quite a lot of this already. |
Quote:
And, I didn't even know that document existed. Why wasn't it sticked? |
Quote:
|
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. :cool:
You should edit the post to include things like not doing things out of code blocks, and what-not. |
Quote:
Also, I like to indent my cases two spaces in, just like my if statements. PHP Code:
|
Quote:
I still don't understand why it's not stickied. |
Quote:
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. |
Quote:
|
Requesting unstick. This contains suggestions such as "avoid falling through", "variables should be declared", and other statements that are pretty blatantly wrong.
|
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?
|
Is it really that bothersome? It was written 6 months ago, and it's basically just the page he linked translated for GS2.
|
1 Attachment(s)
Quote:
Attachment 50395 Notice the window title compared to the RC output and class window? :D Note: Sorry, I have nothing to do :( |
Quote:
Could you elaborate perhaps? Regarding the class capitalizing, I would change that but I can't edit this anymore. Quote:
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. |
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. |
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. |
Quote:
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:
|
yea :0
|
Quote:
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:
PHP Code:
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. |
Graal really needs some coding conventions. The finer points can be debated at other times. I want this thread stickied.
|
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.
|
Quote:
|
Quote:
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. |
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. |
Quote:
I'm sorry if I upset you but I'm honestly trying to do something good here. |
I'm upset.
|
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. |
Quote:
Quote:
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. |
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. |
Quote:
Quote:
Quote:
|
Quote:
PHP Code:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
|
Quote:
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. |
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.
|
Quote:
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. |
Quote:
Would be interesting for someone to clear this up. |
Quote:
PHP Code:
|
Quote:
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. |
| All times are GMT +2. The time now is 03:55 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.