Good-looking code: tips
Disclaimer: There are lots of awesome books about OOP and how to write good code (for example, 99 bottles of OOP, but there are tons of them)!
However, books take longer time to read, so here I attempted to compose a list of small but very useful articles which address the most crucial (IMO) problems.
OOP
Code smells list
most frequent non-trivial ones: Feature envy
, Primitive obsession
, Data class
(lots of articles across the web, a couple I was able to recall below)
PrimitiveObsession - awesome video
related to DataClass
code smell: Anemic domain model anti-pattern
Composition vs inheritance, 4 articles:
SOLID
A million of articles, what I was able to find in 2 minutes:
- https://thoughtbot.com/blog/back-to-basics-solid
- https://www.honeybadger.io/blog/ruby-solid-design-principles/
- http://marcyliao.me/2018/11/26/solid-in-ruby/
Typical abstractions
https://codeclimate.com/blog/7-ways-to-decompose-fat-activerecord-models/
https://www.sitepoint.com/7-design-patterns-to-refactor-mvc-components-in-rails/ (the same but with better syntax highlighting)
Overkill for small tasks, but good in bigger projects: dry-rb family
For example, on the last project we used dry-container
, dry-auto-inject
, dry-validation
, dry-schema
, dry-struct
(and I think we could also use dry-events
instead of self-made implementation).
Recommend to take a look at, there are some useful abstractions.
Testing
Good general article about testing pyramid
Rspec-specific:
a lot of people overuse fancy stuff like let
, Let’s not helps to understand how to keep the balance between them and pure ruby functions to make specs better