logo
Published on

How to Write Functions Like a Senior Developer - Eight Tips

featured Image
Authors

According to Robert C. Martin, we should always conceive of creating code as a tale, and our objective should be to tell it as elegantly as possible. It not only makes code readers happy, but it also aids developers in addressing concerns like as readability, error resistance, ease of modification, scalability, testability, and fault tolerance. Today, I'll go over some helpful hints that will improve your functions in a variety of ways.

1.Concentrate on one subject and do it well.

Your function should only do one job. However, sometimes we undertake additional subtasks inadvertently while performing a single task. So, are we in violation of the rule in such situation? If we keep the same degree of abstraction inside that function, the answer may be No.

2.Use a low-level class to hide switch statements.

We can't always avoid using the switch statement because it always accomplishes N things. In such situation, we should conceal it someplace within the base class or a low-level class so that we don't have to repeat it. Polymorphism can help us achieve this.

3.Keep your arguments to a bare minimum.

It's generally a good idea to restrict the number of parameters to a function to a bare minimum. Because if we utilise a lot of arguments, it necessitates a lot of conceptual force. It is also more difficult in terms of testing. Our argument is even more perplexing since we don't expect knowledge to be disseminated through debates.

4.Never utilise flag parameters in your code.

When a function has a bool parameter, it must accomplish at least two things. In true, it does one thing, while in false, it does another. As a result, it is a deplorable practise.

5.Use an object to pass parameters.

Passing arguments as an object rather than a long list of parameters is usually a good idea.

6.Error codes are preferred over exceptions.

We have to deal with many conditional statements (if else) if we return error codes, which makes the code unsightly. Returning exceptions, on the other hand, make the work easier and eliminate the need for many conditional statements. If you use error code, for example, it appears like this:

if(deleteConfigurationFromDb(adfsProvider)==E_OK){
  if(deleteConfigurationFromSettingFile(adfsProvider)==E_OK){
  
  }
}

if you use error exception, it looks like:

try{
  DeleteConfigurationFromDb(adfsProvider);
  DeleteConfigurationFromSettingFile(adfsProvider);
}
catch(Exception e){
  logger.log(e.getMessage)
}

7.Make a good name for yourself.

It's critical to give the function a name that accurately represents its purpose. When using a single argument function (also known as a monad), the function and argument should make a pleasant verb/noun pair, such as read (book). You can get more information regarding good naming skills in my earlier post here.

8.Make a distinction between the command and the query.

As previously stated, a function should only do one task. As a result, we should create a function in such a way that it either performs a command or responds to a query, but not both. To put it another way, your function should either modify the state of an object or return information about it.

public boolean set(string attribute, string value)
if(set("username","Robert")) ..... 

Set function sets the value of such a named attribute and returns true if it is successful and false if no such property exists, as shown in the diagram above. This results in an unusual if statement on the next line.

Conclusion

Of course, these aren't the only things to think about while constructing a function. I'm eager to hear from you if you have any other suggestions. Thank you for taking the time to read my article. If you have any ideas on this, please leave a comment and clap if you enjoy it.