https://wiki.bennugd.org/index.php?title=Sandman%27s_code_convention&feed=atom&action=historySandman's code convention - Revision history2024-03-29T12:51:02ZRevision history for this page on the wikiMediaWiki 1.36.2https://wiki.bennugd.org/index.php?title=Sandman%27s_code_convention&diff=6616&oldid=prevSandman at 02:49, 1 December 20082008-12-01T02:49:09Z<p></p>
<p><b>New page</b></p><div>Fenix/Bennu Code Convention by Sandman (work in progress)<br />
<br />
== Indenting and Blank Lines ==<br />
Indenting is done with four spaces or a tab of four spaces wide. The [[Program]]-, [[Process]]-, [[Function]]-, [[Global]]-, [[Local]]-, [[Public]]-, [[Private]]- and [[Begin]]-statements are always completely to the left, with no indentation at all. The exception to this is inside the [[Declare]] block, in which case [[Public]] and [[Private]] are indented once. Declaration of variables are indented once. Precompiler-statements are generally to the left as well, but for small blocks they can be indented. The code between Begin and [[End]] statements are indented once at first.<br />
<br />
The use of blank lines doesn't matter, but its use may improve readability, so try to keep it in mind.<br />
<br />
See the example for a visual representation.<br />
<br />
== Names ==<br />
Separating sections of a name is done by use of the underscore ("_") character. An easy example is the [[function]] [[NET_Init]]. "NET" is a different section than "Init", because the first denotes the origin of the function and the second denotes what the function does. Another example is [[NET_ERROR_CONNECTING]]: "NET" denotes the origin, "ERROR" denotes a more precise origin (an error) and "CONNECTING" denotes what the constant means (error while connecting).<br />
<br />
Separating some words with an underscore ("_") is not needed, but can simply be put together, making the first character of the second word a capital. For example [[NET_SendVar]]. Here we see the section "NET" and the section "SendVar", which is further cut down to "Send" and "Var".<br />
First sections denoting the origin are typed in capitals, while the other sections only begin with a capital.<br />
<br />
Both Fenix and Bennu have the convention to mostly use an underscore ("_") in names, so this is allowed too. It's more a matter of taste than practicality:<br />
<code>int numberOfProcessesAlive;</code> versus <code>int number_of_processes_alive</code>.<br />
<br />
=== Constants ===<br />
Constants are written in all uppercase.<br />
<br />
Like: [[C_SCREEN]], [[NET_ERROR_CONNECTING]], [[B_ALPHA]]<br />
<br />
=== Variables ===<br />
Variables are written in mostly lowercase, whether they are private, public, local or global. When defining a [[struct]], the name of the struct start with a capital and is accessed with a capital as well. For example "Mouse.left". "Mouse" is a struct, so it's typed with a capital and "left" is a normal variable, so it's in lowercase.<br />
<br />
The [[datatype]] of the variables are ''always'' noted when declaring them, even when they're integers.<br />
<br />
Like: [[ctype]], [[graph_mode]], [[Scroll]], numberOfProcessesAlive<br />
<br />
=== Types ===<br />
Types are written in both cases, but mostly in lowercase. A type starts with an underscore ("_") or with a capital.<br />
<br />
Like: [[Network.dll]] has the type "_IPaddress", _point, Point<br />
<br />
=== Defines ===<br />
Defines are, like constants, written in all uppercase. On top of this, the following way of defining can be used, to prevent multiple includes of the same file. Note the "__".<br />
<pre><br />
#ifndef __MYFILENAME_MYFILEEXTENSION<br />
#define __MYFILENAME_MYFILEEXTENSION<br />
// Place the file's code here.<br />
#endif<br />
</pre><br />
This is done in [[Network.dll]]:<br />
<pre><br />
#ifndef __NETWORK_BH<br />
#define __NETWORK_BH<br />
// Network.fh code<br />
#endif<br />
</pre><br />
<br />
=== Functions and Processes ===<br />
Functions are written in both cases. Defining a function or process goes like so:<br />
<pre>keyInput( int keycode, int pointer status)</pre><br />
The opening parenthesis ("(") comes directly after the function name, followed by a space (" ") and the [[datatype]] of the first parameter, before a space and the name of the first parameter. Then a comma (",") and the whole thing starts again, ending with a closing parenthesis (")").<br />
<br />
Processes start with a capital, both in the declaration and when calling.<br />
<br />
== Calling functions and processes ==<br />
Calling a function or process isn't bound to any rules, because in many cases, the clarity of the code is different, even though the code is written the same. So just best suit the layout of the code to the environment. In general though, a good look is created by the use of a space (" ") before every [[argument]].<br />
<br />
== Control-flow keywords ==<br />
Control-flow keywords can be typed in two ways. The difference denotes the importance of the statement. The important control-flow statements are typed with the first character a capital, while the less important ones are only lowercase.<br />
<br />
== Example ==<br />
<pre><br />
import "mod_key"<br />
import "mod_wm"<br />
import "mod_proc"<br />
<br />
Const<br />
KEY_TARGET = _A;<br />
End<br />
<br />
Global<br />
int keyPressed;<br />
End<br />
<br />
Process KeyInput( int keycode, int pointer status)<br />
Begin<br />
Loop<br />
if(key(keycode))<br />
*status = TRUE;<br />
else<br />
*status = FALSE;<br />
end<br />
frame;<br />
End<br />
End<br />
<br />
Process Main()<br />
Private<br />
KeyInput keyInput;<br />
Begin<br />
keyInput = KeyInput(KEY_TARGET, &keyPressed);<br />
Repeat<br />
if(keyPressed)<br />
say("Key pressed!");<br />
end<br />
frame;<br />
Until(exit_status)<br />
OnExit<br />
signal(keyInput, S_KILL);<br />
End<br />
</pre></div>Sandman