Small Project Organization
Validation Result
A result struct returns both a success flag and a reason code.
Validation Result
validation_result.cpp
#include <iostream>
struct Validation {
bool ok;
int code;
};
Validation validateAge(int age) {
if (age < 0) {
return {false, 1};
}
if (age < 18) {
return {false, 2};
}
return {true, 0};
}
int main() {
int age = ;
Validation result = validateAge(age);
std::cout << "age=" << age << std::endl;
std::cout << "ok=" << result.ok << std::endl;
std::cout << "code=" << result.code << std::endl;
return 0;
}
#include <iostream>
struct Validation {
bool ok;
int code;
};
Validation validateAge(int age) {
if (age < 0) {
return {false, 1};
}
if (age < 18) {
return {false, 2};
}
return {true, 0};
}
int main() {
int age = ;
Validation result = validateAge(age);
std::cout << "age=" << age << std::endl;
std::cout << "ok=" << result.ok << std::endl;
std::cout << "code=" << result.code << std::endl;
return 0;
}
#include <iostream>
struct Validation {
bool ok;
int code;
};
Validation validateAge(int age) {
if (age < 0) {
return {false, 1};
}
if (age < 18) {
return {false, 2};
}
return {true, 0};
}
int main() {
int age = ;
Validation result = validateAge(age);
std::cout << "age=" << age << std::endl;
std::cout << "ok=" << result.ok << std::endl;
std::cout << "code=" << result.code << std::endl;
return 0;
}
result
Return a small result object when the caller needs more than true or false.
reason
Reason codes let the caller distinguish invalid input from input that is merely incomplete.