smell your code! @ free dimension

84
Smell your Code! Yaser Sulaiman 1 www.free-dimension.com

Upload: yaser-sulaiman

Post on 02-Jul-2015

778 views

Category:

Technology


2 download

DESCRIPTION

This is the public version of an internal presentation I gave at Free Dimension, the company I work for part-time.

TRANSCRIPT

Page 1: Smell your Code! @ Free Dimension

Smell your Code!

Yaser Sulaiman

1 www.free-dimension.com

Page 2: Smell your Code! @ Free Dimension

These slides are based on an internal presentation I gave

@ Free Dimension (FD)

2 www.free-dimension.com

Page 3: Smell your Code! @ Free Dimension

It contained examples from real projects

3 www.free-dimension.com

Page 5: Smell your Code! @ Free Dimension

after removing, or disguising, the real examples

5 www.free-dimension.com

Page 6: Smell your Code! @ Free Dimension

Disguised examples are marked with

6 δ www.free-dimension.com

Page 7: Smell your Code! @ Free Dimension

Our road map

7 www.free-dimension.com

Page 8: Smell your Code! @ Free Dimension

What? Why? How?

8 www.free-dimension.com

Page 9: Smell your Code! @ Free Dimension

If you can take away only 1 lesson…

9 www.free-dimension.com

Page 10: Smell your Code! @ Free Dimension

Don’t Repeat Yourself!

10 www.free-dimension.com

Page 11: Smell your Code! @ Free Dimension

This rule is so important I have to break it

11 www.free-dimension.com

Page 12: Smell your Code! @ Free Dimension

Don’t Repeat Yourself!

12 www.free-dimension.com

Page 13: Smell your Code! @ Free Dimension

What? Why? How?

13 www.free-dimension.com

Page 14: Smell your Code! @ Free Dimension

Code smells?!

14 www.free-dimension.com

Page 15: Smell your Code! @ Free Dimension

Warning signs

15 www.free-dimension.com

Page 16: Smell your Code! @ Free Dimension

16

photo by pj_in_oz

www.free-dimension.com

Page 17: Smell your Code! @ Free Dimension

Surface indications of deeper problems

17 www.free-dimension.com

Page 18: Smell your Code! @ Free Dimension

18

photo by BenChenowethWork

www.free-dimension.com

Page 19: Smell your Code! @ Free Dimension

Surface indications of deeper problems

19 www.free-dimension.com

Page 20: Smell your Code! @ Free Dimension

20 www.free-dimension.com

Page 21: Smell your Code! @ Free Dimension

What? Why? How?

21 www.free-dimension.com

Page 22: Smell your Code! @ Free Dimension

The bigger picture

22 www.free-dimension.com

Page 23: Smell your Code! @ Free Dimension

Software Quality

Code Quality

Code Smells

23

not to scale

www.free-dimension.com

Page 24: Smell your Code! @ Free Dimension

Writing HQ code should be the priority

24 www.free-dimension.com

Page 25: Smell your Code! @ Free Dimension

25 www.free-dimension.com

Page 26: Smell your Code! @ Free Dimension

26 www.free-dimension.com

Page 27: Smell your Code! @ Free Dimension

One path to cleaner code goes through code smells

27 www.free-dimension.com

Page 28: Smell your Code! @ Free Dimension

What? Why? How?

28 www.free-dimension.com

Page 29: Smell your Code! @ Free Dimension

Can you smell code?!

29 www.free-dimension.com

Page 30: Smell your Code! @ Free Dimension

30

photo by lanceball

www.free-dimension.com

Page 31: Smell your Code! @ Free Dimension

There’re many code smells

31 www.free-dimension.com

Page 32: Smell your Code! @ Free Dimension

The following smells are just a subset

32 www.free-dimension.com

Page 33: Smell your Code! @ Free Dimension

They’re some of the smells I smelled while working here

33 www.free-dimension.com

Page 34: Smell your Code! @ Free Dimension

Code Smell #1

34 www.free-dimension.com

Page 35: Smell your Code! @ Free Dimension

DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code

35 www.free-dimension.com

Page 36: Smell your Code! @ Free Dimension

The nastiest code smell

36 www.free-dimension.com

Page 37: Smell your Code! @ Free Dimension

DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code DupIicated code

37 www.free-dimension.com

Page 38: Smell your Code! @ Free Dimension

Example #1

38 www.free-dimension.com

Page 39: Smell your Code! @ Free Dimension

BooksController

& NotebooksController

39 δ www.free-dimension.com

Page 40: Smell your Code! @ Free Dimension

The diff test

40 www.free-dimension.com

Page 41: Smell your Code! @ Free Dimension

41

ignoring whitespace differences

FD Employees Only!

www.free-dimension.com

Page 42: Smell your Code! @ Free Dimension

Example #2

42 www.free-dimension.com

Page 43: Smell your Code! @ Free Dimension

MessagesController

43 δ www.free-dimension.com

Page 44: Smell your Code! @ Free Dimension

class MessagesController extends AppController { function zebra_inbox() { // inbox code } function zebra_view($id = null) { // view message code } }

44 δ www.free-dimension.com

Page 45: Smell your Code! @ Free Dimension

class MessagesController extends AppController { function zebra_inbox() { // inbox code } function elephant_inbox() { // inbox code } function zebra_view($id = null) { // view message code } function elephant_view($id = null) { // view message code } }

45 δ www.free-dimension.com

Page 46: Smell your Code! @ Free Dimension

But then came the penguin, the dolphin, the chimp,…

46 δ www.free-dimension.com

Page 47: Smell your Code! @ Free Dimension

Refactor!

47 www.free-dimension.com

Page 48: Smell your Code! @ Free Dimension

class MessagesController extends AppController { function zebra_inbox() { $this->setAction('inbox'); } function elephant_inbox() { $this->setAction('inbox'); } function inbox() { // inbox code } function zebra_view($id = null) { $this->setAction('view', $id); } function elephant_view($id = null) { $this->setAction('view', $id); } function view($id = null) { // view message code } }

48 δ www.free-dimension.com

Page 49: Smell your Code! @ Free Dimension

Added plus: no duplicated views

49 www.free-dimension.com

Page 50: Smell your Code! @ Free Dimension

50

FD Employees Only!

www.free-dimension.com

Page 51: Smell your Code! @ Free Dimension

Code Smell #2

51 www.free-dimension.com

Page 52: Smell your Code! @ Free Dimension

Too many parameters

52 www.free-dimension.com

Page 53: Smell your Code! @ Free Dimension

How many is too many?

53 www.free-dimension.com

Page 54: Smell your Code! @ Free Dimension

54 www.free-dimension.com

Page 55: Smell your Code! @ Free Dimension

55 www.free-dimension.com

Page 56: Smell your Code! @ Free Dimension

Example

(brace your noses!)

56 www.free-dimension.com

Page 57: Smell your Code! @ Free Dimension

public abstract class AProvider { public abstract void AddToFamily(int familyId, string firstName, string sex, string photo, DateTime? birthDate, string birthCountry, string birthCity, bool living, DateTime? deathDate, string country, string city, string email, string homepage, string mobilePhone, string biography, string jobTitle, string maritalStatus, string spouse, int order, int? fatherId); public abstract void UpdateInFamily(int id, int familyId, string firstName, string sex, string photo, DateTime? birthDate, string birthCountry, string birthCity, bool living, DateTime? deathDate, string country, string city, string email, string homepage, string mobilePhone, string biography, string jobTitle, string maritalStatus, string spouse, int order, int? fatherId); }

57 δ www.free-dimension.com

Page 58: Smell your Code! @ Free Dimension

!

58 www.free-dimension.com

Page 59: Smell your Code! @ Free Dimension

I misunderstood coupling. My bad!

59 www.free-dimension.com

Page 60: Smell your Code! @ Free Dimension

public abstract class AProvider { public abstract void AddToFamily(Person person); public abstract void UpdateInFamily(Person person); }

60 δ www.free-dimension.com

Page 61: Smell your Code! @ Free Dimension

The remaining smells have many examples; check the repository

61 www.free-dimension.com

Page 62: Smell your Code! @ Free Dimension

Code Smell #3

62 www.free-dimension.com

Page 63: Smell your Code! @ Free Dimension

Not following a coding convention/standard/style

consistently

63 www.free-dimension.com

Page 64: Smell your Code! @ Free Dimension

Not following a coding convention/standard/style

consistently

64 www.free-dimension.com

Page 65: Smell your Code! @ Free Dimension

The specifics doesn’t really matter; what matters most is consistency

65 www.free-dimension.com

Page 66: Smell your Code! @ Free Dimension

Code Smell #4

66 www.free-dimension.com

Page 67: Smell your Code! @ Free Dimension

// code.CommentOut();

67 www.free-dimension.com

Page 68: Smell your Code! @ Free Dimension

“Commented-out code is an abomination.”—Uncle Bob

68

photo used with permission of Uncle Bob

www.free-dimension.com

Page 69: Smell your Code! @ Free Dimension

Don’t commit commented-out code

69 www.free-dimension.com

Page 70: Smell your Code! @ Free Dimension

Code Smell #5

70 www.free-dimension.com

Page 71: Smell your Code! @ Free Dimension

// This is a comment

71

redundant

www.free-dimension.com

Page 72: Smell your Code! @ Free Dimension

What? Why? How?

72

Comments should focus on

www.free-dimension.com

Page 73: Smell your Code! @ Free Dimension

What? Why? How?

73 www.free-dimension.com

Page 74: Smell your Code! @ Free Dimension

I may have come off as an a$$

74 www.free-dimension.com

Page 75: Smell your Code! @ Free Dimension

My code smells as bad as everyone else’s.. sometimes even worse

75 www.free-dimension.com

Page 76: Smell your Code! @ Free Dimension

But I’m aware of my smells

76 www.free-dimension.com

Page 77: Smell your Code! @ Free Dimension

I try my best to prevent them

77 www.free-dimension.com

Page 78: Smell your Code! @ Free Dimension

I don’t ignore them; I deal with them

78 www.free-dimension.com

Page 79: Smell your Code! @ Free Dimension

I smell my code

79 www.free-dimension.com

Page 80: Smell your Code! @ Free Dimension

You should smell your code too

80 www.free-dimension.com

Page 81: Smell your Code! @ Free Dimension

If you want to sniff more…

81 www.free-dimension.com

Page 83: Smell your Code! @ Free Dimension

…</presentation> <questions>…

83 www.free-dimension.com

Page 84: Smell your Code! @ Free Dimension

84 www.free-dimension.com