Naming conventions for any programming languages.
(but based on PHP during the talk)
Presented at CodeMania01
Bangkok University Room 421 Building 4
27 June 2015
7. PHP Variable name rules
!
!
[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*!
27 June 2015
Review
source: php.net!
7@rzranya
8. PHP Variable name rules
• A variable starts with the $ sign, followed by
the name of the variable!
• A variable name must start with a letter or the
underscore character!
• A variable name cannot start with a number!
• A variable name can only contain alpha-
numeric characters and underscores (A-z,
0-9, and _ )!
• Variable names are case-sensitive ($age and
$AGE are two different variables)!
27 June 2015
Review
source: w3schools.com!
8@rzranya
13. Naming in PHP
• Case sensitive!
– variables!
– constants!
– array keys!
– class properties!
– class constants!
• Case insensitive!
– functions!
– class constructors!
– class methods!
– keywords and
constructs (if, else,
null, foreach, echo
etc.)!
27 June 2015
Review
source: the-echoplex.net!
13@rzranya
14. Naming in JAVA
• Classes!
• Methods!
• Variables!
• Constants!
27 June 2015
Review
14@rzranya
15. Letter case styles
• UPPERCASE!
• lowercase!
• camelCase!
• PascalCase!
• train-case!
• snake_case!
• StUdLyCaPs!
• Acronyms treatment: ALL CAPS!
27 June 2015
Review
15@rzranya
16. Reserved words in JAVA
abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while
27 June 2015
Review
source: oracle.com!
16@rzranya
22. 27 June 2015
PHP Naming Conventions
source: wordpress.com!
22@rzranya
23. • Use camelCase, not underscores, for
variable, function and method names,
arguments;!
• Use underscores for option names and
parameter names;!
• Use namespaces for all classes;!
• Prefix abstract classes with
Abstract. Please note some early XX
classes do not follow this
convention and have not been renamed
for backward compatibility reasons.
However all new abstract classes
must follow this naming convention;!
• Suffix interfaces with Interface;!
• Suffix traits with Trait;!
• Suffix exceptions with Exception;!
• Use alphanumeric characters and
underscores for file names;!
27 June 2015
PHP Naming Conventions
23@rzranya
24. • Use camelCase, not underscores, for
variable, function and method names,
arguments;!
• Use underscores for option names and
parameter names;!
• Use namespaces for all classes;!
• Prefix abstract classes with
Abstract. Please note some early XX
classes do not follow this
convention and have not been renamed
for backward compatibility reasons.
However all new abstract classes
must follow this naming convention;!
• Suffix interfaces with Interface;!
• Suffix traits with Trait;!
• Suffix exceptions with Exception;!
• Use alphanumeric characters and
underscores for file names;!
27 June 2015
PHP Naming Conventions
source: symfony.com!
24@rzranya
25. • Both database tables and columns are named in lower
case.!
• Words in a name should be separated using
underscores (e.g. product_order).!
• For table names, you may use either singular or plural
names, but not both. For simplicity, we recommend
using singular names.!
• Table names may be prefixed with a common token
such as tbl_. This is especially useful when the
tables of an application coexist in the same database
with the tables of another application. The two sets of
tables can be readily separate by using different table
name prefixes.!
27 June 2015
PHP Naming Conventions
25@rzranya
26. • Both database tables and columns are named in lower
case.!
• Words in a name should be separated using
underscores (e.g. product_order).!
• For table names, you may use either singular or plural
names, but not both. For simplicity, we recommend
using singular names.!
• Table names may be prefixed with a common token
such as tbl_. This is especially useful when the
tables of an application coexist in the same database
with the tables of another application. The two sets of
tables can be readily separate by using different table
name prefixes.!
27 June 2015
PHP Naming Conventions
source: yiiframework.com!
26@rzranya
29. INCORRECT:!
function fileproperties() ! !// not descriptive and needs underscore separator!
function fileProperties() ! !// not descriptive and uses CamelCase!
function getfileproperties() ! !// Better! But still missing underscore separator!
function getFileProperties() ! !// uses CamelCase!
function get_the_file_properties_from_the_file() !// wordy!
!
CORRECT:!
function get_file_properties() !// descriptive, underscore separator, and all !
! ! ! ! ! ! !// lowercase letters!
!
INCORRECT:!
$j = 'foo'; ! !// single letter variables should only be used in for() loops!
$Str! ! ! !// contains uppercase letters!
$bufferedText ! !// uses CamelCasing, and could be shortened without losing semantic
meaning!
$groupid ! ! !// multiple words, needs underscore separator!
$name_of_last_city_used // too long!
!
CORRECT:!
for ($j = 0; $j < 10; $j++)!
$str!
$buffer!
$group_id!
$last_city!
27 June 2015
PHP Naming Conventions
source: ellislab.com!
29@rzranya
39. Naming Strategy
DO!
• Variables !what it contains (noun)!
• Class ! !how you call it in general (noun)!
• Constant !type of usage&value (nominal phrase)!
• Function !what it does (verb)!
27 June 2015
Naming Strategy
39@rzranya
40. Naming Strategy
DO!
• File ! !what it applies to the program (noun)!
• Folder !type of files it contains || part name (noun)!
• Table! !Object or action it records
! ! ! !(noun / verb / noun & verb)!
• Field! !what it contains (noun)!
• FK! ! !what it refers to (and may be how)!
27 June 2015
Naming Strategy
40@rzranya
41. Naming Strategy
DO!
Short and meaningful!
!
Avoid programming keywords!
!
English!
27 June 2015
Naming Strategy
41@rzranya
43. Naming Strategy
1. Examine it as object!
2. Write full description!
i. What is is?!
ii. What it does?!
3. Find n. , v. or extract to phrase!
4. Check its distinctness!
27 June 2015
Naming Strategy
43@rzranya