Musings Programming

Rubocop Musings

Rubocop helps standardize Ruby code. Rules give you freedom. It’s a paradox, I know. Here are some lessons I learned the hard way.

“Whoooa there! Slow down and pay attention to indentations!”

Rubocop is a nifty tool to make sure everything in your Ruby program is formatted correctly. I love the idea – it reminds me of one of my favorite programs for students, Grammarly, which shows them their spelling and grammatical errors. I also enjoy, my go-to source for citing sources in MLA format.

“Ms., this needs to be in MLA format, right?” Yes, yes, young chillun. Always.

Standards are important for code readability. I understand the importance of standards – as an English teacher, I have had to read upwards of 120 essays at a time, and every little formatting deviance compounds to make a huge difference in my ability to understand the most important part of each essay: the ideas themselves.

My students learn that standards allow them to be free. Rules give you freedom. It’s a paradox, I know. But I will not be judging them based on the kind of font they use, or if they have a fancy cover page or an expensive plastic cover, or the fact that the text is justified instead of left-aligned (and therefore seems “longer”). If their text looks exactly the same as everyone else’s, it is more difficult to tell who wrote it, and this is a good thing because Bias. (If I taught in a college setting, I would require them to put their student ID instead of their name. For reals.)

Standards help me, the evaluator, to determine the grade to give the essay based on the power of their ideas instead of judging the book by its cover. Standards promote equality and justice because they allow for a wider, more universal comprehension and a blank slate where the ideas can shine for themselves.

(I am obliged to point out that this is for academic essays only. Creative writing is a different beast.)

So I’m all on board with Rubocop. Tell me what I did wrong! Yay feedback! I watched the LaunchSchool example video and saw that the speaker only had about 4 offenses (the Rubocop term for deviations from the standards). I figured I might have a few more.

I ran the analyzer on my program. And I received…

87 offenses! Yikes!

Things I learned from this process (because “Success is counted sweetest by those who ne’er succeed”):

  • It is easier to fix errors as you go than all at the end. I think I might run Rubocop earlier and more regularly. Figuring out where all the white spaces are is annoying, and changing out the ” symbol for ‘ is &%$!. I could probably write some kind of code to fix it all at once, but I’m not sure how to do that yet. So it’s good to know as soon as possible what works, what doesn’t, and what things need to be changed. Launch School really recommends breaking the problems down and being systematic.
  • I really liked being able to highlight a block of code and indent it all at once in order to make a loop, but it created whitespace, so… this is why planning is important.
  • I learned how to make a block of code shorter by using one-liners for my if statements.
  • Formatting helps readability. It really did look better when I was done.