paint-brush
Object Oriented Tricks: #5 Boy Scout Ruleby@arunsasidharan
5,900 reads
5,900 reads

Object Oriented Tricks: #5 Boy Scout Rule

by Arun (now voidmain.dev)2mMay 3rd, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

<a href="https://hackernoon.com/tagged/oot" target="_blank">OOT</a> is a mini series on writing maintainable <a href="https://hackernoon.com/tagged/object-oriented" target="_blank">Object Oriented</a> code without pulling your hair out. Click here for <a href="https://hackernoon.com/oo-tricks-the-art-of-command-query-separation-9343e50a3de0" target="_blank">trick #1</a>, <a href="https://hackernoon.com/object-oriented-tricks-2-law-of-demeter-4ecc9becad85" target="_blank">trick #2</a>, <a href="https://hackernoon.com/object-oriented-tricks-3-death-by-arguments-d070ac86d996" target="_blank">trick #3</a> &amp; <a href="https://hackernoon.com/object-oriented-tricks-4-starter-pattern-android-edition-1844e1a8522d" target="_blank">trick #4</a>.

Company Mentioned

Mention Thumbnail
featured image - Object Oriented Tricks: #5 Boy Scout Rule
Arun (now voidmain.dev) HackerNoon profile picture

Leave the campground cleaner than you found it.

OOT is a mini series on writing maintainable Object Oriented code without pulling your hair out. Click here for trick #1, trick #2, trick #3 & trick #4.

Entropy

/ˈɛntrəpi/: lack of order or predictability; gradual decline into disorder.

Entropy in physics refers to the amount of “disorder” in a system. The laws of thermodynamics guarantee that the entropy in the universe tends toward a maximum.

While the laws of physics don’t apply to software, entropy hits it hard. When disorder increases in software, programmers call it “software rot.”

The Broken Window Theory

Software rot starts with one broken window.

One broken window, if left unrepaired for a substantial amount of time, instils a sense of abandonment. So another window gets broken. People start littering. Graffiti appears. Serious structural damage begins. In a relatively short time, the building becomes damaged beyond the owner’s desire to fix it, and the sense of abandonment becomes reality.

The Boy Scout Rule

There are times when I have hacked together code because the entire component was somewhat hack-y. This only made the code worse than what it already was and made it tremendously difficult to change it later. In contrast, if you are working on a team/project where the code is cleanly written, well designed, and elegant — you will likely take extra care not to mess it up.

Don’t leave “broken windows” unrepaired. Fix each one as soon as it is discovered. Leave the code cleaner than you found it.

Dealing With Entropy

As Uncle Bob says, it’s not enough to write code well, the code has to be kept clean over time. If we all checked-in our code a little cleaner than when we checked it out, the code simply could not rot. The cleanup doesn’t have to be something big. Change one variable name for the better, break up one function that’s a little too large, eliminate one small bit of duplication, clean up one composite if statement.

Done And Gets Things Smart

Programmers are all smart and get things done but we should cultivate the habit of getting things done and making code smart.

“You want someone who, when you give them a project to research, will come in on Monday and say: “I’m Done, and by the way I improved the existing infrastructure while I was at it.” — Stevey’s Blog Rants

TL;DR

You see evil and you destroy it. Always be refactoring. I’ve found that the following prioritization helps quite a lot:

bugs > refactoring > features

This applies to any programming paradigm, not just OOP. Andy Hunt and Dave Thomas have written about this extensively in their book The Pragmatic Programmer.

Be sure to check it out and tell me your views: Twitter

Check out trick #6 here.

If you liked this post, please hit the little heart! ❤