Code Bloating in C++ with Examples

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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #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());
        }
    };

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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;
        }
    };

    chevron_right

    
    

    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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.