Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • See all Red Hat products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Red Hat OpenShift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • See all technologies
    • Programming languages & frameworks

      • Java
      • Python
      • JavaScript
    • System design & architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer experience

      • Productivity
      • Tools
      • GitOps
    • Automated data processing

      • AI/ML
      • Data science
      • Apache Kafka on Kubernetes
    • Platform engineering

      • DevOps
      • DevSecOps
      • Red Hat Ansible Automation Platform for applications and services
    • Secure development & architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & cloud native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • See all learning resources

    E-books

    • GitOps cookbook
    • Podman in action
    • Kubernetes operators
    • The path to GitOps
    • See all e-books

    Cheat sheets

    • Linux commands
    • Bash commands
    • Git
    • systemd commands
    • See all cheat sheets

    Documentation

    • Product documentation
    • API catalog
    • Legacy documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore the Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Writing Sleek Functions

March 21, 2017
Abdul Azeez Idris

    Your functions must be small, very small. The number of lines a function take should be small enough so the main concept of the function is understood without having to go too far. Beyond having functions that are small, they should do only one thing and one thing alone, and they should do it very well.

    Knowing exactly what that one thing is might be rather difficult, but make sure you function executes a verb. When functions are small, it is easy to give them very descriptive names. The name is not doing much anyways. Function names should be such that the logic of the function is directly explained by the name. Having to comment a function name is a very bad sign and shows that the exact meaning is not given by the function name. Never be afraid to make a function name very long. As far as the name gives meaning. There is really no award for writing short names, which give no meaning. Take time to find a very good name, as it is far better to take the time to create a good name than to have to update later. Consistency in naming functions is also very important.

    Beyond creating functions, passing arguments is one basic thing you have to do to use a function. Ensure you pass very little arguments to a function. Remember to make it as small as possible. Try to create a class for the list of arguments if it is more than two. It allows for easy understanding of the parameters when passed as whole rather than different small parts. Try to make sure your function has no side effect. Let it do what it is supposed to do with no side effects. Ensure that your functions get or set but not both. That means that your method should either access or mutate but never both. This might lead to confusion as that means your function does more than one thing. Remember, we said let your function do only one thing.

    Error Handling is one thing. So ensure you have a separate method that handles that. Then you can have another method that implements the main function. Also, throw exceptions rather than Error codes.
    Do not repeat code. Once you discover a certain code is duplicated, it is time to make it a function and never duplicate functions. However, at the end, the most important is that your functions are testable and are tested. Unit tests for functions should be encouraged and improved.

    These guidelines given here are to guide you while creating your functions. You do not have to get it all the time. You simply need to know that they exist and try to follow them as much as you can refactoring the code along the way until you have the best code that you want.
    This is an excerpt from the Book “Clean Code” By Robert C. Martins


    Whether you are new to Linux or have experience, this cheat sheet can assist you when encountering tasks you haven’t done lately.  

    Last updated: March 20, 2017

    Recent Posts

    • Right-sizing recommendations for OpenShift Virtualization

    • OpenJDK 25 now available in Red Hat Enterprise Linux 10.1

    • Migrating Red Hat Ansible Automation Platform: From RPM to container on Red Hat Enterprise Linux

    • Python 3.9 reaches end of life: What it means for RHEL users

    • Upgrade air-gapped OpenShift with self-signed certificates

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue