Why is it important to write the code in components/modules way?

58 views Asked by At

Recently, I get to know that writing code in the modular or components way is important. But I am not sure why is it so important.

Can someone explain why is it important if you know?

1

There are 1 answers

1
Anurag On

code modularity is important for code readability, maintainability and post production support.

If you write a function which has 500 lines of code, it will be very difficult to understand but if you break your 500 lines of code into 10 different functions, it will be easy to understand and debug.

Example: //without code modularity

public float performCalculation(float a, float b)
{
         float result;
         /** writing code calculation1**/
          -
          -

           /** writing code calculation2**/
          -
          -
          -

           /** writing code calculating result**/
          -
          -
          -
          -


}


// with code modularity
public float performCalculation(float a, float b)
{
        float calculation1 = performCalculation1(a,b); // call function performCalculation1
        float calculation2 = performCalculation2(a,b); // call function performCalculation2
        float result = findResult(calculation1 , calculation2 ); // call function findResult
return result; 

}

Decide yourself which code is more readable. Here I have provided a very simple example but just think how will you maintain your code when it becomes huge.

Also, you can go through some online material to learn by yourself on best practices of code modularity.