loading...

Ruby Readability

leamsii profile image Liam Escribano ・2 min read

Have you ever looked at someone else’s code and thought, “What on earth is this?”. We’ll you’re not the only one, you can have amazing projects but if your code is written poorly, it won’t matter. Take a look at this piece of code for example.

def happy_birthday name,age
  puts "Congrats "+name+" "+"You turned "+age.to_s+" "+"years old today!"
if age>100
  puts "You're full of experience!"
else
  puts "You still have a lot to learn!"
end
end
happy_birthday("Tom", 20)

We will be taking this piece of code and transforming it into something beautiful. Let’s begin with..

Indentation

Sure the code works, even if the two end keywords are right on top of each other. There’s currently a whole war among developers about the proper way of indenting your code. The Spacers believe you should indent with 2 or 4 spaces. The Tabbers believe you need to indent using tabs. Both agree on the importance of proper spacing for readability. Let me show you…

def happy_birthday name, age
  puts "Congrats " + name + " " + "You turned " + age.to_s + " " + "years old today!"
  if age > 100
    puts "You're full of experience!"
  else
    puts "You still have a lot to learn!"
  end
end

Now compare the if statement block with the code above. Through indentation we can clearly show that the if statement is a block of its own with its own scope. Not only that, the variables are also easier to read now.

String Interpolation & Ternary Expressions

def happy_birthday name, age
  puts "Congrats #{name}, you turned #{age} years old today!"
  puts (age > 100 ? "You're full of experience!" : "You still have a lot to learn!")
end
happy_birthday("Tom", 20)

Here, we’ve replaced + and the .to_s method with string interpolation: #{}. We then used a ternary expression to shorten our if statement: You can read more about them here.

You need to be careful when using ternary expressions. Sometimes they can make your code harder to read, use them wisely.

Keyword Arguments

For our final clean up trick we will add keyword arguments to this code. How does it look? I’ll show you:

def happy_birthday(name:, age:)
  puts "Congrats #{name}, you turned #{age} years old today!"
  puts (age > 100 ? "You're full of experience!" : "You still have a lot to learn!")
end
happy_birthday({:name => "Tom", :age => 50})

And here’s our initial code for comparison:

def happy_birthday name,age
  puts "Congrats "+name+" "+"You turned "+age.to_s+" "+"years old today!"
if age>100
  puts "You're full of experience!"
else
  puts "You still have a lot to learn!"
end
end
happy_birthday("Tom", 20)

Notice how our new piece of code looks much cleaner and shorter.

When using keyword arguments the order of the parameters doesn’t matter.

Code readability is important across all languages not just Ruby. So next time you write a piece of code, think to yourself, “is this sexy enough?”.

Thank you for reading!

Posted on by:

leamsii profile

Liam Escribano

@leamsii

I'm a full stack web developer. I love Python and my last name literally means 'writer'.

Discussion

pic
Editor guide