Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Guard Clause | Advanced Topics
C++ Conditional Statements
course content

Conteúdo do Curso

C++ Conditional Statements

C++ Conditional Statements

1. Introduction to Conditional Statements
2. Conditional Control Flow Practice
3. Advanced Topics

book
Guard Clause

As applications become increasingly complex, the potential for bugs and errors also grows. To combat this, developers have turned to various techniques and practices to ensure code quality. One such technique that has gained prominence in recent years is the Clause Guard Technique.

Consider an application that verifies whether a user meets three conditions: being a student, having a premium account, and being connected. If and only if all these conditions are satisfied, the application will produce the output: Requirements satisfied. Otherwise, log in the console the specific step where the initialization process fails.

You can achieve the desired outcome using nested if statements, but this approach can make the code hard to understand and difficult to modify when you need to add new conditions. Look at the code snippet.

cpp

without_guard_clause

copy
123456789101112131415161718192021
#include <iostream> int main() { bool isStudent = true; // Change it to false and see the output bool isPremium = true; bool isConnected = true; if (isStudent) { if (isPremium) { if (isConnected) { std::cout << "Requirements satisfied"; } else std::cout << "You are not connected\n"; } else std::cout << "You don't have premium\n"; } else std::cout << "You are not a student\n"; }

To apply the guard clause technique effectively, it's important to remember that we can terminate function execution at any moment by using the return keyword. In this approach, we reverse our conditions, meaning that if a user is not a student, we immediately display a message and terminate the main function. This is done to avoid nested if tree and unnecessary execution of code when it serves no purpose.

cpp

with_guard_clause

copy
12345678910111213141516171819202122232425
#include <iostream> int main() { bool isStudent = true; // Change them to false and see the output bool isPremium = true; bool isConnected = true; if (!isStudent) { std::cout << "You are not a student\n"; return 0; } if (!isPremium) { std::cout << "You don't have premium\n"; return 0; } if (!isConnected) { std::cout << "You are not connected\n"; return 0; } std::cout << "Requirements satisfied"; }

The Clause Guard Technique is a powerful tool in the arsenal of software developers striving for code reliability, flexibility and safety. By implementing it developers can reduce the amount of errors, improve code maintainability, and enhance overall software quality.

What is the main goal of the Clause Guard Technique?

What is the main goal of the Clause Guard Technique?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt