Wiki

Clone wiki

Mono:UI / Project / Coding Style

Coding Style

I've taken to writing my code in a particular way to make it more consistent and easier to look at. This document aims to describe those practices.

Indentation

Indentations are a tab, which are displayed at a width of 4 spaces.

Case statements in a switch are indented:

switch ( _i ) {
    case 1:
    case 2:
        do_something();
        break;
    default
        do_something_else();
        break;
}

When indenting something, take care that the opening and closing symbols are on the same indentation:

if (
    something &&
    something_else &&
    yet_another_thing
) {
    do_something();
}

Never open something and then close it on a different indentation.

It's also possible to indent parts of code that belong together:

surface_set_target( _s );
    draw_sprite( spr_foo, image_index, 0, 0 );
surface_reset_target();

Breaking Lines

Never break a line in the middle of a statement without bracketing it. So avoid this:

var _i = some_value + some_other_value +
    something_else;

If you must do this, do it like this:

var _i = (
    some_value + 
    some_other_value +
    something_else
);

Don't put two statements on a single line. Avoid especially this:

if ( _i == 0 ) do_something();

Do this instead:

if ( _i == 0 )
    do_something();

Curly Braces

Curly braces go at the end of the line, not at the next line:

if ( _i == 0 ) {
    do_something();
    do_something_else();
}

If you can avoid using braces, you should do so:

if ( _i == 0 )
    do_something();

However, when different sections of one if/else chain have more than one statement, brace all of them:

if ( _i == 0 ) {
    do_something();
} else {
    do_something_else();
    do_another_thing();
}

Round & Square Brackets

Brackets should always have spaces inside as padding between them and the things within. The only exception is when there is nothing in between the opening and closing bracket:

do_something( _foo, _bar );
do_something_else();

In if, while, until, switch and repeat statements, brackets are not needed if their expression is a single component. So:

if _foo
    do_something();

if ( _bar == 0 )
    do_something();

Note that a single function call still counts as a single component, even if it has many arguments:

if do_something( _foo, _bar )
    do_something_else();

When using accessors, the symbol counts as part of the opening bracket. So:

var _foo = _bar[| _index ];

Spaces

Spaces should also be places between keywords and brackets or braces, but not between functions and brackets or braces. So avoid the following:

if( _bar == 0 )
    do_something();

When using operators like = + - / *, || && or == >= > <= <, make sure to put spaces between them and anything to their left or right. When using ! however, only put a space to the left side. When using ++ or --, don't put a space on the side of the object they're connected to. So:

if ( !_foo && _bar >= _i++ )
    do_something();

Naming

When using local variables, start them with an underscore to avoid mixing them up with instance variables. You should name these in detail wherever necessary, but abbreviating them is usually not an issue. When using instance or global variables, do not abbreviate.

If you wish to indicate that a variable is important to the functionality of an instance or mechanic and shouldn't be changed without caution, or possibly at all, start with a double underscore. This will prevent them from clashing with other variable names, and visually set them apart so that people can take note not to use them. When it becomes necessary to access such variables, try only to do so from within the host instance, or from within a script specifically meant to alter that variable. This further helps to safeguard their integrity.

Updated