Coding Standards

From DevArt Knowledgebase

DevArt Software Co. – Coding Standards & Best Practices

Version 1.4 – 2023-07-25
Author: Pathum Bandara Premaratne

1. Introduction

Most programmers can write working code—but writing good code is an art. Characteristics of good code:

  • Reliable
  • Maintainable
  • Efficient

Efficiency matters, but not at the cost of reliability or maintainability.

2. Purpose

To produce reliable, maintainable applications, DevArtists must follow consistent standards and best practices. These rules stem from experience and industry guidance.

3. Naming Conventions

Casing Rules

  • PascalCase → `BackColor`, `HelloWorld`
  • camelCase → `backColor`, `userName` *** DO NOT USE ***

Standards

  • Classes, methods, variables, and file names: PascalCase
  • Interfaces: prefix with `I` → `IEntity`
  • Avoid Hungarian Notation (e.g. `m_sName`, `nAge`)
  • Member variables: prefix with underscore → `_Name`
  • Use full words → `Address`, not `addr`
  • Avoid single-letter variables except in tight loops
  • UI Control Prefixes:
Control Prefix
Label Lbl
TextBox Txt
Button Cmd
Grid Grd
Checkbox Chk
RadioButton Rdo
Image Img
Table Tbl
Panel Pnl
Hyperlink Lnk
DropDownList Drp
Validators Val
  • Namespace pattern: `DevArt.Product.Module.Submodule`

4. Indentation and Spacing

  • Use TABs (tab size = 4), no spaces
  • Curly braces `{}` on separate lines
  • One blank line between logical code blocks
  • Comments aligned to code level
  • Operators surrounded by spaces
if (IsVisible == true)
 for (int Counter = 0; Counter < 10; Counter++)
  • Use `#region` for grouping code
  • Private members top of file; public at bottom

5. Programming Practices

  • Method size: ideally ≤ 25 lines
  • Method should do one job only
  • Prefer descriptive method names
  • Use `int`, `string`, `object` (not `Int32`, `String`)
  • Always handle unexpected enum values:
switch (MemberType)
{
  case MemberTypes.Registered: ...
  case MemberTypes.Guest: ...
  default: throw new Exception("Unexpected MemberType");
}
  • Avoid hardcoding – use constants or configs
  • Use `String.Empty` instead of `""`
  • Prefer local variables to shared members
  • Use enums for discrete states
  • Don't expose public fields – use properties
private bool _IsFinished;
public bool IsFinished => _IsFinished;
  • Avoid logic in UI event handlers – delegate to methods
  • Never hardcode paths – use relative, dynamic access

6. Architecture

  • Use layered architecture: UI → Business → DAL
  • Never access DB directly in UI
  • Catch and log DB errors in DAL, rethrow for handling
  • Split solution into multiple assemblies when appropriate

7. ASP.NET Guidelines

  • Limit use of session variables – encapsulate access
  • Do not store large objects in session
  • Use CSS for all styling; never hardcode UI styles

8. Commenting Standards

  • Comment before logic, use `//` or `///`
  • Don't comment every line; code should be self-explanatory
  • If logic is complex or non-obvious, comment generously
  • Use proper spelling, grammar, punctuation
  • Minimize comments if identifiers are meaningful

9. Exception Handling

  • Never catch exceptions silently
  • Avoid generic `catch (Exception)` blocks
try 
{ 
    ... 
}
catch (FileIOException Ex) 
{
    Log(Ex);
    throw;
}
  • Preserve stack trace: use `throw;` not `throw Ex;`
  • Use specific exception types (e.g. `SqlException`)
  • Use separate try/catch blocks for distinct logic chunks

10. Cascading Style Sheets (CSS)

Formatting

  • Use tabs, not spaces
  • One rule per line, selectors on own lines
  • Use lowercase hex → `#fff`
  • Avoid over-qualified selectors: use `.container` not `div.container`

Property Order

  • Display → Position → Box Model → Typography → Other
  • TRBL order: top, right, bottom, left
  • Use vendor prefixes:
.Box
{
    -webkit-box-shadow: ...;
    -moz-box-shadow: ...;
    box-shadow: ...;
}

Font & Value Rules

  • Use numeric font weights → `font-weight: 700;`
  • Avoid units on zero → `margin: 0;`
  • Line-height: unitless unless pixel-specific

Media Queries

@media screen and (max-width: 700px)
{
    .Container
    {
       ...
    }
}

11. JavaScript Guidelines

Formatting

  • Use tabs, not spaces
  • Always use braces `{}` for blocks
  • End all statements with semicolons
  • Prefer `const` or `let` over `var`

Naming and Structure

  • PascalCase for functions, files, variables
  • Prefix member variables with `_`
  • Avoid abbreviations and single-letter names

Best Practices

  • Use single quotes → `'Text'`
  • Prefer array literals → `[]` over `new Array()`
  • Avoid chained logic on one line
if (IsValid)
{
   DoSomething();
}

Switch Statements

switch (event.keyCode)
{
    case 13:
       Submit();
       break;
 
    default:
       Cancel();
       break;
}

Spacing & Multi-line Logic

  • E.g.
let HtmlString = `<p>` + Title +
                 ` and ` + Subtitle +
                 `</p>`;

Event Naming Convention

  • Format: `Element_Action(Sender)`
<a id="LnkSubmit" onclick="LnkSubmit_Click(this)">Submit</a>

Strict Comparison

  • Use `===` & `==` mindfully. Use `===` if you want to check both the value and the type. Use `==` if you are checking only the value.

Iteration

E.g.

for (let Counter = 0, Limit = List.length; Counter < Length; Counter++)
{ 
   ...
}

12. SQL Standards

Object Naming Conventions

Type Example
Table Users (Plural Entity Name)
View UserLogins (Plural Purpose/Entity Name)
Stored Procedure UserGet (EntityAction)
Function CalculateAge (Operation/Action)
Trigger OnUserSave (OnAction)

Keys and Indexes

  • Primary Key: `PKUsers_Id`
  • Foreign Key: `FKUsersEmails`
  • Indexes: `IdxUsers_Email`

Data Types

  • Use `VARCHAR`, `INT`, `DECIMAL` where possible
  • Avoid `MONEY`, use `DECIMAL(10,2)` for prices

Queries

  • Avoid `SELECT *`
  • Always use explicit column lists
  • Format with indentation
  • Use `SET NOCOUNT ON` in SPs
SELECT     Id, Name
FROM       Users
WHERE      IsActive = 1
ORDER BY   Name

Best Practices

  • Avoid CURSORs
  • Use `MERGE`, `EXCEPT`, or `NOT EXISTS` over complex joins
  • Never prefix SPs with `sp_` (reserved)
  • Return empty collections rather than `NULL`
  • Place `DECLARE` and `SET` at top of stored procedure
  • Comment logic generously if complex