Back to vBulletin 3 Articles

Don't take vB's code as a model for perfect coding!
by filburt1 14 Mar 2003

Unlike rumors of vB3, vB2 has astetically horribly written code. Here are some things that you should do (and that vB doesn't always do):

1. When referencing a key in an array, always put the key in quotes. See http://www.php.net/manual/en/language.types.array.php for an explanation. For example, do this:
PHP Code:
$myarray['mykey'] = "whatever"
Not this:
PHP Code:
$myarray[mykey] = "whatever"
2. Whitespace is your friend. Use it. For example, this is painful to read:
PHP Code:
if($a==b&&$c xor ($d+1)){foo();bar()}; 
For the love of God, do this instead:
PHP Code:
if ($a == $b and $c xor ($d 1)
{
    
foo();
    
bar();

For readability the two braces really should be on their own lines although many prefer to put the opening brace with the previous line. But do not do the same with the last brace as this can cause more trouble.

3. Indent or be smacked.
Bad:
PHP Code:
while (true)
{
if (
$something)
{
for (
$i 0$i $x$i++
{
dosomething();
}
}

Good:
PHP Code:
while (true)
{
    if (
$something)
    {
        for (
$i 0$i $x$i++)
        {
            
dosomething();
        }
    }

If you write like the first example you'd be fired at your job. A side note: the typical editor uses four spaces or a tab character to indent. Try to keep this consistant! vB does not and can wreak havoc when trying to indent the standard four spaces because nothing will line up.

I'm sure many more examples are out there. Reply if you have any.

vblts.ru supports vBulletin®, 2022-2024