This section presents the eLua coding style that should be followed by every developer working on eLua. The following rules apply:
i = 3 (not i=3)
a = ( a + 5 ) / 3
for( i = 0; i < 10; i ++ ) ...
if( ( i == 5 ) && ( a == 10 ) ) ...
unsigned i = ( unsigned )p;
void func( int arg1, const char* arg2 ) ...
if( i == 2 )
{
// some code here
}
else
{
// some other code here
}
Or:
void f( int i )
{
// function code
}
Do not enclose single statements in {} when given a choice. For example, do this:
if( i == 2 )
return;
instead of this:
if( i == 2 )
{
return;
}
Also, follow the "one statement per line" rule. In other words, don't do this:
if( i == 2 ) return;
Do this instead:
if( i == 2 )
return;
Note that eLua code does not use a space between the function name and its parameter list when calling/defining it (like in the Lua code, for example). So do this:
void f( int i )
{
// function code here
}
f( 2 ); // function call
instead of this:
void f ( int i )
{
// function code here
}
f ( 2 ); // function call
int simple;
double another_identifier;
char yes_this_is_OK_although_quite_stupid;
As opposed to:
int Simple1;
double AnotherIdentifier;
char DontEvenThinkAboutWritingSomethingLikeThis;
DO NOT USE HUNGARIAN NOTATION (like iNumber, sString, fFloat ... if you don't know what that is, it's fine, as it means that we don't need to worry about it :) ). It has its advantages
when used properly, it's just not for eLua.
if( key == 10 )
sys_ok();
else if( key == 5 )
phone_dial( "911" );
else if( key == 666 )
{
while( user_is_evil() )
exorcize_user();
}
else if( key == 0 )
sys_retry();
else
sys_error();
Instead, define some constants with meaningful names (via enums or even #define) and write like this:
if( key == KEY_CODE_OK )
sys_ok();
else if( key == KEY_CODE_FATAL_ERROR )
phone_dial( "911" );
else if( key == KEY_CODE_COMPLETELY_EVIL )
{
while( user_is_evil() )
exorcize_user();
}
else if( key == KEY_CODE_NONE )
sys_retry();
else
sys_error();
You can see in this example an accepted violation of the "one statement per line" rule: it's OK to write "else if (newcondition)" on the same line.// This function returns the sum of two numbers
// Input: n1 - first number
// Input: n2 - the second number
// Output: the sum of n1 and n2
int sum( int n1, int n2 )
{
return n1 + n2;
}
When something is self-obvious from the context, documenting it more is pointless and decreases readability.int uart_tx_count, uart_rx_count;
int uart_receive( unsigned limit )...
unsigned uart_send( const char *buf, unsigned size )...
Also, if you're using 3rd party code (from a library/support package for example) making it follow the above rules is nice, but not mandatory. Focus on functionality and writing your own code properly, and come back to indent other people's code when you really don't have anything better to do with your time.