We are reading code for the most part of our days. Surprisingly, when a new feature needs to be implemented we sometimes forget that our code will be read lots of times in the future (by us or someone else). Dozens of books, articles, posts, etc have been written about improving our software. I've found useful to embrace some of the ideas expressed in these sources as mantras, and have them present at all times.
2. Mantra
• “a sound, word, or phrase that is repeated by
someone who is praying or meditating
• “a word or phrase that is repeated often or that
expresses someone’s basic beliefs”
(Merrian-Webster)
9. Broken window theory
• “When a community tolerates minor examples of
disorder (…) such as broken windows (…) people
are more likely to commit more serious crimes”
Source: http://bit.ly/2ebQEgR
47. Extract classes
• When a function is hard to split (i.e. it has many
parameters)…maybe it should be a class
• Or maybe two classes!
• More on this later
52. 3. “Do one thing, do it
well, and do it only”
(Single Responsibility Principle)
53.
54. What is (or what defines) a
responsibility?
It depends…
55. Functions
1. Its name describes accurately what it does
2. Such name does not have conjunctions (And / Or)
3. It does not have side effects
56.
57.
58. Classes
• Definition of class: “family of functions with a clear
and well defined objective”
• This objective is the responsibility of the class
• Responsibility as a bunch of solutions to an actor
59. Actors / Roles
• Final user
• External systems
• Accountants / Stakeholders
• Software Architects
60. Classes and actors
A class must satisfy the needs of one actor,
and that actor only
61.
62.
63.
64.
65.
66.
67. If we do more than one thing
• Lines of code will grow without control
• Share code between different responsibilities
• Heterogeneous collaborators
• Classes are difficult to test
• Our code will be very hard to change
68. How to do one thing
1. Testing (TDD)
2. Extracting
78. Benefits
• More robust design, as there are more immutable
parts
• We can code without knowing all the details (defer
details)
• Facilitates testing, as we can test the abstraction, not
specifics
• Details that are likely to change are not spread out in
the code, so it’s faster to find them
79. Get details out of your code!
• Data structures (OK, still code :))
• Properties files / YAML
• Environment variables
• Datastores
• Configuration server
84. 5. “Make it work, make it
better, make it pretty”
85. My five mantras
1. Don’t leave broken windows
2. Extract till you drop
3. Do one thing, do it well, and do it only
4. Abstractions in code, details in data
5. Make it work, make it better, make it pretty