Skip to content

Code Development Rules

netniV edited this page Dec 25, 2017 · 2 revisions

Code Development Rules

The following is a set of rules to which code development and pull requests should adhere to. This is to maintain a standard format of code across the project. See Cacti Development Standards and PHP Standards Recommendations (PSR) standards.

Basics

The following are some basic code, formatting and styles choices made when writing code for the cacti project. Where examples can be given, these are provided in a subsection below.

  • Remove trailing whitespaces from lines
  • No soft tabs on initial tabbing (use hard tabs \t instead of spaces)
  • Use of sizeof() instead of isset() for array checks
  • Strings should use i18n versions via __('<string>')
  • Pull requests should include an entry in docs/CHANGELOG

SQL Formatting

  • Use db_xxx_prepared functions if passing parameters
  • Keep first part of query with the db_..() function
  • Keywords in SQL Statements should be capitalized
  • FROM, JOIN, WHERE, ON, AND, OR should be on separate line
  • Favor the use of WHERE over HAVING

Examples

sizeof() vs isset()

When checking for array presence, it is better to use sizeof() to verify that you have a result.

if (isset($users)) {

This could should be re-written as follows to check that we have elements, not just whether an variable is defined, not that this does mean the array should be predefined.

if (sizeof($users) > 0) {

SQL formatting

Using the above formatting rules, the following code should be re-formatted:

$tables = db_fetch_assoc('SELECT * FROM information_schema.tables WHERE table_schema = ' . $database_default);

This code should be re-formatting as follows using hard tab indentation before the FROM, WHERE and array lines. The parameter has also been shifted to an array password to the _prepared function in order to prevent SQL injection attacks possible by just tagging the value on the end of the statement.

$tables = db_fetch_assoc_prepared('SELECT * 
	FROM information_schema.tables
	WHERE table_schema = ?',
	array($database_default));