DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Writing Sleek Functions

Writing Sleek Functions

How should you design your functions? Read on for one point of view, which suggests that your functions should be very small.

Abdul Azeez Idris user avatar by
Abdul Azeez Idris
·
Jun. 12, 17 · Opinion
Like (2)
Save
Tweet
Share
1.97K Views

Join the DZone community and get the full member experience.

Join For Free

Your functions must be small. Very small. The number of lines a function take should be small enough that 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 your 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 that 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.  

unit test code style Concept (generic programming) Pass (software) Data Types Task (computing) Book Linux (operating system) ASSIST (computing)

Published at DZone with permission of Abdul Azeez Idris, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • A Brief Overview of the Spring Cloud Framework
  • How To Check Docker Images for Vulnerabilities
  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: