Practical Rails Dev Tips to Improve the Code You Produceby@dwestendorf
932 reads
932 reads

Practical Rails Dev Tips to Improve the Code You Produce

by Daniel WestendorfApril 10th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Most tips and tricks for leveling up as a developer focus on knowledge, experience, or communication skills. While these are definitely contributing factors they’re often too abstract for a learner to execute on effectively.
featured image - Practical Rails Dev Tips to Improve the Code You Produce
Daniel Westendorf HackerNoon profile picture

Most tips and tricks for leveling up as a developer focus on knowledge, experience, or communication skills. While these are definitely contributing factors they’re often too abstract for a learner to execute on effectively.

There are no quick shortcuts becoming a better developer, but here are some practices and tools which will definitely help. Here I’ll share some things I did to improve the code I produce, and thusly, the level up as a developer.

These are suggestions not doctrine; Your milage may vary.

Implement and Enforce a Style guide

Ruby is a very expressive language. With this expression comes a million styles of code to accomplish a task. Unifying these styles helps unify a codebase making it easier to grok and more enjoyable to work in. Use rubocop to accomplish this. Rubocop comes with predefined styles you can tweak and refine to your taste. What your code style tastes are isn’t the goal here. Uniformity throughout your codebase is.

Oh, but you have an existing project which is all over the place in code style? Rubocop can ignore existing violations and let you fix them as you go. It will even try to fix your code style violations if you wish.

Enforcing code style can be challenging on solo and team projects. Use automated tools to enforce these styles such as guard-rubocop, CodeClimate, Hound, or others to prevent committing mis-styled code. Avoid code smells with tools like reek.

Collect Performance Metrics Early


You’ve probably read or heard this somewhere:

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” — Donald Knuth

This quote gets referenced a lot, is good advice to live by. The time to optimize typically comes with little warning, so it is good to be well informed when the time comes. Having this information already is immensely helpful when performance becomes a problem, instead of trying to collect it in the heat of an outage.

Check out tools like Librato and Skylight for collecting performance metrics in production. Additionally, see the excellent rack-min-profiler gem for per-request performance metrics.

Avoid Costly Database Queries

Okay, enough with the GIF’s already

If premature optimization is the root of all evil, ignorance of poorly performing code is it’s close relative. When writing Rails code, this usually comes in the form of poorly thought out, unused, or plainly expensive database queries.

Rails performance is usually “fast enough” for most of our webapps. Slow performance can usually be blamed on N+1 queries causing multiple round trips to your database for data. Most performance monitoring tools mentioned perviously will identify these, but there is no reason to avoid them from the get-go. Identify and eliminate these in development using the excellent bullet gem. It will analyze your page loads and tell you when you’re loading data which isn’t being used or opportunities to eliminate N+1 queries.

Make Effective Commits


We’ve all be guilty of making commits with messages which fall short of helpful. Or have malformed code. Or has a debugger call in it. We’re human after all.

Take advantage of git’s pre-commit hooks to analyze you changes and commit message to ensure they’re up to snuff. From validating that your commit message is meaningful, to ensuring your the schema changes exists for that migration you just wrote, to validating your code is following your styleguide. Checkout configurable tools like pre-commit (ruby), and pre-commit (framework), or add your own!

Write Your Migrations with Zero Downtime in Mind

Are you still reading these captions?

You’ll inevitably need to make a code change which alters the structure of your database. These changes might introduce downtime in your app while migrations run locking database commands.

While your apps might not be mission critical and downtime is acceptable, getting in the habit of writing your migrations with zero downtime in mind will be helpful when you need to make a schema change the day after your last maintenance window. Check out some of these great guides to learn how.

Write Full-stack Tests

10/10 would recommend bacon pancakes to level up your breakfast game.

You’re writing tests, right? Unit tests are great for identifying breaking changes, but validation that all the cogs are working correctly together will help you sleep better at night. Full stack tests validate that your models, controllers, views, and front-end code are functioning exactly as your users would expect. Capybara will allow you to run your tests against real browsers like Chrome or Firefox or headless browsers like PhantomJS.

Hiring = Headache

Hiring can be a tedious process. Most of us can agree that it’s not the most enjoyable part of our to-do list. You proudly craft a job description, decide what job boards to post it to, and away you go. As you get back to actually doing the things that are in your job description, the consequences of your actions become very apparent. You take a deep breath and sink into your chair as you watch your inbox explode in front of your tired eyes. It’s only been 24 hours, and there are 140 new applications in front of you.

If reading this makes your hands sweaty; hi, we’re HireLoop. We exist to solve the problem of inefficient hiring. What we are not, is another job board.

HireLoop makes it easy for hiring managers to keep track of applicants in one organized place. Post your open position on any job board you’d like and just have interested applicants send an email to a custom HireLoop address . . . we’ll handle the rest.

Stop abusing your inbox and start efficiently hiring new talent, for free.