28
Coding standard - Let’s do it Code for machine executable but for human readable @tmquang6805

Coding standard let’s do it

Embed Size (px)

Citation preview

Page 1: Coding standard   let’s do it

Coding standard - Let’s do itCode for machine executable but for human readable

@tmquang6805

Page 2: Coding standard   let’s do it

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

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

Look like this ?

4

Page 5: Coding standard   let’s do it

Coding standard - Let’s do ithttp://www.doolwind.com/images/blog/comic/comic-05.jpg

5

Page 6: Coding standard   let’s do it

These are guidelines exercises, NOT RULES

These are guidelines exercises, NOT RULES

6

Page 7: Coding standard   let’s do it

No.1Try CTRL + CC X

Duplicate code is EVIL!!!

7

Page 8: Coding standard   let’s do it

v0.2

8

Page 9: Coding standard   let’s do it

v0.3

9

Page 10: Coding standard   let’s do it

Key Benefits

● Increase re-usable● Keep class small (see later)

10

Page 11: Coding standard   let’s do it

No.2Try to avoid “ELSE”

11

Page 12: Coding standard   let’s do it

Simple example

I work most of days in week, except Sunday!!!

12

Page 13: Coding standard   let’s do it

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

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

No.3Wrap primitive types and string

15

Page 16: Coding standard   let’s do it

16

Page 17: Coding standard   let’s do it

17

Page 18: Coding standard   let’s do it

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

No.4Only one → per line

19

Page 20: Coding standard   let’s do it

properties are harder to mock

No whitespace

Can you READ it ???

20

Page 21: Coding standard   let’s do it

How about this? Maybe you can guess code idea

READABLE21

Page 22: Coding standard   let’s do it

Key Benefits

● Readable● Easier mocking (Testable)● Easier to debug● Demeter’s Law

22

Page 23: Coding standard   let’s do it

No.5Keep your class “small”

23

Page 24: Coding standard   let’s do it

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

Key Benefits

● Single Responsibility● Objective and clear methods● Slimmer namespaces● Avoids clunky folders

25

Page 26: Coding standard   let’s do it

● 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

27

Page 28: Coding standard   let’s do it

28