Refactoring


Names have meaning: 13 ideas on how to improve names in the project

Names have meaning: 13 ideas on how to improve names in the project

As developers, we are often talking about high-level architecture. I mean DDD, hexagonal architecture, and so on. We want to introduce those concepts to our project. We want to do that now! The new concept, new architecture, new language, new framework. We fall into a trap. Technical news is for us like drugs. We want more and more. And we don’t think about the consequences. The goal is noble. We want to have a good quality project which can easily adjust to new requirements. Unfortunately, the way we do that is not always the best. We need evolution, not revolution....

Small code smells

I think every programmer, at least at some point, wanted to resolve some big problems, focus on big architecture and be the one, who will fix the entire world. Did you ever feel that way? I did, more than once. It’s funny because at that moment I feel that I can change application completely, for the better of course ;] I can do some refactoring here and there. I can do that alone, without any help. I see the solution inside my head. It is almost done. But after this moment of excitement, I know that this is a daily...

Ruby Refactoring step by step - part 2

Last time in the article Ruby Refactoring step by step - part 1 we went through the very procedural code in one class to a more object oriented approach. This time we will continue our journey through refactoring. We will cover small object instead of one big class and we will use composition to plug behavior into an object. Let’s start!

Ruby Refactoring step by step - part 1

It was a long break from the last technical article. During that time I was trying many different things. You can look here: Be More - my lifestyle blog in Polish, Woman on Rails YouTube channel and my travel Vimeo channel. It was a great time to discover what I like to do and what not. But back to the topic. I prepared this article for a long time. I can say even too long. I started in 2015 and now you will see the results. Let’s get started. Refactoring is one of my favorite topics. I love to clean...

Lambdas in Ruby

I had situation where I worked with many different (but in some way similar) collections in Ruby. This collections had very similar methods to search elements by name. In this particular example I decided to use lambda. Let me show you how this looks like. Code which I get looks like this: @first_collection.select do |activity| activity.name == 'name' end This is not bad code but in others places I had similar things. Collections (I mean arrays) and names were only changes. Like this: @second_collection.select do |activity| activity.name == 'name1' || activity.name == 'name2' end So I decided to use lambda....

SessionsController refactoring

A few weeks ago I worked on code like this: class SessionsController < ApplicationController respond_to :json, only: [:create] def create @user = User.find_by(email: user_params[:email]) if @user && @user.authenticate(user_params[:password]) @user.token = Session.create(user: @user).token end respond_to { |format| format.json { render_user } } end private def user_params params.require(:user).permit(:email, :password) end def render_user if @user render json: @user, status: :created else render json: { errors: 'Email or password was invalid' }, status: :unprocessable_entity end end end When you look on this code at first time, you probably think that this is not too bad. Class isn’t to long. It have about 25 lines....

Ruby Refactoring – part 1

This time I will change Ruby code. My little monster looks like this: def sum_by_column(data) sum_array = data.group_by do |column| [column[0], column[2]] end.values.map { |item| item.transpose[1].inject(:+) } data.group_by do |column| [column[0], column[2]] end.keys.zip(sum_array).map { |first, last| [first[0], last, first[1]] } end If You know what this code does I say: Wow! I wrote this code but it’s terrible. I must do something with it. OK, lets start from the beginning. This method gets data as an array of integer arrays. Like this: [[1, 2, 3], [4, 5, 6]]. Then groups the data by first and last column and sums by...

CoffeeScript Refactoring – part 1

This will be the first article about refactoring. I love refactoring, so let’s start. I think that the best way to do this is to put some code on the beginning and then change it. Today I would like to show you a piece of CoffeeScript code: if checked_items == all_items $('#myId').prop('checked', true) else $('#myId').prop('checked', false) This code is simple. I check if number of selected items is same as number of all items. And then I select (or not) a checkbox on a web page. As I said it’s simple but can be improved: $('#myId').prop('checked', checked_items == all_items) The...