Coding standard - Let’s do it
Code for machine executable but for human readable
@tmquang6805
Who Am I
● Trần Minh Quang (tmquang6805)
● Web Developer - Blogger
● Got ZCPE (Since 2014)
● Author
o Phalex (http://tmquang6805.github.io/phalex)
2
Why does my code
SUCK?
Is it READABLE ?
Is it TESTABLE ?
Is it MAINTAINABLE ?
Is it REUSABLE ?
3
Look like this ?
4
Coding standard - Let’s do it
http://www.doolwind.com/images/blog/comic/comic-05.jpg
5
These are guidelines exercises,
NOT RULES
These are guidelines exercises,
NOT RULES
6
No.1
Try CTRL + CC X
Duplicate code is EVIL!!!
7
v0.2
8
v0.3
9
Key Benefits
● Increase re-usable
● Keep class small (see later)
10
No.2
Try to avoid “ELSE”
11
Simple example
I work most of days in week, except Sunday!!!
12
Simple example
I work most of days in week, except Sunday!!!
http://www.tutorialspoint.com/cprogramming/images/if_else_statement.jpg http://www.pilotlogic.com/sitejoom/images/allcommon/pascalbasics/decision_making.jpg
13
Key Benefits
● Helps avoid code duplication
● Easier to read (single true path)
● Reduces cyclomatic complexity
● Keep class small (see later)
14
No.3
Wrap primitive types and string
15
16
17
Key Benefits
● Helps identify what should be an Object
● Type hint
● Encapsulation of operations
● Testable (can mock)
18
No.4
Only one → per line
19
properties are harder to mock
No whitespace
Can you READ it ???
20
How about this? Maybe you can guess code idea
READABLE
21
Key Benefits
● Readable
● Easier mocking (Testable)
● Easier to debug
● Demeter’s Law
22
No.5
Keep your class “small”
23
200 lines per class
10 methods per class
15 classes per package/namespace
Increase to include
docblocks 15-20 lines per method
24
Key Benefits
● Single Responsibility
● Objective and clear methods
● Slimmer namespaces
● Avoids clunky folders
25
● Your code sucks, let's fix it! (http://www.slideshare.net/rdohms/object-
calisthenicstek13)
Reference & Thank to
26
27
28

Coding standard let’s do it