Table of Contents
Rule of Explicitness (RoE)
Variants and Alternative Names
- Explicit Is Better Than Implicit (EIBTI)1)
Explicit is better than implicit.
Solutions often differ in the level of explicitness. A feature can be implemented explicitly or it can be a side-effect of the implementation of another feature or a more general functionality. The same applies to module communication. A module can invoke another module directly or there can be various forms of indirections like events or observers.
RoE states that explicit solutions are better than implicit ones. Indirection, side-effects, configuration files, implicit conversions, etc. should be avoided.
If something is realized explicitly, it is easier to understand. Implicit solutions require the developer to have a deeper understanding of the module as it is necessary to “read between the lines”. Implicit solutions also tend to be more complex. So explicit solutions are assumed to be less error-prone and easier to maintain.
- Avoid indirection (but keep LC in mind)
- Avoid indirection though events/listeners/observers, etc. and use direct references instead.
- Avoid indirecting middleware like messaging middleware in favor of direct communication. Explicit communication paths are easier to grasp and debug.
- Avoid configurability (but keep GP in mind)
- Avoid using configuration files for specifying behavior. Instead implement varying behavior explicitly.
- Avoid highly configurable modules. Instead implement varying behavior explicitly.
- Explicitly state which module to use
- Avoid importing all classes of a given package/namespace and import the needed classes explicitly. In Java this means not to specify wildcard imports like
import package.*and to avoid static imports. Similarly in Python this means not to use wildcard imports. In C++, do not import entire namespaces (e.g. do not use
using namespace std;).
withstatements in Delphi and other languages having constructs that let you invoke methods without explicitly stating the associated object.
- Explicitly name parameters
- In Python and other languages that allow this use named parameters.
- Avoid long parameter lists and use objects with explicit attribute assignments instead. (see Option-Operand Separation)
- Use parameter types that explicitly state what the input is. Rather use specific types for parameters like customers, articles, URLs, colors, money, etc. instead of using strings or integers for these values (see primitive obsession).
- Avoid implicit type conversions.
- In C# do not to specify implicit cast operations
- In C++ use the
explicitkeyword on single-parameter constructors
- In PHP use the
===operator instead of
==where the type matters
See section contrary principles.
- First without being explicitly stated RoE has been a central design principle of the programming language Python2). Python dates back to 1991.
- Later this philosophy was stated as part of the “Zen of Python”3)
- The rule—although often not stated as such—is also known outside the python community4).
- Extend and origin beyond that remains unclear.
- The name “rule of explicitness” is newly introduced here.
Relations to Other Principles
- More Is More Complex (MIMC): Stating something explicitly requires more code.
- Generalization Principle (GP): RoE often results in specific solutions. Generality often requires stating something implicitly.
- Low Coupling (LC): Direct communication typically has the disadvantage of a higher coupling. Indirection reduces coupling but creates implicit/indirect communication paths.
- Don't Repeat Yourself (DRY): Following RoE sometimes leads to duplication.
- Keep It Simple Stupid (KISS): Explicit solutions are often also simpler.
- Murphy's Law (ML):The typical reason for RoE is to avoid unnecessarily complicated solutions and possibilities for defects. Don't lose sight of that goal.
- Model Principle (MP): RoE states that primitive obsession shall be avoided. Instead more specific types should be used in parameter lists. MP makes this even clearer: In object-orientation objects instead of plain integers or strings are used.
- Law of Leaky Abstractions (LLA): Often abstractions create a level of implicitness. Abstraction leaks are one reason why explicit solutions can be considered preferable.
- Martin Fowler: To Be Explicit
- Tim Peters: The Zen of Python
Discuss this wiki article and the principle on the corresponding talk page.