Trimming your code: for beginners
I’ve read that the only way to minimize bugs in your code, is to simply have less code to begin with! As a beginner, I’ve been more focused on my code working than efficiency. However, I’ve started to notice, the more efficient I can make my code, the greater the chances of it working!
Below are a few tips I’ve picked up by comparing my code (in Ruby) to more experienced coders.
Use a ruby class method
Map, reduce, find, sample, oh my! There are many many many ruby methods that can be used to make your life much easier. Using these methods can save you a lot of loops and if-else statements. However, just using them isn't enough. Keep an eye out for which of these methods would be the MOST EFFICIENT one to use.
The following is something I worked on (fairly simplified below). It shows how I went from a messy code to something neater.
def 1_to_1000 (array) return_array=[]
array_of_ones=array.all.select{|num| num== 1}
array_of_ones.each {|num| return_array<<num*1000}
return_arrayend1_to_1000([1,1,1,2,3,4]) #=>[1000, 1000, 1000]
Then used map and select
def 1_to_1000(array)array.select{|num| num== 1}.map{|num| num*1000}end1_to_1000([1,1,1,2,3,4]) #=>[1000, 1000, 1000]
That looks much better! No need to create a new array and so on. I tried to go a little further and combines the select and map. The internet offered me the reduce method, which would look something like this:
def 1_to_1000(array)array.reduce([]){|return_array, num| return_array<<(num*1000) if num== 1; return_array}end1_to_1000([1,1,1,2,3,4]) #=>[1000, 1000, 1000]
However, I prefer the map and select. Maybe I was taking it too far.
Use the Boolean Data Type
Ruby will evaluate each expression and return a value. For Boolean data, this will return a true or false. Using this directly can help reduce some redundancies.
See if you can skip the if-else or ternary. This is one that I do all the time and when looking at the alternative it looks so much cleaner!
For example, if we need to return a true for a number greater than 10 and false or less we could use an if-else:
def greater_than_10? (number) if number>10
true
else
false
endend
Or a ternary:
def greater_than_10? (number) number>10? true: falseend
But we can take advantage of the statement’s return instead:
def greater_than_10? (number) number>10end
Use the || =
Another way to take advantage of the true or false return is by using the “||=” . This was introduced in my Flatiron Software Engineering course. Googling this operator shows there may be alternative uses with applications that may be beyond my reach at the moment. However, I can show you how I have used it, and how it saved me a bit of lengthy code.
The “||=” will assign a value (like =)
only if what is left of the operator is falsey. Why would we ever need that?? Let’s look below at the adapted Flatiron example:
def greeting(name=nil) name ||= “stranger"
"hello #{name})"endgreeting() #=> "hello stranger"
Here we can see that if the method is called without a name passed in, the name variable is falsey (remember nil is falsey!). This means the function will assign “stranger” to the name. Otherwise, the first line will not be executed and the name that was passed through will not be changed! Pretty neat stuff.
There’s more! Well lemme show you a handy trick I came across for creating hashes. Another example from Flatrion’s course work:
def add_student(name, grade) @roster[grade] ||= []
@roster[grade] << name end
The above method, takes in a student name and their grade and updates a hash where the student grades are the keys and the name will be added to the value array. The ||= can be used to create keys and values in a new hash. The trick is that it helps the code check if a certain key already exists or not. If it does, then put the name in. If it doesn't, make the value array first, THEN put the name in. Saving you from writing some if-else states.
Don’t Get Attached
On a less technical note. I know you spent a long time figuring out exactly how to do something. But hey if there’s a one-word method that makes your hours of work into minutes… use it! Don’t get attached to your verbose and winded way of doing something only because of the time you’ve spent on it! Choose the easy way out.
Hopefully these tips are useful in writing more efficient code. But remember it’s not just the shortest code with the longest chains that is the winner. Be efficient but be effective. Readability is important! If other people can’t understand your code then it may not be very useful in the end after all. Sometimes creating another line to ensure nothing is lost in communication is the best way.