- Building Enterprise JavaScript Applications
- Daniel Li
- 261字
- 2021-07-23 16:31:23
You ain't gonna need it (YAGNI)
Note that the previous logic will indiscriminately return a 400 response, even if the payload is not empty. This is fine because the TDD process encourages you to write the minimum amount of code possible to make the tests pass, and so far we have only written a test for the empty payload scenario.
The rationale behind this is to ensure you are not falling into the trap of coding something that you don't need. This principle has been summarized in the phrase "You ain't gonna need it," or YAGNI, which is a principle that originated from extreme programming (XP). The original states that you should "always implement things when you actually need them, never when you just foresee that you need them". You may have also heard the phrase "do the simplest thing that could possibly work" (DTSTTCPW).
Being disciplined and sticking to this principle yields several benefits:
- It ensures you follow TDD: The tests are written before the code is.
- It saves you time: If we preempt a feature and implement it before it is needed, it may turn out that the feature was not needed after all, or the feature has changed from what you had in mind when you implemented it, or other parts of the code have changed and you'd need to revise your original implementation. In any case, you would have spent time on something that is not useful.
Even when you "know" for sure, make it a habit to follow the YAGNI principle.