Skip to content

Instantly share code, notes, and snippets.

@paulghaddad
Last active August 29, 2015 14:14
Show Gist options
  • Save paulghaddad/9ac8456aaf1b12272650 to your computer and use it in GitHub Desktop.
Save paulghaddad/9ac8456aaf1b12272650 to your computer and use it in GitHub Desktop.
LevelUp 7: Employs YAGNI (and knows when to violate it)
1. Explain how YAGNI improves the quality of our code.
YAGNI improves our code in many ways, but the most important reasons are:
- There is less overall code. Less code means it is less likely to break and become spaghetti code; it is also easier to comprehend.
- By focusing on what is absolutely needed right now, we can focus on making the feature as well built as possible. With many priorities, it is more difficult to produce excellent code.
- If we implement a feature that is needed in the future right now, we probably don't understand its exact requirements. If we violate YAGNI and go ahead an build it now, it won't be as good as it could have been if we waited to build in it the future, when we understand the requirements better.
- Our code is not polluted by incorrect guesses that turned out to be wrong but haven't been removed.
In summary, YAGNI increases the quality of our code because it reduces our code to the absolute essentials. A smaller codebase will almost always be of higher quality because it is easier for us to comprehend, test and change.
2. If we follow YAGNI, explain how we can still make intelligent decisions about features we may need.
We can still make intelligent decisions about future features even when we follow YAGNI. First, when deciding whether to implement a feature, it should undergo the following tests:
- Every decision should be made with a cost-benefit analysis. There are many items to consider: the opportunity cost; the investment in resources in building a feature; the cost to maintain the system. Decisions should be made with this lens.
- Will the feature add complexity to the system? If so, it should be added only if it is necessary. If a database abstraction layer isn't needed, then it shouldn't be added.
- Ensure you implement features that are tied to a specific function. If the function is to have a website, a feature may be to create a mobile layout. But if the function doesn't require a mobile layout, then it just increased development time and complexity.
3. Explain the role of proper encapsulation and loose coupling in following the convention.
Proper encapsulation means making the simplest modules, interfaces, classes and methods possible. Moreover, a developer should only add an encapsulation as a component that is needed to fulfill an requirement. If the requirement doesn't call for a specific component in the system, then YAGNI calls for it to not be built at all. For example, if you are building a simple web application that requires using a single database, then there is no need for an encapsulation layer between the application and the database that handles connections to various databases. It is not needed and can always be developed in the future. YAGNI proposes only building encapsulations that are required at the moment.
YAGNI also supports loose coupling in that interactions (which lead to increased coupling) between components should only be built if they are absolutely needed. When developing software, you may guess that two classes will interact in the future. Thus, you build external interfaces between the two classes. But if this is not currently needed, it will simply add coupling in the system. Adhering to YAGNI reduces coupling by eliminating unnecessary connections between components.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment