Make your code a good code by improving conditional statements

For any developer, writing code is a form of exercise, the more you write it the more you become a pro at it. But is it just writing codes that make one a good developer? Before understanding this, let's first understand what is good code.

Good Code Vs Bad Code

Any code that you are able to compile and successfully run can be considered good code because the end result is the same. Is it? No, that is not the case here as terms like debugging and modifying exist. Along with the same lines, we can define what is good code and what is bad code.

A Good Code is one that has the following traits:

  1. Easily Understandable
  2. Well organized
  3. Runs in less time
  4. Has appropriate comments that explain actions
  5. Uses naming conventions with proper meaning
  6. Well-tested
  7. It should be easily readable for anyone in the future so that any required updates or modifications are done in a seamless manner by other developers.

On the other hand, a Bad Code is one that appears to have been written in a jiffy. The most common traits of a bad code are:

  1. Less or no documentation
  2. Overuse of loops and “if-else” statements
  3. Takes more than the required time in execution
  4. Complex and not easy to read
  5. Has poor coding standards
  6. Not properly tested

This creates a ruckus for any developer to further modify the code for any future requirements.

Therefore, it is of utmost importance to write cleaner code and make things less complex. As Ivar Giaever has rightly said, “One should not make things complicated when a simple explanation will do.”

Although there are a lot of things that need to be kept into consideration while writing good code, in this blog we will discuss how you can make code simple by improving conditional statements.

Improving Conditional Statements

Conditional statements in any programming language are used for taking decisions. The most commonly used conditional command is “if-else”. For example,

  • Return the Condition Value Early: We all are aware of nested conditions and how complex they can become when there are lots of conditions associated with a value. It becomes even more difficult if you are reading that code on a device with a fairly small screen. Let’s take an example of which animal is a baby pet.

The above code looks messy. Therefore, it is important to return the true or false value early without creating much confusion. Instead, you can write the code like this:

  • Use of an array.includes: Now taking the above example only, if we have to select a pet from a pool of animals, how can we proceed with that? If we don’t focus on the code to be a good one, we might write it as:

But won’t it be a long code with as many || operators added as the number of animals present in a group?

Here what steps can be taken to make our code simpler and less complex? We can use array.includes. Let’s see how.

Therefore, for multiple conditions, we can simply use an array.includes and get the job done.

  • Use Function Parameters as Default: Let’s start with an example of how we can eliminate a few conditional statements with the help of default function parameters.

Here the use of the first two “if” statements can be easily omitted. Rather we can use a default function, assign it some value and write the code as follows:

This way a small function can play an impactful role in making your work done in a smaller number of steps.

  • Introduce the Use of array.every for Multiple Choices: Let us suppose the pets that we are going to choose should now also be categorized under the number of legs equal to four. Considering the wide variety of animals we have in our pool, we would have to mention each animal with the number of legs. Such as:

Now, I tell you that the above code can be easily reduced to just one line if you use an array.every, wouldn’t it be a great step up? Have a look.

Several other ways can help you improve the conditional statements and help you write code that is easy to understand, less complex, and even with a smaller number of lines covers every condition involved in a program.

Conclusion

As mentioned above even a bad code can get executed and give us the result. But if we analyze it in detail, it increases the complexity of a code along with the time taken to write it. Hence, if we start giving an eye to detail, starting with the conditional statements, it will simplify our code, reduce its complexity, make it easily readable, and will help in hassle-free modifications in the future.