Code bloat

From Seo Wiki - Search Engine Optimization and Programming Languages

Code bloat is the production of code that is perceived as unnecessarily long, slow, or otherwise wasteful of resources. Code bloat can be caused by inadequacies in the language in which the code is written, inadequacies in the compiler used to compile the code, or by a programmer. Therefore, code bloat generally refers to source code size (as produced by the programmer) but sometimes is used to refer instead to the generated code size or even the binary file size.

Contents

Examples

Often bloated code can result from a programmer who simply uses more lines of code than the optimal solution to a problem.

Some examples of programmer derived code bloat are:

overuse of object oriented (OOP) constructs -- such as classes and inheritance can lead to messy and confusing designs, often taking many more lines of code than an optimal solution.

incorrect usage of design patterns -- OOP developers will often attempt to "force" design patterns as solutions to problems that do not need them

overuse of OOP methods/functions/procedures -- breaking an algorithm up into many methods is a way to allow developers to reuse these methods to solve other problems. However this often adds code bloat and makes the code difficult, if not impossible, to read and debug and reduces algorithmic efficiency.

declarative programming -- implementing a declarative programming style in an imperative or OOP language often leads to code bloat.

excessive use of multiple conditional If statements -- instead of, for instance, using a lookup table

Some naïve implementations of the template system employed in C++ are examples of inadequacies in the compiler used to compile the language.

A naïve compiler implementing this feature can introduce versions of a templated function for every type it is used with. This in turns leads to compiled functions that may never be used, thus resulting in code bloat. More sophisticated compilers and linkers detect the superfluous copies and discard them, reducing the bloat. Thus template code can result in smaller binaries because a compiler is allowed to discard dead code.[1]

Performance implications

In many cases, when two programs implement the same functionality, the larger program will also run slower than the smaller program.
There are a however a few cases where there is a space-time tradeoff -- in these cases, a larger program can run faster than a smaller one.