I read the Readable code Let's write a way to make the control flow easier to read
if(length >= 10)
if(10 <= length)
Both have the same meaning, but the first one is easier to read
In the previous example, the constant 10 is moved to the right side. Bringing a variable called length to the left side makes it easier to read.
Even in Japanese "If I'm over 20" "If 20 is under my age" When thinking about, the former is easier to understand.
My age changes, but 20 is a constant. By placing the values that change to the left side and the values that do not change to the right side in the same order as natural language. It turns out that it can be easily understood.
The ternary operator is very convenient. You can shorten the facilitating if-else statement by using the following
if(hour >= 12) {
time_str += "pm";
} else {
time_str += "am";
}
//Use ternary operator
time_str += (hour >= 12) ? "pm" : "am";
But if you use it blindly, it may be difficult to understand.
return exponent >= 0 ? mantissa * (1<<exponent) : mantissa/(1<< -exponent);
Reply when the user's result is successful and permission is obtained. Returns an empty error if the user's result is successful and no permission is given. If the user's result is unsuccessful, an error is returned. Processing.
It's getting a little complicated.
if(user_result == SUCCESS) {
if(permission_result != SUCCESS) {
reply.WriteErrors("error reading permissions");
reply.Done();
return;
}
reply.WriteErrors("");
} else {
reply.WriteErrors(user_result);
}
reply.Done();
//User first_result !=It returns the result of SUCCESS.
if (user_result != SUCCESS) {
reply.WriteErrors(user_result);
reply.Done();
return;
}
//The rest is user_result ==Only for SUCCESS
if(permission_result != SUCCESS) {
reply.WriteErrors(permission_result);
reply.Done();
return;
}
reply.WriteErrors("");
reply.Done();
This made the nest shallower. The control flow could be simplified by returning early with return.
Recommended Posts