Why Clean Code Matters: Insights on Function

Hello everyone! Continuing with my journey of exploring “Clean Code” by Robert C. Martin, I am back with my thoughts on the chapter about Functions. This blog is a continuation of my series on Clean Code insights. If you missed the first post, you can find it here.

So, let’s dive into some key lessons about writing clean and effective functions.

  • Functions Should Be Small: Functions should be small, doing only one thing and doing it well, as they’re easier to read, understand, and maintain. If you notice sections in a function, it’s a sign it’s doing too much—break it into smaller, focused functions instead.

  • Order of Functions in a Class: While there isn’t a strict rule, functions should ideally be written in the same order they are called. This makes reading the code feel natural, like reading a newspaper article.

  • Descriptive Names: A function’s name should clearly say what it does. It doesn’t matter if the name is long; what matters is clarity. A well-named function saves time and confusion later.

As mentioned in the book, “Don’t be afraid to make a name long. A long descriptive name is better than a short enigmatic name.

  • Keep Function Arguments Minimal: The fewer arguments a function has, the better.

    • Best: 0 arguments
    • Good: 1 or 2 arguments
    • Okay: 3 arguments
    • Avoid: More than 3 arguments (it gets confusing).
  • Avoid Flag Arguments (true/false): If a function takes a boolean flag, it’s a sign it’s doing more than one thing. Instead, split it into separate functions.

    # Not recommended
    render(bool isMale):
        if isMale:
            print("Washroom is on the right")
        else:
            print("Washroom is on the left")
    
    # Better
    renderMensWashroomDirection():
        print("Washroom is on the right")
    
    renderWomensWashroomDirection():
        print("Washroom is on the left")
    
  • Use Classes for Many Arguments: If a function requires too many arguments, consider wrapping the arguments in their own class. For example:

    # Too many arguments
    makeCircle(double x, double y, double radius)
    
    # Better
    makeCircle(Point center, double radius)
    
  • Order of Arguments: Be mindful of the order of arguments. A descriptive name for the function can help avoid confusion about the argument order. Example:

     # Confusing
     sendEmail(string message, string recipient, string subject)
    
     # Better
     sendEmailToRecipientWithSubjectAndMessage(string recipient, string subject, string message)
    

    In the better version, the function name itself makes the expected order clear, reducing the chances of mixing up arguments.

  • Worried About Remembering Long Function Names? Don’t be! Modern code editors auto-complete function names, so you don’t have to type them fully every time. It’s better to be clear than to use short names that cause confusion.

  • Delete Unused Functions: If a function is never called, remove it to keep the code clean—no worries, version control (Git) can always bring it back if needed.

  • Error Handling: Functions should do one thing, and if a function is meant to handle errors, it should do only that and nothing else.

  • Single Responsibility Principle One Function = One Task: A function should either do something or answer something, but not both. For example:

    // Not recommended
    public boolean set(String attribute, String value) {
        if (attribute.equals("username")) {
            this.username = value;
            return true;
        }
        return false;
    }
    

    This function sets a value and returns a status, which mixes two responsibilities. Remember the first point: If a function needs to do more than one thing, split it into separate functions.

Final Thoughts

Writing clean functions isn’t just about following rules—it’s about making your code easier to read, maintain, and scale. Small, well-named, and focused functions reduce complexity and make debugging easy.

The key takeaway? Keep it simple, make it clear, and let your code speak for itself.

Fuel My Work

If you’ve found something here helpful, consider buying me a coffee (or helping me keep this server running)! ☕ Support me HERE

Need Guidance or Mentorship?

I’m happy to help! Whether it’s coding, career advice, or tech insights, feel free to reach out to me on Topmate: https://topmate.io/saxenaakansha30