It doesn’t have to be a bug to smell.

“Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. …[Therefore,] making it easy to read makes it easier to write.”
― Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship

Writing software is often a monumental undertaking. In any large project, you could have a codebase with tens of thousands of lines of code—or even more! As the codebase grows, so does the challenge of maintaining it. Without proper structure and clarity, reading the code can quickly turn into a nightmare. Have you ever seen a function named something like “HandleData()” that has a docstring that reads “Handles the data passed as an argument” and is 300 lines long? Were you the one to write it…you were weren’t you? What data are we handling? What does “handling” entail? Why are you like this?

Lessons Learned

Listen, I know you’re telling everyone you read Clean Code by Robert Martin, so am I. However, we both know it was in audio book format in the background at 10% volume as you rewatched The Office or whatever other degenerate stuff you get up to. So let me give you the lowest hanging fruit:

Short Single Purpose Functions

Listen it isn’t like I don’t believe in you, I do! However, writing short function that do one thing can make up for a lot of other bad practices. For example, lets look at an example of terrible code that is improved drastically with this one tip. For example:

def stuff():
    a = 10
    b = 20
    c = a + b

    # Do things
    print(c)

    # Do other things
    d = thing(c)
    print(d)

def thing(e):
    return e * 2  # Double it

That hurt to write. Look at it! However, imagine you are exploring this terrible code, you can just jump to the definition of the “thing()” function and see within seconds what it does. No docstring needed. Imagine if every function in the code is like this, it almost makes it bearable.

Conclusion

I know what you are thinking, “It is already over? Did this guy just monologued for two paragraphs, give one tip, then quit?” Listen, you aren’t going to learn about clean coding in one blog post. However, if you want to get started here is a summary of Clean Code, don’t ask me how I know about it.

Clean Code Summary

Leave a Reply