Code Bloat - Common Causes

Common Causes

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

Some reasons for 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 may attempt to "force" design patterns as solutions to problems that do not need them.
  • Not using appropriate encapsulation of solutions to partial problems and thus allowing for their re-use, resulting in code duplication
  • Declarative programming – implementing a declarative programming style in an imperative or OOP language often leads to code bloat.
  • Excessive loop unrolling – without justification through improved performance.
  • 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 method of a template class for every type it is used with. This in turns leads to compiled methods that may never be used, thus resulting in code bloat. More sophisticated compilers and linkers detect the superfluous copies and discard them, or avoid generating them at all, reducing the bloat. Thus template code can result in smaller binaries because a compiler is allowed to discard this kind of dead code.

Some examples of native compiler derived bloat include:

  • Dead code – code which is executed but whose result is never used.
  • Redundant calculations – re-evaluating expressions that have already been calculated once. Such redundant calculations are often generated when implementing "bounds checking" code to prevent buffer overflow. Sophisticated compilers calculate such things exactly once, eliminating the following redundant calculations, using common subexpression elimination and loop-invariant code motion.

Read more about this topic:  Code Bloat

Famous quotes containing the word common:

    “The aim of every political constitution is, or ought to be, first to obtain for rulers men who possess most wisdom to discern, and most virtue to pursue, the common good of the society; and in the next place, to take the most effectual precautions for keeping them virtuous whilst they continue to hold their public trust.”
    —James Madison (1751–1836)

    “This morning I threw up at a board meeting. I was sure the cat was out of the bag, but no one seemed to think anything about it; apparently it’s quite common for people to throw up at board meetings.”
    —Jane Wagner (b. 1935)