The DRY-ER the BETTER

Charliepatronr
3 min readAug 13, 2020

Do Not Repeat Yourself is one of the most important programming principles. It improves your code’s maintainability, scalability and readibility. But besides these reasons why should we try to keep our code as DRY as possible? I think it should be for ourselves.

When working on a big project or code challenge for what might seem an eternity, before hitting run for the 1,000th time you think to yourself that its gotta be this time, it has to work now. You hit enter and it finally works! After seeing your code run, you feel that rush, you admire your creation for a little bit, and close your laptop for the day. A couple of days or weeks pass and you want to take a look at your project, maybe you might want to see how you tackled a certain problem or algorithm, copying and pasting should be enough. Well now you cant even understand your own code. Why? Did the weekend binge watching the office make you forget everything? Not at all.

I just underwent my first code challenge as a Flatiron School student. Before the actual code challenge we had a mock test to get a hang of things. This test was focused on Object Oriented Programming where we where tasked to create Software Companies instances which handed out “freebies” and Developers who got these freebies. To study for the actual code challenge I wanted to look back at my code for a particular method which returned the company class instance which handed out the most freebies, and see what I had done right! But I was greeted to this…

def self.most_distributed  company_hash = {}  self.all.each do |individual_company|    freebie_counter = 0    Freebie.all.each do |individual_freebie|      if(individual_freebie.company == individual_company)      freebie_counter += 1      end    end    company_hash[individual_company.name] = freebie_counter  end  most_distributed = company_hash.sort_by{|k, v| -v}.first.first  self.all.find do |company|    company.name == most_distributed  endend

No comments, no single-responsability-principle, nada. My code was a mess, sure I solved in the moment, but to read it and actually understand what my train of thought was more than 24 hours ago was pretty time consuming. I started noticing I was repeating the same lines of code throught other methods I had previously written, over and over again, while going through the test. Object Oriented principle states that a method should not exceed around 5 lines of code, I had 16 lines…

After watching my instructors solve the challenge, and reviewing my code again, I noticed that I was not really paying attention to the previous methods I had written, I was simply focused on making each method output what I needed, that not only forced my to write more lines of code, but it made me rely heavily on low-level enumerable methods such as each a lot more than I wanted to.

The code above code of been solved with the following lines…

def freebies
Freebie.all.select do |individual_freebie|
individual_freebie.company == self
end
end
def self.most_distributed
Company.all.max_by do |individual_company|
individual_company.freebies.count
end
end

Two methods, five lines each, DRY.

Be kind to yourself when coding, think about your future self…

--

--