Ajax Talk - Asynchronous javascript and xml discussion board
 FAQ   Search    Usergroups      Register   Profile   Check your messages   Log in 
Signup now to enjoy a range of free membership advantages !!
Ajax Talk - Moderators Wanted
Jesdisciple's Coding Terminology and Conventions

 
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> AJAX Programmers Exchange
View previous topic :: View next topic  
Author Message
Jesdisciple
Frequent Poster
Frequent Poster


Joined: 20 Oct 2007
Posts: 36
Location: Tejas

PostPosted: Fri Dec 07, 2007 11:51 pm    Post subject: Jesdisciple's Coding Terminology and Conventions Reply with quote

I'm pretty picky about my code, so I want to detail my pet peeves and preferences. I'll assume that everyone knows what objects, arrays, strings, regular expressions (or "regexes"), numbers, functions, and properties are.

Types of Functions
First, I need to define these three types of functions: classes, methods, and procedures.

A class is the kind of function that you would instantiate (use new to call), like Object:
Code:
var obj = new Object();
Classes are the cornerstone of object-oriented JavaScript (really just a hack to make a procedural language behave like an object-oriented one). By convention (and not just mine), class names should always be in UpperCamelCase. When you're writing a class, the pseudo-variable this refers to each individual instance of that class that will ever exist.

A method is a function which happens to be the property of an object, like document.write:
Code:
document.write('Hello, world!');
Methods, like all other properties, should always have lowerCamelCase names (again, by widely accepted convention). In a method, this refers to the object in whose context the method was called. (Since functions are just callable objects, the method of one object can be assigned as the property of another, and, when that property is called, this will not be what you expected when you wrote the method.)

Technically, a procedure is a type of method, except it's a method of window, like window.open:
Code:
//var w = window.open(); //That does exactly the same thing as this:
//var w = open();
Uncommenting either (but not both!) of the above lines will open a blank window (or tab, depending on your browser's settings). Whenever you declare a function and don't specify that it's a property of any particular object, it latches onto window, so this is always the current instance of the Window class (and thus quite useless) in a procedure. Like methods', procedures' names should be in lowerCamelCase.

When needed, my program initiation procedure will be called "main," under influence from my history with Java:
Code:
<html>
   <head>
      <title>Titled Document</title>
      <script type="text/javascript">
         function main(){
            //
         }
      </script>
   </head>
   <body onload="main();">
      
   </body>
</html>

My Program Format
As I partially demonstrated above, I put spaces on each side of my operators (=, +, -, *, /, %, ?, :, &&, ||, ==, !=, ===, !==, <, <=, >, >=, ^, ~, &, |, <<, >>, >>>), except for colons used for assignment/labels, exclamation points, and increment/decrement signs (++ and --). Commas and assignment/label colons get no spaces before and one space after.

All JavaScript strings are enclosed in single quotes. This makes it easier to write HTML attributes and user-friendly quotes from JavaScript, and only causes problems when apostrophes get involved.

Each line which would end with a semicolon in most languages will in my JavaScript. This way, readability is enhanced (I believe), and some rare ambiguous situations are clarified for the browser. (I can't think of any of those ambiguous situations offhand, so you'll have to take my word for it and keep your eyes open.)

Curly brackets are always reason to break the line if there's anything in them; round brackets, square brackets, and ternary operators are only when their contents get too long for the screen. All such containers increase the indentation of any contained lines by one.

Most people use alphabetical iterator identifiers (i, j, k, etc.) in their for loops. However, I got very frustrated and embarrassed (in my high school Computer Science class) by a simple bug - a spelling error - and now use numeric identifiers (i0, i1, i2, etc.) to make the difference between my first (i or i0) and second (j or i1) visually obvious. I also have a higher range of identifiers that don't cause confusion available; whereas someone else's 19th iterator might be called "zi," mine would simply be "i19."

Here's a demonstration:
Code:
var one = 1;
var two = one * 2;
var three = two + one;
var yup = true;
if(!yup || (three <= two)){
   alert('This can't be happening!');
}
for(var i0 = 0; i0 < 10; i0++){
   //
}
var object = {
   numbers: [ //This array really isn't long enough to break, but
      one,    //I want to show what I mean.
      two,    //(This applies to argument lists, too.)
      three
   ],
   do: function(){
      alert(function dummy(){
            //This is just a dummy.
         });//The round brackets don't break, but the curly ones do.
   }
}
var something = yup ? object : null;
switch(something){
   case object:
      //
      break;
   case null:
      //
      break;
   default:
      //
}
I think you get the idea.
_________________
In ALL things, strive for ><>,
Chris
Back to top
View user's profile Send private message Visit poster's website AIM Address MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> AJAX Programmers Exchange All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
 Advertisements
 XML & Copyright Notice 
    


Powered by phpBB © 2001, 2005 phpBB Group

What is according to Wikipedia: Ajax Web2.0 XML E4X


Link to our site:

  


del.icio.us digg spurl wists simpy newsvine blinklist furl blogmarks yahoo! myweb smarking ma.gnolia segnalo reddit fark technorati cosmos


Normal Bookmark


Partner Button Links:
Codecrunch.com: Webmaster Tutorials    One Ajax


Partner Text Links:
quomon | web20log | ajaxmatters | tableless | 456bereastreet | music charts | gift ideas | competitions | free xbox 360 | link trade ?


Credits:
it consultancy & project management lead generation zoekmachine optimalisatie steal these buttons seo expert