The first rule of Fight Club
There is a time and a place for pop culture references in your code. I’m not saying that you should start naming your variables something like
vader_is_cool (although that is still better than
x). The right places to let your imagination run wild are seeds, test data and documentation. There is nothing more annoying than seeing your database filled with names like
qweqweqwe or bumping into
User2 while debugging tests. Wouldn’t it be better if every user had a legit name like Rachel Green or Chandler Bing? Wouldn’t it make you smile if descriptions instead of classic
asd were more like:
Whenever I’m sad, I stop being sad and be awesome instead.
Generating realistic looking data is not just about writing beloved characters names or funny quotes into our code. The important part is having valid values in fields like emails, addresses, passwords, credit card numbers or even geographical coordinates. This seems like a lot of work, one might say. Not to worry! In the never ever disappointing world of Ruby gems there is a true treasure called Faker.
Fake it till you make it
The beauty of faker is that using it is truly a piece of cake. You simply install it –
gem install faker and add
require 'faker' in a file you want to populate some magic data ✨
Populate your database
Let’s say you have a User model and need some data for your development environment. Let’s have some fun with
seeds.rb file. The simplest way to generate some real data is with something like that:
But doesn’t it seem like waaaay less work to go with that?
Using faker is not only great because it’s easier to write while creating more easy-to-work-with data. It’s cool because you don’t have to think about names and quotes that won’t embarrass you in front of your team, also you don’t waste time on googling Batman’s birthday 🤦🏼️
Testing is not the most exciting thing. It’s not very challenging and you keep writing the same test cases. The thing that makes it even less appealing is creating boring factories.
I mean what’s fun about this?
Thankfully tests are another place where all of your pop culture friends can come to live. The situation here is similar to generating database seeds and yes you can use faker here as well. Let’s write a sample test.
Show your work
There will come the time in your life when you will have to write documentation to your beautiful code. (Yes, I do believe your code is clean and pretty). Why not add some hidden gems for follow developers here as well? Now that you are all fired up I have to disappoint you…
…no faker here 🙈 Writing docs is a real test for your creativity. In this case, it’s better to stick to one universe — it’s cleaner. So pick your favorite fantasy world, open some fandom wiki and get to work. You know that random laughs in front of the computer that programmers make while coding? That’s for sure from seeing Vader in sample response!
Where we go from there is a choice I leave to you
That line seems familiar, right? Of course it does, it’s from Matrix. We all have seen that one. Putting pop culture references in your code is not just about making jokes. We get excited about things we recognize, things we’ve seen before. It makes us feel comfortable, it makes us feel at home. So why not give a little of that sweet homey feeling to your code?
If you enjoyed this post, please hit the clap button below 👏👏👏