Extract Value Object

One more on our clean code series (I think every post I make is about clean code, at least I do my best). The last one was about the Null Object Pattern. Today will be about the Extract Value Object. class SummaryCard attr_accessor :reviews def initialize(attributes = {}) @stars = attributes.fetch(:stars, nil) @reviews ||= review_scores […]

Read More Extract Value Object

Null Object Pattern

Another one, follows from the clean code series I am making. The last post was about Extract Class. Today we are going to talk about the Null Object Pattern and how we can implement it in ruby. Below we have an example of a music platform subscription pattern with the classes: class User include ActiveModel::Model […]

Read More Null Object Pattern

Extract class

Last post I wrote about the extract method. Today I will show how and when to extract a class. Take this file ball_jugler.rb: require ‘faraday’ class BallJugler BASE_URL = ‘https://balljugler.com/’ def initialize(ball_number, name) @ball_number = ball_number @name = name end def jugle @selected_ball = rand(69) end def show_ball if same_number? puts “You won with the […]

Read More Extract class

Extract method

Following the clean code series, today we will talk about the extract method. This looks a little like the replace variable with query but a bit different. The extract method usually is to remove the extra code lines from a method, as to make it more readable and understand what it does when looking at […]

Read More Extract method

Replace Variable with Query

In the last blog post I showed a way to simply reading the code by using variables explaining what they are doing. Today we will start from where we finished and extract the variables to methods. class Product TAX = 0.05 def initialize(amount:, discount_percentage: 0) @amount = amount @discount_percentage = discount_percentage end def total tax_amount […]

Read More Replace Variable with Query

Explaining variable

I was studying a few weeks back and did a refactoring trail in ThoughBot’s Upcase screen casts. Some of the naming they used were outlandish to me and most of the things there I already knew by learning on the job and watching videos or reading blog posts. One of the things that we can […]

Read More Explaining variable

JSONb with Ruby on Rails

I needed to get something from the database in my job. I tried the current query using PSequel and it gave an error. SELECT * FROM declarations WHERE data->>’some_data’ IS NOT NULL; The way I got the results was through the console with the following: bundle exec rails console declarations_ids = [] Declaration.find_each do |d| […]

Read More JSONb with Ruby on Rails