Single Responsibility Principle on Rails Explained

A few weeks back we had a small drama about SRP. There were some smart comments, some stupid ones and a few funny jokes even, like that for example: https://twitter.com/porras/status/220456288017059840 If I remember correctly it all started with this post. I’ve seen criticism on twitter saying that the post shows shitty code, that it’s more complex than it should be, that User class is definitely the best place to put code that creates a user and so on....

July 9, 2012 · 5 min · Peter Solnica

Get Rid of That Code Smell – Primitive Obsession

This is a post from the Get Rid of That Code Smell series. Primitive Obsession is another popular code smell in Ruby land. It’s very easy, tempting and just feels convenient to use primitive objects to represent various concepts in our code. Here are some primitive classes in Ruby that we like to be obsessed about: Array Hash String Fixnum Float Whenever you use one of these classes in a context where they don’t actually fit being semantically incorrect, that’s when you introduce Primitive Obsession code smell....

June 25, 2012 · 3 min · Peter Solnica

Part of DataMapper 2 Is Done - Announcing Virtus 0.5.0

UPDATE: DataMapper 2 was renamed to Ruby Object Mapper (ROM). For more info check out rom-rb.org I’m happy to announce that Virtus 0.5.0 was released. It’s sort of a milestone for me as Virtus is now considered feature-complete and I’m quite happy with the code. Further development will only focus on bug fixes and small internal clean ups. We plan to extract smaller pieces into separate gems at some point too....

June 10, 2012 · 3 min · Peter Solnica

Get Rid of That Code Smell – Duplication

This is a post from the Get Rid of That Code Smell series. Removing duplication from the code is a seemingly easy task. In many cases it is pretty straight-forward - you look at similar bits of code and you move them to a common method or class that is reusable in other places. Right? No, not really. It is true that code that looks similar might be an indicator that there’s a duplication but it’s not the definitive way of determining the smell....

May 11, 2012 · 4 min · Peter Solnica

Get Rid of That Code Smell - Control Couple

This is a post from the Get Rid of That Code Smell series. If you are serious about Object Oriented Design and respecting Single Responsibility Principle then you definitely want to get rid of Control Couple code smells. In this post I will show you a simple example explaining how to identify and remove control coupling from your code. I like to think about that code smell also in the context of SRP because I like to apply it to every piece of my system - whether it’s a method, a class or a whole library....

April 11, 2012 · 3 min · Peter Solnica

Get Rid of That Code Smell - Attributes

In this post I will show you why using attribute accessors is a code smell in most of the cases. This is a very convenient feature of Ruby but you should only consider using it if you’re implementing a data-like objects which expose, well, data to other parts of your system. A good example is an Active Record object which exposes its attributes. Another good example could be an object which wraps a response from a remote API and through attribute readers gives you access to data returned in that response....

April 4, 2012 · 3 min · Peter Solnica

Get Rid of That Code Smell

While working on DataMapper 2 libraries we are measuring quality of our code with various code metric tools. Dan Kubb has been using this approach successfully for over 2 years in Veritas which resulted in a beautiful and clean code. When I started working on Virtus I decided to embrace all the practices that Dan introduced in his projects. It was a fantastic experience because Virtus was not a green-field project - I extracted a piece of DataMapper 1 and turned it into a standalone library....

March 30, 2012 · 2 min · Peter Solnica

DataMapper-2 Presentation From wroc_love.rb Conference

Once again thank you for the awesome event that took place in Wrocław last weekend - the wroc_love.rb conference. We’ve had fantastic and inspiring talks and many great discussions. It’s clear to me that a new era in our community has started and people are willing to learn and embrace patterns from other languages without being scared that we’re going to lose the “agile” aspect of programming in Ruby and Rails....

March 13, 2012 · 1 min · Peter Solnica

New Virtus Release With Truly Awesome Features

Just a quick announcement that I just pushed a new version of Virtus with support for long awaited features: EmbeddedValue, member type coercions for array/set attributes and ValueObject. Current version is 0.2.0, please give it a try and tell me what you think. Here’s a quick sneak-preview of what you can do with Virtus: class GeoLocation include Virtus::ValueObject attribute :lat, Float attribute :lng, Float end class City include Virtus attribute :name, String attribute :location, GeoLocation attribute :districts, Array[Symbol] end class User include Virtus attribute :name, String attribute :age, Integer attribute :city, City end user = User....

February 8, 2012 · 1 min · Peter Solnica

Yes, You Should Write Controller Tests!

It really surprises me that there are people arguing that writing controller tests doesn’t make sense. Probably the most common argument is that actions are covered in acceptence tests along with checking if views are properly rendered. Right? Right…well that’s just wrong! Are you trying to say that your slow acceptance tests are covering every possible controller action scenario? Are you trying to say that, for instance, every redirect that should take place is tested within an acceptance test?...

February 2, 2012 · 5 min · Peter Solnica