According to Robert C. Martin, while writing code we should always think of it as a story telling and our goal should be telling it as beautifully as possible. It makes not only the code readers happy but also helps developers to address the issues like: readable, less prone to error, easy to change, scalable, testable, fault tolerant etc.
Today, I am going to discuss some of the useful tips that make your functions better in several ways.
1.Do one thing and do it well
Your function should do a single task. But some time while doing a single task we indirectly do other sub tasks also. So the question is are we breaching the rule in that case? The answer might be No if we have maintained the same level of abstraction inside that function.
2.Hide switch statements inside a low level class
Switch statement always does N things and we can not always avoid it. So what we do in that case is that we should hide it somewhere inside the base class or a low level class so we do not have to repeat it. We can achieve it through polymorphism.
3.Keep arguments minimum
It is always a good practice to keep arguments to a function minimum. Because it demands a lot of conceptual power if we use many arguments. It is more harder from testing point of view also. Out argument is even more confusing because we do not usually expect information to be going out through the arguments.
4.Never use flag arguments
If a function have a bool argument it does minimum two things. It does one thing in true and does another in false. So it is a terrible practice.
5.Pass arguments as an object
It is always a good idea to pass arguments as an object instead of passing a long list of arguments.
6.Prefer exceptions over error codes
If we return error codes, we have to deal with multiple conditional statements (if else) that makes code ugly. On the other hand, returning exceptions make job easy and eliminate the necessity of multiple conditional statements. For example:
if you use error code, it looks like:
if you use error exception, it looks like:
7.Choose a good name
It is crucial to give a name that reflects the intention of the function. In case of single argument function (called monad), the function and argument should form a very nice verb/noun pair such as read(book). For the further reading about good naming skills, you can read my previous post here.
8.Make separation between command and query
As I mentioned above, a function should do a single task. So we should define a function in such a way that either it does something (command) or answer something (query) but not both. In other word, either your function should change the state of an object or it should return some information about that object.
public boolean set(string attribute, string value) if(set("username","Robert")) .....
In the above figure, set function sets the value of a named attribute and returns true if it is successful and false if no such attribute exists. This leads to odd if statement on the next line.
These are of course not all of that we should consider while writing a function. I am looking forward to hear from you people if you have any other tips. Thank you for reading my post. Please make comments if you have any thoughts on this and press clap button if you like it.
Full Article: Dhruba Dahal @ Medium
Mobile App Development Best Practices – 03.10
iOS MetaCodable – Supercharge Swift’s Codable implementations with macros meta-programming How to build a Tuist plugin and publish it using...
How to make and use BOM (Bill of Materials) dependencies in Android projects
By using a BOM dependency, you can avoid specifying the versions of each individual library in your app, and let...
Telegram turns 10 years old and revenues stagnate
Telegram seems to want to grow not only through messaging but also through communities, which pretty much means it wants...
MetaCodable – Supercharge Swift’s Codable implementations with macros meta-programming
Supercharge Swift‘s Codable implementations with macros. Overview MetaCodable framework exposes custom macros which can be used to generate dynamic Codable implementations. The core of the framework...
How to get started with Swift Concurrency 🧵 (Beginner Tutorial)
Swift has built-in support for writing asynchronous and parallel code in a structured way. Asynchronous code can be suspended and resumed later,...
Mobile App Development Best Practices – 02.10
Data.ai has summarized the interim results of the year – and once again we have a record. Annual consumer spending...