This is blog post number 6 about my journey to becoming a software engineer.

As an already experienced developer I was pretty much able to breeze through my assignments so far. Fortunately that changed a little bit recently. Why fortunately? Because I like being challenged. Without challenge there is no space to improve.

Anyone who writes code knows that there are many, many ways one can achieve a goal. For instance, outputting the integers 1 through 10 could be achieved like so in Ruby:

count = 1
10.times do
puts count
count = count + 1
end

One can achieve the same effect with:

for i in 1..10
puts i
end

Both options work. How can we know which option is best? Some of this is just personal preference, while other aspects go into code performance and best practices that have been proven by many developers who are far more intelligent than I am.

Single Responsibility – Orthogonality – Keeping Code Decoupled

All the terms in the heading above have come to signify a kind of independence or decoupling. We want to design code components that are self-contained: independent, and with a single, well defined purpose (The Pragmatic Programmer pages 34 and 35)

In assignment 31 of my Rails Web Development course I had to build a filter that would check every fifth post of my application for spammy content. I achieved this task by coding some logic into the posts controller. It totally worked. However, my mentor Phil Spitler challenged me to think further. He didn’t like the fact that the controller had to deal with filtering. Wouldn’t it be much better if I could simply inject my filter into the controller, and therefore, have two code components that are decoupled from each other? That is exactly what we did!

I won’t make this post any more technical in nature, but I will post the initial and improved version of my app. The first link is my solution and the second link reflects the better choices. Both approaches achieve the same goals, but the second version is a lot better when it comes to following best coding practices.

Initial Approach

Improved Approach

If you are interested in doing the BLOC apprenticeship – don’t fret. My mentor is a bit stricter with me since I already have some coding experience. That is what I love about BLOC, they are very flexible!

Leave a Reply

Your email address will not be published. Required fields are marked *

SiteLock