Scripting Style

You may have noticed the way the examples I write contain tabs.

{
	int $i;
	for ($i = 0; $i < 10; $i++)
	{
		print "Please use good style.\n";
	}
	print "Or else.\n";
}

It has absolutely nothing to do with the way the script is executed.
For all Maya cares, the whole script could be on one line.

{int $i; for( $i = 0; $i < 10; $i++){print "Please use good style.\n";}print "Or else.\n";}

Those will both work.
But which was easier to understand?
I had no prior programming experience when I started Mel-scripting, yet it didn't take long before I
came up with a coding style.  After seeing code written by other people I was relieved to see that,
for the most part, all programmers use similar styles.  Its common sense.
The rules I follow are simple enough.
Always put a { character on a new line.
after the { tab in all the text that is enclosed within the brackets.

This makes it very easy to see what code is happening where, especially with nested loops and conditionals.
You can follow the starting bracket to the ending bracket in a straight line down the page, and know
where all the code is inside that block.
Here is one of the examples from the loops page:

{
	int $x;
	int $y;
	int $z;
	for ( $x = 0; $x < 10; $x++ )
	{
		for ( $y = 0; $y < 10; $y++ )
		{
			for ( $z = 0; $z < 10; $z++ )
			{
				polyCube -w .9 -h .9 -d .9;
				move $x $y $z;
			}
		}
	}
}

Here it is without tabs

{
int $x;
int $y;
int $z;
for ( $x = 0; $x < 10; $x++ )
{
for ( $y = 0; $y < 10; $y++ )
{
for ( $z = 0; $z < 10; $z++ )
{
polyCube -w .9 -h .9 -d .9;
move $x $y $z;
}
}
}
}

How about one line?

{int $x;int $y;int $z;for ( $x = 0; $x < 10; $x++ ){for ( $y = 0; $y < 10; $y++ ){for ( $z = 0; $z < 10; $z++ ){polyCube -w .9 -h .9 -d .9;move $x $y $z;}}}}

All those examples work, but imagine having to find this bug :

{int $x;int $y;int $zfor ( $x = 0; $x < 10; $x++ ){for ( $y = 0; $y < 10; $y++ ){for ( $z = 0; $z < 10; $z++ ){polyCube -w .9 -h .9 -d .9;move $x $y $z;}}}}


// Error: {int $x;int $y;int $zfor ( $x = 0; $x < 10; $x++ ){for ( $y = 0; $y < 10; $y++ ){for ( $z = 0; $z < 10; $z++ ){polyCube -w .9 -h€ //
// Error: Line 1.26: Syntax error //
// Error: {int $x;int $y;int $zfor ( $x = 0; $x < 10; $x++ ){for ( $y = 0; $y < 10; $y++ ){for ( $z = 0; $z < 10; $z++ ){polyCube -w .9 -h€ //
// Error: Line 1.39: Syntax error //
// Error: {int $x;int $y;int $zfor ( $x = 0; $x < 10; $x++ ){for ( $y = 0; $y < 10; $y++ ){for ( $z = 0; $z < 10; $z++ ){polyCube -w .9 -h€ //
// Error: Line 1.50: Syntax error //
// Error: {int $x;int $y;int $zfor ( $x = 0; $x < 10; $x++ ){for ( $y = 0; $y < 10; $y++ ){for ( $z = 0; $z < 10; $z++ ){polyCube -w .9 -h€ //
// Error: Line 1.69: Syntax error //
// Error: {int $x;int $y;int $zfor ( $x = 0; $x < 10; $x++ ){for ( $y = 0; $y < 10; $y++ ){for ( $z = 0; $z < 10; $z++ ){polyCube -w .9 -h€ //
// Error: Line 1.80: Syntax error //
// Error: {int $x;int $y;int $zfor ( $x = 0; $x < 10; $x++ ){for ( $y = 0; $y < 10; $y++ ){for ( $z = 0; $z < 10; $z++ ){polyCube -w .9 -h€ //
// Error: Line 1.99: Syntax error //
// Error: {int $x;int $y;int $zfor ( $x = 0; $x < 10; $x++ ){for ( $y = 0; $y < 10; $y++ ){for ( $z = 0; $z < 10; $z++ ){polyCube -w .9 -h€ //
// Error: Line 1.108: "$z" is an undeclared variable. //
// Error: ...{int $x;int $y;int $zfor ( $x = 0; $x < 10; $x++ ){for ( $y = 0; $y < 10; $y++ ){for ( $z = 0; $z < 10; $z++ ){polyCube -w .9 -h //
// Error: Line 1.152: "$z" is an undeclared variable. //
// Error: ...{int $x;int $y;int $zfor ( $x = 0; $x < 10; $x++ ){for ( $y = 0; $y < 10; $y++ ){for ( $z = 0; $z < 10; $z++ ){polyCube -w .9 -h //
// Error: Line 1.154: Syntax error //

The script editor doesn't even print the whole line in the error read-out!

The style I follow when with variable names is the same as in the Alias scripts.
ex: $thisIsAnExample
The first letter is lower case, and the beginning of each word after has a capital letter.
Some may prefer using _ underscores instead, it doesn't really matter.

Rremember, these style rules are common sense, and make life easier for everyone.
For impeccable programming style check out the Alias scripts that come with Maya.