It might be challenging to create decent, reusable code at times. We may occasionally learn to code in several languages and adhere to certain restrictions or patterns that make sense in that environment. Although there is no right or wrong way to write code without considering the context, there are some straightforward techniques that may be used in many different situations and result in better legible, manageable, and predictable code. Here are a few examples:
1. Removing someone else’s from conditional handling
Ever had a code structure like this, where you have two conditions and then have to handle different codes for each of them:

Well, there’s a different way of dealing with this scenario that can make things easier to read.

In general, you should try to eliminate the 'else' clause from your code. Your code becomes simpler to read and maintain as a result. The code can be made even simpler by deleting the second if if one of the conditions in this situation depends on the other.
2. More declarative code with built-in functions
Let’s say you have an array of numbers and you want to sort them. A common implementation would be the following:

The implementation works, and sometimes you want to build your own implementations. However, in a simple scenario, you might want to use built-in functions for some reasons:
- They make the code smaller, and smaller code tends to be more maintainable (not always true though).
- They already use implementations that are more worked on, so they can be faster, more secure, and known by other developers.
This way, using the array method sort from Java ES6, the code can be greatly simplified and also improve the speed of bigger arrays.

This is also valid for other scenarios, and not only arrays. It’s a good practice to use built-in functions when the scenarios are more generic, and you should avoid them when they don’t fill your application requirements, such as speed, security, and portability. Here are some more array methods.
3. Separate your code into logical blocks
This one is valid not only for JavaScript but for most languages. Let’s say you have the following business scenario: An input array of fruits that needs to be parsed first determines if the fruit is citric or not and then does some processing.

Now, what happens if the separator inside the input changes? Or the types of each fruit? The output? In this scenario, it’s easy to change things, but many times the code is very complex, and each step has complex logic. One way of handling this is by decoupling and identifying the key steps of a process.

Here we identified that the parsing, the handling of the citric fruit, and the output are the steps of the process, and we have decoupled each part, so now if the input changes or some part changes, we have a more robust and easy-to-change code. Business rules are not as easy to determine, but a good step is to speak with the person that is the reference for the product and work around these critical parts, especially in applications that are subject to constant changes.
4. Switch cases with dictionaries
Let’s say you have some filter logic inside your application. A common way of handling this is by using the switch case statement.

There’s nothing wrong with this approach. However, there’s a read that also helps when order matters for the response of each statement. Using dictionaries In JavaScript, the default object is already a dictionary, that is, an unordered array where the elements can be accessed using a key, normally a string or a numeric value. This also means that dictionaries shine in problems where order doesn’t matter, but only access.

Also, the return and the index are not limited by simple strings. Numbers and functions can be applied too, making this pattern usable in many different situations.
5. Use destructuring and spreading for simple object reassignment.
If you’re managing a state, you may eventually find yourself in a situation where you have to manipulate and change or reassign objects. A simple way of avoiding mutation is by simply looping over an object and reassigning it.

With ES6 we have a more simple way of doing this, using the Object.assign method or even simpler spread syntax.

This will create a new Object, and avoid mutation, along with being shorter than the other options.
Wrap Up
You might be able to write faster, more logical, portable, and stable code with the help of these suggestions. There is no right or wrong when developing code, though, because context is necessary. Let's use a linguistic comparison. Would you care if the message you were sending to your coworker in a Slack chat was formal? Obviously not. Whether your colleague is aware of the context or not, you may or may not care that the message is direct and not open to interpretation.
If you are writing an email to a client, you may make the text concise and not open to second interpretation. The same applies to writing code. As a rule of thumb, always try to understand what you want to achieve with the code. If there is no application, there is no code.
Post a Comment
Post a Comment