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