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:

    I don’t have any doubts that there will be a place for progressive white people in this country in the future. I think the paranoia common among white people is very unfounded. I have always organized my life so that I could focus on political work. That’s all I want to do, and that’s all that makes me happy.
    Hettie V., South African white anti-apartheid activist and feminist. As quoted in Lives of Courage, ch. 21, by Diana E. H. Russell (1989)

    So neither the one who plants nor the one who waters is anything, but only God who gives the growth. The one who plants and the one who waters have a common purpose, and each will receive wages according to the labor of each. For we are God’s servants, working together; you are God’s field, God’s building.
    Bible: New Testament, 1 Corinthians 3:7-9.