Konvencije koda
Obavezni standardi kodiranja za WvdS FPC RAD Suite.
Konvencije imenovanja
Tipovi
| Kategorija | Prefiks | Primjer |
| Klasa | TWvdS* | TWvdSBuildConfig, TWvdSProjectManager |
| Interface | IWvdS* | IWvdSLogger, IWvdSParser |
| Record | TWvdS* | TWvdSBuildResult, TWvdSToolPath |
| Enum | TWvdS* | TWvdSProjectType, TWvdSBuildStatus |
| Exception | EWvdS* | EWvdSFileNotFound, EWvdSParseError |
| Callback | TWvdS* | TWvdSBuildCallback, TWvdSProgressHandler |
Vanjski
API-tipovi (VSCode, Node.js) zadržavaju svoje originalne nazive.
Nazivi Unita
Microsoft-stil Namespaces s WvdS-prefiksom:
WvdS.{Domain}.{Layer}.pas
Primjeri:
WvdS.Build.Models.pas
WvdS.Build.Service.pas
WvdS.Projects.SettingsDialog.pas
WvdS.VSCode.Commands.pas
Layer-Sufiksi
| Sufiks | Sadržaj | Primjer |
| .Models | Records, Enumi, Tipovi | WvdS.Build.Models |
| .Service | Poslovna logika | WvdS.Build.Service |
| .Dialog | WebView-Dijalozi | WvdS.Projects.SettingsDialog |
| .Provider | VSCode API Wrapper | WvdS.Designer.EditorProvider |
Varijable i Parametri
| Kategorija | Prefiks | Primjer |
| Privatna polja | F | FProjectPath, FConfig |
| Parametri | A | APath, AOptions, ACallback |
| Lokalne varijable | Bez | Result, I, Config |
Resourcestrings
Prefiks prema značajci:
| Prefiks | Značajka |
| rsCore* | Core Extension |
| rsBuild* | Build Extension |
| rsProject* | Projects Extension |
| rsDesigner* | UI Designer |
| rsPreview* | UI Preview |
| rsMeta* | UI Meta |
| rsPackaging* | Packaging |
| rsTool* | Toolchain |
Struktura koda
Struktura Unita
unit WvdS.{Feature}.{Layer};
{$mode objfpc}{$H+}
interface
uses
// 1. System Units
SysUtils, Classes,
// 2. WvdS Common
WvdS.System, WvdS.Collections,
// 3. Feature-specifično
WvdS.{Feature}.Models;
type
// Definicije tipova
function PublicFunction(const AParam: string): Boolean;
procedure PublicProcedure(AValue: Integer);
implementation
uses
// Private Uses (samo ovdje potrebne Units)
WvdS.VSCode.Strings;
// Privatni tipovi i varijable
type
TInternalHelper = class
end;
var
InternalState: TObject;
// Implementacije
function PublicFunction(const AParam: string): Boolean;
begin
// ...
end;
procedure PublicProcedure(AValue: Integer);
begin
// ...
end;
initialization
// Inicijalizacija
finalization
// Čišćenje
end.
Struktura klase
type
TWvdSExampleClass = class(TObject)
private
FName: string;
FValue: Integer;
procedure SetName(const AValue: string);
protected
procedure DoInternalWork; virtual;
public
constructor Create(const AName: string);
destructor Destroy; override;
procedure Execute;
property Name: string read FName write SetName;
property Value: Integer read FValue write FValue;
end;
Dokumentacija
(*
@abstract(Kratki opis u jednoj rečenici.)
Detaljni opis svrhe i korištenja.
Može sadržavati više rečenica.
@param(APath Potpuna putanja do datoteke)
@param(AOptions Opcionalna konfiguracija, može biti nil)
@returns(True ako uspješno, False kod greške)
@raises(EWvdSFileNotFound ako datoteka ne postoji)
@raises(EWvdSAccessDenied ako nema prava čitanja)
Security:
- CWE-22: Putanja se validira protiv Path Traversal
@seealso(RelatedFunction)
@seealso(TWvdSRelatedClass)
*)
function ProcessFile(const APath: string; AOptions: TOptions): Boolean;
Inline-Komentari
// Kratki komentar za jednu liniju
Result := CalculateValue;
// Višelinijski komentar za složeniju logiku
// Objašnjava razlog, ne što
if (Value > Threshold) and (Mode = mAdvanced) then
begin
// Moramo ovdje koristiti prošireni algoritam,
// jer jednostavni nije točan kod velikih vrijednosti
Result := AdvancedCalculation(Value);
end;
Konstante umjesto Magic Numbers
// ZABRANJENO
if Length(Name) > 64 then
if Timeout > 30000 then
// ISPRAVNO
const
MAX_PROJECT_NAME_LENGTH = 64;
DEFAULT_TIMEOUT_MS = 30000;
if Length(Name) > MAX_PROJECT_NAME_LENGTH then
if Timeout > DEFAULT_TIMEOUT_MS then
Terminologija
Koristite konzistentne pojmove:
| Koristite | Izbjegavajte |
| Path | Url, Location, Dir (nekonzistentno) |
| Config | Settings, Options, Prefs (nekonzistentno) |
| Create | Make, Build (za objekte) |
| Generate | Create (za izlaz) |
| Validate | Check, Verify (za unose) |
| Initialize | Setup, Init (nekonzistentno) |
| Execute | Run, Process (nekonzistentno) |
Uvlačenje
2 razmaka za uvlačenje (ne tabovi)
begin na vlastitoj liniji kod blokova
end na istoj razini uvlačenja kao pripadajući begin
// ISPRAVNO
procedure Example;
begin
if Condition then
begin
DoSomething;
DoMore;
end
else
begin
DoAlternative;
end;
end;
// ZABRANJENO
procedure Example; begin
if Condition then begin DoSomething; DoMore; end
else DoAlternative;
end;
Duljina linije
// Kod dugih lista parametara
function VeryLongFunctionName(
const AFirstParameter: string;
const ASecondParameter: Integer;
AThirdParameter: TOptions
): Boolean;
// Kod dugih uvjeta
if (FirstCondition) and
(SecondCondition) and
(ThirdCondition) then
begin
// ...
end;
Zabranjeni Patterni
Prazni Exception-Handleri
// ZABRANJENO
try
DoSomething;
except
// Ništa ne raditi - gutanje greške!
end;
// ISPRAVNO
try
DoSomething;
except
on E: Exception do
LogError(rsUnexpectedError, [E.ClassName, E.Message]);
end;
TODO/FIXME u produkcijskom kodu
// ZABRANJENO u main-Branchu
// TODO: Implement this later
// FIXME: This is broken
// DOZVOLJENO samo u Feature-Branchevima, mora se ukloniti prije Mergea
Hardkodirani Stringovi
// ZABRANJENO
ShowMessage('File not found');
ShowMessage('Datei nicht gefunden');
// ISPRAVNO
ShowMessage(rsFileNotFound); // resourcestring
Globalne varijable u Servisima
// ZABRANJENO
var
GlobalConfig: TConfig; // Teško testabilno!
// ISPRAVNO - predavanje parametra
function ProcessWithConfig(const AConfig: TConfig): Boolean;
Testabilnost
Servisi moraju biti testabilni bez VSCode/UI:
// ZABRANJENO - UI u Servisu
procedure ValidateAndShowError(const AName: string);
begin
if not IsValid(AName) then
ShowMessage('Invalid name'); // UI-ovisnost!
end;
// ISPRAVNO - povratna vrijednost
function ValidateName(const AName: string; out AError: string): Boolean;
begin
Result := IsValid(AName);
if not Result then
AError := rsInvalidName;
end;
pas2js Kompatibilnost (VAŽNO)
pas2js NIJE 100% kompatibilan s FPC-om! Ova ograničenja MORAJU se poštivati.
Nepodržane značajke
| Značajka | FPC | pas2js | Zaobilazno rješenje |
class var | ✓ | ✗ | Koristiti Unit-Level Variable |
Inline var | ✓ | ✗ | Deklarirati u var-bloku |
Int64 | ✓ | ✗ | Koristiti Integer |
| | | |