Code Bloating in C++ with Examples

31 May, 2020
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 vectorinline 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.
#include <vector>  // Function to find the sum of elements// in a vectorinline 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:Define template functions in source file and explicitly instantiate them.Use extern template declarations in header, combined with explicit template instantiations in source files itself.
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:
Define template functions in source file and explicitly instantiate them.Use extern template declarations in header, combined with explicit template instantiations in source files itself.
Define template functions in source file and explicitly instantiate them.
Use extern template declarations in header, combined with explicit template instantiations in source files itself.
Program:
#include <iostream>using namespace std;  // Driver Codeint main(){    // Code Bloating    string str("GeeksForGeeks");      // Print string str    cout << str << endl;    return 0;}
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.
C++-Misc C++
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
std::string class in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
Header files in C/C++ and its uses
Sorting a Map by value in C++ STL
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
C++ program for hashing with chaining