Standards are a set of rules that you should follow to ensure that the code you write is easily readable not only you but others as well. This is a must if you want to apply for SRL Members or even want help on your code, it allows people to read and understand what your doing, so if you need help, others can easily find the problem, if you apply for members, people can read what you do so they can decide if your ready. Overall it makes your code look a lot neater, nicer to look at and much easier to read.
This tutorial is for people who want to use proper standards in their scripts but want examples on how they should do them
I realised that there was not already a tutorial which done this, so i decided to write it
All information has come from the original standards post from Kaitnieks http://www.kaitnieks.com/scar/scriptingsta/
Indenting will be two spaces per level. You can indent by selecting text in Simba and pressing Tab and decrease the indent by pressing Shift+Tab.
Simba Code:
{YES}
begin
Writeln('Hi');
Writeln('This is indented');
end;
{NO}
begin
Writeln('Hi');
Writeln('This is not indented');
end;
Margins will be set to 80 characters. Try not to make too long lines.
Simba Code:
{YES}
i := SomeProcedure(x, y, f, r, t) + SomeProcedure(x, y, f, r, t) +
SomeProcedure(x, y, f, r, t) + SomeProcedure(x, y, f, r, t) +
SomeProcedure(x, y, f, r, t) + SomeProcedure(x, y, f, r, t);
{NO}
i := SomeProcedure(x, y, f, r, t) + SomeProcedure(x, y, f, r, t) + SomeProcedure(x, y, f, r, t) + SomeProcedure(x, y, f, r, t) + SomeProcedure(x, y, f, r, t) + SomeProcedure(x, y, f, r, t);
The begin statement appears on its own line. The end statement always matches the begin statement by columns.
Simba Code:
{YES}
begin
begin
Writeln('Hi');
Writeln('This is indented');
end;
end;
{NO}
begin
begin
begin
Writeln('Hi');
Writeln('This is indented');
end;
end;
end;
Do not combine two or more statements on a single line. Write each on its own line.
Simba Code:
{YES}
i := 1;
x := 3;
{NO}
i := 1; x := 3;
Use semicolons at the end of the lines except after var, begin, then, else, repeat, do and before else.
Simba Code:
{YES}
var
i: Boolean;
x: Integer;
begin
if (i) then
Writeln('hi')
else
begin
repeat
i := True;
until(i);
end;
for (x := 0) to 3 do
Writeln(x);
end;
{NO}
var;
i: Boolean;
x: Integer;
begin;
if (i) then;
Writeln('hi');
else;
begin;
repeat;
i := True;
until(i);
end;
for (x := 0) to 3 do;
Writeln(x);
end;
Use spaces after commas and arithmetical signs.
Simba Code:
{YES}
i := 2 + 3 + 5;
{NO}
i := 2+3+5;
There shall never be white space between an open parenthesis and the next character.
Simba Code:
{YES}
Writeln('Correct');
{NO}
Writeln( 'Incorrect' );
Object Pascal language reserved words and key words shall always be completely lowercase. Never capitalize words that Simba displays in bold. It's completely unnecessary and looks ugly!
Simba Code:
{YES}
var
s: string;
i: Integer;
begin
for (i := 0) to 3 do
Writeln(i);
if (i = 3) then
Writeln('i = 3');
repeat
Writeln(s);
until(s = '');
end;
{NO}
Var
s: String;
i: Integer;
Begin
For (i := 0) To 3 Do
Writeln(i);
If (i = 3) Then
Writeln('i = 3');
Repeat
Writeln(s);
Until(s = '');
End;
Routine names shall always begin with a capital letter and be PascalCase for readability.
Simba Code:
{YES}
procedure FormatHardDrive;
{NO}
procedure formatharddrive;
Routines shall be given names meaningful to their content.
Simba Code:
{YES}
procedure Proggy;
{NO}
DoStuffWithStuff
Where possible, procedure and function formal parameters of the same type shall be combined into one statement
Simba Code:
{YES}
procedure Test(x, y, z, a, b, c: Integer; f, g: string);
{NO}
procedure Test(x: Integer; y: Integer; z: Integer; a: Integer; b: Integer; c: Integer; f: string; g: string);
All formal procedure and function parameter names will be meaningful to their purpose.
Simba Code:
{YES}
procedure ScreenCoords(x1, y1, x2, y2: Integer);
{NO}
procedure ScreenCoords(a, b, c, d: Integer);
All functions should always return some value. Do not rely on Simba initializing procedure return value automatically!
Simba Code:
{YES}
function ReturnNumber: integer;
begin
{do stuff}
Result := 123;
end;
{NO}
function ReturnNumber: integer;
begin
{do stuff}
end;
Variables will be given names meaningful to their purpose. That includes local and loop control variables, although in this case single character names are acceptable.
Simba Code:
{YES}
var
BoneDTM, BankSymb, Flax: Integer;
{NO}
var
DTM1, DTM2, DTM3: Integer;
Boolean variable names must be descriptive enough so that their meanings of True and False values will be clear.
Simba Code:
{YES}
var
FoundColor: Boolean;
{NO}
var
a: Boolean;
Variable declarations start in next line after var statement and are indented.
Simba Code:
{YES}
var
x, y, z: Integer;
{NO}
var x, y, z:Integer
Use of global variables is discouraged. However, they may be used when necessary.
Not really a example for this one, but whenever you can use local variables, use them instead of global variables.
DTMs will be marked with dtm prefix.
Simba Code:
{YES}
var
dtmFishingSpot, BoneDtm: Integer;
{NO}
var
FishingSpot, Bone: Integer;
Bitmaps will be marked with bmp prefix.
Simba Code:
{YES}
var
bmpFishingSpot, BoneBmp: Integer;
{NO}
var
FishingSpot, Bone: Integer;
Type names that are reserved words shall be completely lowercase. For other variable names, the first letter shall be uppercase, and the rest shall be camel-capped for clarity.
Simba Code:
{YES}
var
i: Integer;
s: string;
{NO}
var
i: integer;
s: String;
The most likely case to execute in an if/then/else statement shall be placed in the then clause, with less likely cases residing in theelse clause(s). This improves performance as well as readability.
Simba Code:
{YES}
var
b: Boolean;
begin
b := True;
if (b) then
Writeln('b is true')
else
Writeln('b is false');
end;
{NO}
var
b: Boolean;
begin
if (b) then
Writeln('b is true')
else
Writeln('b is false');
end;
If multiple conditions are being tested in an if statement, conditions should be arrange from left to right in order of least to most computation intensive. If condition statement is complex, replace it with function or Boolean variable with a meaningful name. This improves readibility and makes code self-documenting.
Simba Code:
{YES}
var
Bool1, Bool2: Boolean;
begin
Bool1 := True;
Bool2 := False;
if (Bool1 or Bool2) then
Writeln('hi');
end;
{OR}
var
Bool1, Bool2: Boolean;
function OneIsTrue: Boolean;
begin
Result := Bool1 or Bool2;
end;
begin
Bool1 := True;
Bool2 := False;
if (OneIsTrue) then
Writeln('hi');
end;
{OR}
var
Bool1, Bool2, Resultant: Boolean;
begin
Bool1 := True;
Bool2 := False;
Resultant := Bool1 or Bool2;
if (Resultant) then
Writeln('hi');
end;
{NO}
var
Bool1, Bool2: Boolean;
begin
Bool1 := True;
Bool2 := False;
if (Bool2 or Bool1) then
Writeln('hi');
end;
enjoy
~shut