Code Conventions Variables
From MapbenderWiki
Contents |
Variable names
Layout
- first letter lowercase, first letter of each following word uppercase, underscore is forbidden
var goodLayoutButCrappyName; //Javascript $goodLayoutButCrappyName; //PHP
- if the variable name contains an acronym like WMS, only write the first letter of the acronym in uppercase
var myWMSDocument; // bad JS var myWmsDocument; // good JS
- constants: uppercase, all caps with underscores
const NUMBER_OF_PAGES = 100 //Javascript
define("NUMBER_OF_PAGES", 100); //PHP
Good names
- has to describe the represented entity accurately
- as specific as possible
bad temp = ... x = ... good scaleFactor = ...
- problem-oriented (printerReady instead of bitFlag)
- average length: 10 to 20 characters
- put modifier at the end of the name (Max, Average, String etc)
- avoid ambiguities (f.e. numSales refers to a total, salesNum to an index...avoid num if possible)
- i, j, k are integer indices only
- avoid abbreviations at all or include them in the code conventions
- avoid similar names in the same piece of code (like input and inputValue)
- avoid numerals in names (like value1 and value2)
- avoid words that are commonly misspelled (calender and calendar, or receive and recieve)
- avoid multiple natural languages (f.e. english only)
- avoid names of standard types, variables, routines (like then = 1)
- avoid names with hard to read characters (like chartI and chartl)
boolean variables
- names that imply true or false
- suffix like done, error, found, success
- prefix "is" is an option (like isPrinterReady)
- use positive names to avoid double negations (f.e. if (! notFound))
Arrays
- if an array can be replaced by a stack, use stack syntax like push and pop to avoid index trouble
bad $someArray[count($someArray)-1] = $someValue; //PHP someArray[someArray.length-1] = someValue //JavaScript good array_push($someArray, $someValue); //PHP someArray.push(someValue); //Javascript
reconsider in performance relevant scenarios...push and pop are slower.
- use shortcut for Array declarations
bad var someArray = new Array() //Javascript good var someArray = []; //Javascript
Scope
- in PHP: redirect variables from $_GET, $_POST, $_SESSION at the top of your script. Avoid the use of import_request_variables!
Example: $backlink = $_REQUEST["backlink"]; $wfs_conf_id = $_REQUEST["wfs_conf_id"]; $frame = $_REQUEST["frame"]; $filter = $_REQUEST["filter"]; $url = $_REQUEST["url"];
- keep variables alive for as short a time as possible
- begin with minimal scope and expand whenever necessary
- don't assign a value to a variable until just before the value is used
- maybe identify global variables (e.g. by prefix global_)
General advice
- good names avoid need for comments
- use each variable for exactly one purpose
- avoid variables with hidden meanings (like pageCount equals -1 in error case)
- avoid magic numbers, use constants instead (never write for (var i=0; i<99; i++))
- make type conversions obvious
- use booleans to document your program (f.e. by adding a variable that stores the result of a complex boolean expression)
Example:
var isGeomColumn = (parseInt(wfsConf[attr]['element'][elementIndex]['f_geom']) == 1);
if (isGeomColumn) {
...
}
- use true and false (not 0 and 1)
