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 moment the very name of Ireland is mentioned, the English seem to bid adieu to common feeling, common prudence, and common sense, and to act with the barbarity of tyrants, and the fatuity of idiots.
    Sydney Smith (1771–1845)

    If common sense had been consulted, how many marriages would never have taken place; if uncommon or divine sense, how few marriages such as we witness would ever have taken place!
    Henry David Thoreau (1817–1862)