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.
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.
Published at DZone with permission of Abdul Azeez Idris , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.