Smell your Code! @ Free Dimension

958 views

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
958
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
8
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Smell your Code! @ Free Dimension

  1. 1. Smell your Code! Yaser Sulaiman www.free-dimension.com 1
  2. 2. These slides are based on aninternal presentation I gave @ Free Dimension (FD) www.free-dimension.com 2
  3. 3. It contained examples from real projects www.free-dimension.com 3
  4. 4. FD agreed to publish it online under a CC BY-NC-SA license… www.free-dimension.com 4
  5. 5. after removing, or disguising, the real examples www.free-dimension.com 5
  6. 6. Disguised examples are marked withδ www.free-dimension.com 6
  7. 7. Our road map www.free-dimension.com 7
  8. 8. What? Why? How? www.free-dimension.com 8
  9. 9. If you can take away only 1 lesson… www.free-dimension.com 9
  10. 10. Don’t Repeat Yourself! www.free-dimension.com 10
  11. 11. This rule is so important I have to break it www.free-dimension.com 11
  12. 12. Don’t Repeat Yourself! www.free-dimension.com 12
  13. 13. What? Why? How? www.free-dimension.com 13
  14. 14. Code smells?! www.free-dimension.com 14
  15. 15. Warning signs www.free-dimension.com 15
  16. 16. photo by pj_in_ozwww.free-dimension.com 16
  17. 17. Surface indications of deeper problems www.free-dimension.com 17
  18. 18. photo by BenChenowethWork www.free-dimension.com 18
  19. 19. Surface indications of deeper problems www.free-dimension.com 19
  20. 20. www.free-dimension.com 20
  21. 21. What? Why? How? www.free-dimension.com 21
  22. 22. The bigger picture www.free-dimension.com 22
  23. 23. Software Quality Code Quality Code Smells not to scalewww.free-dimension.com 23
  24. 24. Writing HQ code should be the priority www.free-dimension.com 24
  25. 25. www.free-dimension.com 25
  26. 26. www.free-dimension.com 26
  27. 27. One path to cleaner code goes through code smells www.free-dimension.com 27
  28. 28. What? Why? How? www.free-dimension.com 28
  29. 29. Can you smell code?! www.free-dimension.com 29
  30. 30. photo by lanceballwww.free-dimension.com 30
  31. 31. There’re many code smells www.free-dimension.com 31
  32. 32. The following smells are just a subset www.free-dimension.com 32
  33. 33. They’re some of the smells I smelled while working here www.free-dimension.com 33
  34. 34. Code Smell #1 www.free-dimension.com 34
  35. 35. DupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated code www.free-dimension.com 35
  36. 36. The nastiest code smell www.free-dimension.com 36
  37. 37. DupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated codeDupIicated code DupIicated code www.free-dimension.com 37
  38. 38. Example #1 www.free-dimension.com 38
  39. 39. BooksController & NotebooksControllerδ www.free-dimension.com 39
  40. 40. The diff test www.free-dimension.com 40
  41. 41. FD Employees Only! ignoring whitespace differences www.free-dimension.com 41
  42. 42. Example #2 www.free-dimension.com 42
  43. 43. MessagesControllerδ www.free-dimension.com 43
  44. 44. class MessagesController extends AppController { function zebra_inbox() { // inbox code } function zebra_view($id = null) { // view message code }}δ www.free-dimension.com 44
  45. 45. 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 }}δ www.free-dimension.com 45
  46. 46. But then came the penguin, the dolphin, the chimp,…δ www.free-dimension.com 46
  47. 47. Refactor! www.free-dimension.com 47
  48. 48. 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 }}δ www.free-dimension.com 48
  49. 49. Added plus: no duplicated views www.free-dimension.com 49
  50. 50. FD Employees Only! www.free-dimension.com 50
  51. 51. Code Smell #2 www.free-dimension.com 51
  52. 52. Too many parameters www.free-dimension.com 52
  53. 53. How many is too many? www.free-dimension.com 53
  54. 54. www.free-dimension.com 54
  55. 55. www.free-dimension.com 55
  56. 56. Example(brace your noses!) www.free-dimension.com 56
  57. 57. 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);}δ www.free-dimension.com 57
  58. 58. !www.free-dimension.com 58
  59. 59. I misunderstood coupling. My bad! www.free-dimension.com 59
  60. 60. public abstract class AProvider{ public abstract void AddToFamily(Person person); public abstract void UpdateInFamily(Person person);}δ www.free-dimension.com 60
  61. 61. The remaining smells have many examples; check the repository www.free-dimension.com 61
  62. 62. Code Smell #3 www.free-dimension.com 62
  63. 63. Not following a codingconvention/standard/style consistently www.free-dimension.com 63
  64. 64. Not following a codingconvention/standard/style consistently www.free-dimension.com 64
  65. 65. The specifics doesn’t really matter; what matters most is consistency www.free-dimension.com 65
  66. 66. Code Smell #4 www.free-dimension.com 66
  67. 67. // code.CommentOut(); www.free-dimension.com 67
  68. 68. “Commented-out code is anabomination.”—Uncle Bob photo used with permission of Uncle Bob www.free-dimension.com 68
  69. 69. Don’t commit commented-out code www.free-dimension.com 69
  70. 70. Code Smell #5 www.free-dimension.com 70
  71. 71. redundant// This is a comment www.free-dimension.com 71
  72. 72. Commentsshould focus on What? Why? How? www.free-dimension.com 72
  73. 73. What? Why? How? www.free-dimension.com 73
  74. 74. I may have come off as an a$$ www.free-dimension.com 74
  75. 75. My code smells as bad as everyone else’s.. sometimes even worse www.free-dimension.com 75
  76. 76. But I’m aware of my smells www.free-dimension.com 76
  77. 77. I try my best to prevent them www.free-dimension.com 77
  78. 78. I don’t ignore them; I deal with them www.free-dimension.com 78
  79. 79. I smell my code www.free-dimension.com 79
  80. 80. You should smell your code too www.free-dimension.com 80
  81. 81. If you want to sniff more… www.free-dimension.com 81
  82. 82. more than just code smells www.free-dimension.com 82
  83. 83. …</presentation> <questions>… www.free-dimension.com 83
  84. 84. www.free-dimension.com 84

×