Weekly Dev Tips
Guard Clauses
- Autor: Vários
- Narrador: Vários
- Editor: Podcast
- Duración: 0:06:27
- Mas informaciones
Informações:
Sinopsis
Your methods should fail fast, if doing so can short-circuit their execution. Guard clauses are a programming technique that enables this behavior, resulting in smaller, simpler functions. Show Notes / Transcript Complexity in our code makes it harder for us and others to understand what the code is doing. The smallest unit of our code tends to be the function or method. You should be able to look at a given function and quickly determine what it's doing. This tends to be much easier if the function is small, well-named, and focused. One factor that's constantly working against you as you try to keep your functions manageable is conditional complexity (a code smell) - if and switch statements. When not properly managed, these two constructs can quickly cause functions to shift from simple and easily understood to long, obtuse, and scary. One way you can cut down on some of the complexity is through the use of guard clauses. A guard clause is simply a check that immediately exits the function, either with a re