Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The Variable Naming Convention I use in my projects explained.

NotificationsYou must be signed in to change notification settings

potherca-blog/VariableNamingConvention

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

permalinktitle
/
Variable Naming Convention for weak and/or dynamic typed languages

Project Stage Badge: Production ReadyGitHub tag

  • auto-gen TOC:{:toc}

Introduction

This document explains the naming convention meant to be used withdynamic and/or weakly typed languages (as defined inthe TypingQuadrant on the Cunningham Wiki) such as PHP, Javascript, Perl, and Bash.

The idea behind the coding convention is that a developer should be abletosee when something is wrong. Either when something from anotherscope is used without validation (parameters in public methods, forinstance) or when a certain type is addressed as though it were anothertype.

Stating the use of this Convention

To link to this convention from code, the following header is recommended:

/** * @NOTE: The variable naming scheme used in this code is an adaption of * Systems Hungarian which is explained at http://blog.pother.ca/VariableNamingConvention/ */

The Convention

The convention used for variables is a derived form of SystemsHungarian notation that consist of a 3 part prefix: the Scope of avariable, a Delimiter, and the Type of a variable.

For variables in a closed scope (usually a function) the Scope andDelimiter are omitted and only the Type is prefixed.

A delimiter is added in order to separate the scope from the rest of thename. This delimiter is a conscious eyesore to make variables from otherscopes stand out, as using variables from another scope can lead topainful situations.

Some languages require a variable name to start with asigil. Fordevelopers used to such a language, Hungarian Notation will feel morenatural than for developers used to languages that do not require sigils.

The letters used to denote the Scope and Type are explained in the"Scope" and "Type" sections below.

For the various languages this convention pertains to this gives us:

LanguageExampleSigilScopeDelimiterTypeName
BASH$g_xFoo$g/m/p/t_xFoo
Javascriptg_xFoog/m/p/t_xFoo
Perl$g_xFoo$/@/%/&g/m/p/t_xFoo
PHP$g_xFoo$g/m/p/t_xFoo
Strictly speaking the `$` character used in Bash isn't truly a sigil but "an unary operator for lexical indirection". The effect, however, is much the same.

Scope

To help identify where variables come from and for easier fetching ofname-lists in scope-aware editors, a scope flag is prefixed to variablesthat aren't in a function/method's closed scope.

Available flags for the scope are:

PrefixScopeRational
gglobal
ttemporary variablesvariables that are created only to be temporarily used and then overwritten or discarded, as often occurs in loops.
pfunction parametersThings from "the outside" can oftennot be trusted. This prefix helps to indicate when values move across scopes.
mclass members

Types

PrefixTypeLanguage(s)CategoryNotes
$jQuery ObjectJSOtherOnly relevant when working with code that uses the jQuery Library.[1][2]
aArrayBASH/JS/PHPCompound
bBooleanBASH/JS/PHPScalar
cCallbackBASH/JS/PHPOtherA callback, also known as a Function.[3]
f""""
dDoubleBASH/JS[4]/PHPScalarA double, also know as a "Float", a floating-point number.
iIntegerBASH/JS[4]/PHPScalar
j(reserved)JSOtherSet aside just in case the$ character (for jQuery Objects) is to be replaced. See[2].
mMixedBASH/JS/PHPOther
nNumericBASH/JS/PHPScalarCan be either an integer or a numeric string[5].
oObjectJS/PHPCompound
rResourcePHPOther
sStringBASH/JS/PHPScalar
uUnknownBASH/JS/PHPOther

Notes

  1. In the Angular framework the dollar character$ has other significant, denoting a variable, parameter, property, or method that belongs to the core of Angular and prevent elements from being iterated (or interpreted) in certain directives.
  2. The fact that this character can lead to confusion as it is a sigil in other languages has lead to the idea that it might be better to usej for jQuery objects instead. That idea is still under revision.
  3. In Bash this refers to variables declared with the-f flag.
  4. Javascript only has one "Number" type which is used for both floating-point numbers and integers
  5. A string that has a value that represents an integer or double.
  • Javascript (since ES6) has a "Symbol" type (also know as "Atoms" in other languages, similar to enumeration types).In PHP the closest thing to this is using aConstant.The Bash equivalent is declaring a variable readonly with the-r flag.As Symbols/Constants are the familiar exceptional to the rule and donot adhere to this standard. they should be declared in so called SCREAMING_SNAKE_CASE6

Name

The "Name" part of a variable should describe what the variable is inthe problem domain it represents. Be as descriptive as possible buthold these two rules of thumb in the back of your head when coming upwith a name:

  • Variable names should suggest a property or noun. UserName, Width, etc.
  • Names should be descriptive, but also concise. Wherever possible, keepvariable names to under 3 words or 15 characters but be prepared tosacrifice a few extra characters to improve clarity.

Syntax Diagram

Putting all of this together we get the following syntax diagram:

VariableName ::=  ( [$@%&]* ([gmpt] '_')*  [abcdfijmnorsu$] [A-Z] [a-zA-Z0-9]+ )

Railroad Diagram

Generated using theRailroad Diagram Generator

History

This convention has been evolving ever sincePotherca startedprogramming Perl in the mid-nineties and branched out into PHP in theearly noughties. Yet it was never put into writing until encourage to doso bySander Krause in 2009.

Copyright

Creative Commons Licence
The Variable Naming Convention byPotherca and Sander Krause is licensed under the Creative Commons Attribution 4.0 International License .


[8]ページ先頭

©2009-2025 Movatter.jp