Date post: | 31-Jul-2015 |
Category: |
Software |
Upload: | minh-quang-tran |
View: | 240 times |
Download: | 9 times |
Coding standard - Let’s do itCode 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 ithttp://www.doolwind.com/images/blog/comic/comic-05.jpg
5
These are guidelines exercises, NOT RULES
These are guidelines exercises, NOT RULES
6
No.1Try 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.2Try 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.3Wrap 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.4Only 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
READABLE21
Key Benefits
● Readable● Easier mocking (Testable)● Easier to debug● Demeter’s Law
22
No.5Keep your class “small”
23
200 lines per class10 methods per class15 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