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
Ifstatements – 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:
“Stiller ... took part in the Spanish Civil War ... It is not clear what impelled him to this military gesture. Probably many factors were combineda rather romantic Communism, such as was common among bourgeois intellectuals at that time.”
—Max Frisch (19111991)
“The common experience is, that the man fits himself as well as he can to the customary details of that work or trade he falls into, and tends it as a dog turns a spit. Then he is part of the machine he moves; the man is lost.”
—Ralph Waldo Emerson (18031882)