Smell your Code! @ Free Dimension

Preview:

DESCRIPTION

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

Citation preview

Smell your Code!

Yaser Sulaiman

1 www.free-dimension.com

These slides are based on an internal presentation I gave

@ Free Dimension (FD)

2 www.free-dimension.com

It contained examples from real projects

3 www.free-dimension.com

after removing, or disguising, the real examples

5 www.free-dimension.com

Disguised examples are marked with

6 δ www.free-dimension.com

Our road map

7 www.free-dimension.com

What? Why? How?

8 www.free-dimension.com

If you can take away only 1 lesson…

9 www.free-dimension.com

Don’t Repeat Yourself!

10 www.free-dimension.com

This rule is so important I have to break it

11 www.free-dimension.com

Don’t Repeat Yourself!

12 www.free-dimension.com

What? Why? How?

13 www.free-dimension.com

Code smells?!

14 www.free-dimension.com

Warning signs

15 www.free-dimension.com

16

photo by pj_in_oz

www.free-dimension.com

Surface indications of deeper problems

17 www.free-dimension.com

18

photo by BenChenowethWork

www.free-dimension.com

Surface indications of deeper problems

19 www.free-dimension.com

20 www.free-dimension.com

What? Why? How?

21 www.free-dimension.com

The bigger picture

22 www.free-dimension.com

Software Quality

Code Quality

Code Smells

23

not to scale

www.free-dimension.com

Writing HQ code should be the priority

24 www.free-dimension.com

25 www.free-dimension.com

26 www.free-dimension.com

One path to cleaner code goes through code smells

27 www.free-dimension.com

What? Why? How?

28 www.free-dimension.com

Can you smell code?!

29 www.free-dimension.com

30

photo by lanceball

www.free-dimension.com

There’re many code smells

31 www.free-dimension.com

The following smells are just a subset

32 www.free-dimension.com

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

33 www.free-dimension.com

Code Smell #1

34 www.free-dimension.com

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

The nastiest code smell

36 www.free-dimension.com

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

Example #1

38 www.free-dimension.com

BooksController

& NotebooksController

39 δ www.free-dimension.com

The diff test

40 www.free-dimension.com

41

ignoring whitespace differences

FD Employees Only!

www.free-dimension.com

Example #2

42 www.free-dimension.com

MessagesController

43 δ www.free-dimension.com

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

44 δ www.free-dimension.com

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

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

46 δ www.free-dimension.com

Refactor!

47 www.free-dimension.com

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

Added plus: no duplicated views

49 www.free-dimension.com

50

FD Employees Only!

www.free-dimension.com

Code Smell #2

51 www.free-dimension.com

Too many parameters

52 www.free-dimension.com

How many is too many?

53 www.free-dimension.com

54 www.free-dimension.com

55 www.free-dimension.com

Example

(brace your noses!)

56 www.free-dimension.com

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

!

58 www.free-dimension.com

I misunderstood coupling. My bad!

59 www.free-dimension.com

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

60 δ www.free-dimension.com

The remaining smells have many examples; check the repository

61 www.free-dimension.com

Code Smell #3

62 www.free-dimension.com

Not following a coding convention/standard/style

consistently

63 www.free-dimension.com

Not following a coding convention/standard/style

consistently

64 www.free-dimension.com

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

65 www.free-dimension.com

Code Smell #4

66 www.free-dimension.com

// code.CommentOut();

67 www.free-dimension.com

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

68

photo used with permission of Uncle Bob

www.free-dimension.com

Don’t commit commented-out code

69 www.free-dimension.com

Code Smell #5

70 www.free-dimension.com

// This is a comment

71

redundant

www.free-dimension.com

What? Why? How?

72

Comments should focus on

www.free-dimension.com

What? Why? How?

73 www.free-dimension.com

I may have come off as an a$$

74 www.free-dimension.com

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

75 www.free-dimension.com

But I’m aware of my smells

76 www.free-dimension.com

I try my best to prevent them

77 www.free-dimension.com

I don’t ignore them; I deal with them

78 www.free-dimension.com

I smell my code

79 www.free-dimension.com

You should smell your code too

80 www.free-dimension.com

If you want to sniff more…

81 www.free-dimension.com

…</presentation> <questions>…

83 www.free-dimension.com

84 www.free-dimension.com

Recommended