STYLES.md 3.6 KB

Coding Styles

First of all, please use monospaced (fixed-width) fonts.

Naming conventions

Hungarian notations

These are the notations used by this styling guide:

  • T : Ord Types (structs, class, object,... except pointer types)
  • P : Pointer Types
  • p : Pointer Variable
  • a : Array
  • n : Integer Variable
  • b : Boolean Variable
  • d : Double Variable
  • f : Float(aka Single) Variable
  • ch : Char Variable
  • sz : PChar Variable
  • str: ShortStrings, AnsiStrings,... Variable
  • s : Shorthand of str, acceptable uses: Var s: String, Procedure Smt(s: String)
  • r : Record Variable
  • c : Class/Object Variable
  • m_ : Member of Class/Object (Variable)
  • g_ : Global Variable inside Units

Special cases

  • aa : Array of Array aka MD array

Variable/Types

  • CamelCase, prefixed with approtiate hungarian notations, and use nouns
  • Short term variable (In a function/procedure, loop) can skip the above rules
  • MACROS must be capitalized
  • Constants don't need hungarian notations unless they're typed, and name them with nouns

Functions/Procedures

  • CamelCase for public functions (could be used by others)
  • camelCase for private functions (internal functions, shouldn't/couldn't be used by others)

Program

Program Example;  // <-- OPTIONAL, but please don't use them
^--               // First letter of any keyword that's in the main block
                  // has to be capitallized, this makes main block
                  // easier to be recognized
|                 // <-- Blank lines between keywords in the main block
// Keep the code and comments in the 80-line bounary                       |
Uses BlankUnit, ExampleUnit;
                  // Short the units alphabetically, use Unit Namespace
|
Type
^--               // Same rules apply
  TCount = SizeInt; // For all kind of counting <-- Explain everything :P
<>                // Two spaces identing
|                 // Same rules
Procedure WriteSomething(s: String);
^--               // Same rules apply here, blank line however is not
                  // necessary
  Var
  ^--             // As above
    i: Integer;
<><> ^            // Ident by two spaces, no space before ':'
  |               // Blank lines
  Begin
  ^--
    for i := 1 to 10 do begin
  <>              //    ^-- Inline,
      write('Writing ', s);
    <>            //   ^-- no space before ',' and a space after ','
      write(' ', i, ' times')
    <>          ^  ^
    end;

    writeln();
           ^      // When calling a functions, brace yourself, even no args
                  // is passed
    writeln('smt')// <-- No semicolon at end of block
  End;
  ^--

Begin
^--
  writeln('Hello'); // <-- For procedure in RTL, no namespace necessary
<>
  case true of
    false: writeln('Error');
  <>
    true : ExampleUnit.DoSomeThing() <-- no semicolon
  end;

  WriteSomething('Hi') // <-- You must call your function the way you
                       // defined it.
<>
End.

Unit

Unit ExampleUnit;
^--

Interface
^--
  Procedure DoSomething();
<>^--                   ^-- // Empty arg list needs brace too

Implementation
  Procedure doSomethingElse()
            ^--             // Private function
    Begin
    ^--
      writeln('Doing something else')
    End;
    ^--
  |
  Procedure DoSomething();
<>^--
    Begin
  <>^--
      writeln('Doing something');
      doSomethingElse()
    <>                ^--
    End;
    ^--
End.