Open In App

Code Bloating in C++ with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

Code bloat is the production of code that is perceived as unnecessarily long, slow, or otherwise wasteful of resources. It is a problem in Software Development which makes the length of the code of software long unnecessarily. So for writing the quality code, we always avoid code bloating in our program.

Following are some reasons for the Code Bloating:

  • Headers: Suppose we have declare some functions in Header File as shown below:




    #include <vector>
      
    // Function to find the sum of elements
    // in a vector
    inline int findSum(vector<int>& arr, int N)
    {
        int sum = 0;
        for (int i = 0; i < N; i++) {
            sum += arr[i];
        }
        return sum;
    }
      
    struct GfG {
      
        // Declare vector arr
        std::vector<int> arr;
      
        // Function
        int execute() const
        {
            return findSum(arr, arr.size());
        }
    };

    
    

    In the above header file, both the functions execute() and findSum() compiles every time when we use the above header file in any source code separately. If we include any header file and doesn’t use any of the function from that header it will still compile the functionality and every other feature of the header files every time which slows the built time of every Software. When the project is large, it contains hundreds of source files, and every function defined in the header compiled hundreds of times. This practice of writing code for Software Development results in Code Bloating i.e., unnecessarily declaration of functionality.

  • Templates:
    In C++ we have built-in templates for functions and containers. Templates are the generalized form of functionality with different data types. Every instance of a template is a completely separate piece of code generated by the compiler.
    Consider below an example for templates:




    template <class T>
    T findSum(const T* arr, int num)
    {
        return accumulate(arr, arr + num, T(0));
    }
      
    template <class T, int N>
    struct GfG {
        T arr[N];
        T run() const
        {
            T res = 0;
            for (int i = 0; i < N; i++)
                res += arr[i];
            return res;
        }
    };

    
    

    In the above template function findSum() is compiled once per each used type of template parameter T.
    The function in GfG::run() is compiled as many times as there are different pairs for which the method run() is called. This function can take more time for changing values of N. There are various techniques to overcome this problem. The problem with headers also applies to templates, because templates are almost always defined in header files.

    There are two ways to overcome this problem:

    1. Define template functions in source file and explicitly instantiate them.
    2. Use extern template declarations in header, combined with explicit template instantiations in source files itself.

Program:




#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    // Code Bloating
    string str("GeeksForGeeks");
  
    // Print string str
    cout << str << endl;
    return 0;
}


Output:

GeeksForGeeks

The above program will work perfectly and will print GeeksForGeeks. But there is a problem of code bloating in above program in first line of main function. The agenda of above program is to print the string GeeksForGeeks, So why we are creating object of string class as it result in creating string class object.
We simply could write

cout << "GeeksForGeeks" << endl; 

Effects of Code Bloat:

  • It creates object of some classes unnecessarily that can make our software slow in execution.
  • It is a problem in Software Development which made the length of code of software long unnecessarily.


Last Updated : 31 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads