Welcome Guest [Log In] [Register]

About Me

Tips, tricks, and projects from the Generation Studio team. Updated often.
Categories
Code Release (2)
Coding Tips (1)
Other (2)
RPGeneration (2)

Readers Online

0 Members, 1 Guest

Feb 09

ZB Coding Conventions

I think it's time we establish a standard in layout of code for ZB. Page layouts for ZB currently look very messy, because something like this happens when you get multiple codes from different coders:
Code:
 

<script>
//Code by John of Doe
//http://google.com
//More random advertising here

var a = document.getElementById('ha');
</script>

<script type='text/javascript'>
/* RPG by Ron of TheCodes
To use this, make sure RPG.on is set to true!
If you have questions, visit support.zetaboards.com! */

var RPG = {};
RPG.on = true;
</script>


Obviously, these are two completely different styles of coding! Therefore I've set out to create a standard for people (including me, from now on!) to follow. I'd appreciate your comments on what changes should be made.

1. The script's type is text/javascript.
It doesn't really matter, but proper XHTML is
Code:
 
<script type="text/javascript">

Therefore, define the type (and only the type!) "text/javascript" in the script tag.

2. Wrap scripts in //<![CDATA[
This helps XHTML parsers from parsing your Javascript incorrectly.
Code:
 

<script type="text/javascript">
//<![CDATA[
(code here)
//]]>
</script>


3. Define information in a single /* .. */ at the top of the code.
Standard information: The code creator, information on how to use the code.
Code:
 
/* Code created by Ryura
The variables are Code.On (when set to true, turns it on. Set to false, turns the code off) Code.Link (defines the link for the code, should be in double quotes [""]). */


4. Define a single global variable (object) for your code to use.
Global variables are bad and can cause problems with other's codes, so define a single, meaningful object for your code.
Code:
 
var TopicEnhance = {};
TopicEnhance.text = "Your topics are being enhanced!";


5. Define actual coding comments with // .
Comments in your code help others to understand what is going on. Make your comments meaningful.
Don't do obvious things like:
Code:
 
TopicEnhance.number=1 //Sets number to 1.


6. Use proper spacing.
Indent is 4 types spaces. Don't use the tab key.
Code:
 

function () {
if(something) {
x++
}
}


7. Use jQuery.
Don't use document.getElementById and the like, unless absolutely necessary. jQuery is included on all ZB boards, so use it!

8. Don't pack, minify
Minify your external scripts to increase speed. Do not pack your scripts, for it takes time for them to be unpacked.

Obviously there may be problems with my initial suggestions, or I may be missing some standards that should be created. Comment with your thoughts!
Posted in Coding Tips at 1:47 pm · 10 comments
« Older Entries