How To Be Lazy — The Beginners Guide.
When I first began my journey as a programmer, there were a few new habits I had to acquire (and continue to acquire) in order to be efficient and successful.
There was one in particular that was highly encouraged in the coding community and was a little confusing to me at a first glance. It was even more confusing when I read it 5 times and heard it another 10. To be a good programmer, you have to be lazy. Us programmers are lazy and must embrace it apparently. So what exactly does this mean? I can tell you when I first read it, I naturally got defensive. I thought I was being called lazy for wanting to become a programmer. Don’t be discouraged! As I began my studies and dipped my toe in the foreign waters, I learned a little bit more of what exactly this means.
Let me break this down into two parts:
The technical side of being a programmer consists of the code you write and the tools used in the process. Turns out you can be lazy and efficient with both!
Let’s take a look at some code to see what being lazy with your code means. I’ll be using Ruby in the short examples below. If you are not familiar with Ruby syntax or have never looked at a snippet of code, just have a lazy state of mind when you start looking into learning how to code:
If you can’t tell what these 5 simple lines of code will do when interpreted by the Ruby language, it is going to print Hello, World! on 5 separate lines. Sure it may be simple and yes maybe we intended to print out 5 repeating lines, but what if it was your best friends birthday and you wanted to print Happy Birthday! 1,000 times for them on your screen? Sure you can use copy and paste, but we are even lazier than that.
Look at the following snippet of code that does just that, but using just one line of code:
This brings up what we call looking for opportunities to “DRY.” DRY meaning Don’t Repeat Yourself. If we can accomplish something without having to repeat ourselves, then lets take that route. Keeps us sane, keeps our code clean, and give any reader of our code less to read.
Lets look at another example that is specific to method definitions in Ruby. Just a tiny bit more complicated (and not very practical, but for visual purposes.) The code shown below has a class with 3 unique instance methods that are short and each have one line in common which confirms the instance variable assignment in the form of a short string message which gets outputted. Remember, we are lazy, which means we can find an easier way to put that repeating “puts” code into all our methods. If we know that we will be creating many methods that share something particular, it is best to remove that method quality and create a new method that can be called within every method that will share that feature:
In these two snippets you will notice the only thing that changed was we created a new method that will be used for printing out our confirmation message. It didn’t make our code much shorter, but since we are lazy, we don’t have to remember to type out “All set to…” at the end of any method we create that needs that confirmation output. Instead, all we need to do is remember to call the confirmation instance method which is responsible for holding that code we don’t want to re-type. Again, not very practical, but imagine if that text was over a paragraph long. One, it would be annoying copying and pasting it in every method definition. And two, we wouldn’t want that paragraph showing up multiple times throughout our code and flooding our code, yuk!
Our code is not the only place to be lazy! Let’s take a look at some of the tools we use as programmers regularly and how we can be more efficient. The following is less technical but just some neat tips and tricks to keep handy, and with practice can go a long way. You’ve probably been staring at your text editor and your terminal for some time now. If you haven’t, you will be one day. The amazing part about most text editors and your terminal is that the creators of them, thought only about the programmers and enriching our experience. There are a ton of keyboard shortcuts available to help create a better programming experience. The cool part about all the different shortcuts available is that as they become second nature to you, you get to strictly focus on the code you are writing and not consume time and energy on navigating your interface. I highly recommend playing around with them and coming up with a nice lazy set up! The images below include some useful terminal shortcuts and some shortcuts for the Atom text editor (you can do a quick google search for shortcuts available for any text editor you are using, but they tend to be universal.)
Atom Text Editor: