[JAVA] Easy to read control flow

Easy to read control flow

I read the Readable code Let's write a way to make the control flow easier to read

List of arguments of conditional expression

if(length >= 10)
if(10 <= length)

Both have the same meaning, but the first one is easier to read

Values that change to the left side, values that do not change to the right side

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.

Ternary operator

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);

Make the nest shallow

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();

Return early to remove nesting

//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

Easy to read control flow
How to read PyPI
How to read JSON
Easy to use SQLite3
Easy to make with syntax
Tips for coding short and easy to read in Python
Easy way to rename files
Easy to use E-Cell 4 Intermediate
Easy to use E-Cell 4 Beginner's edition
Easy copy to clipboard on Linux
Easy way to customize Python import
How to read e-Stat subregion data
Django1.11.1 Image uploader Easy to stumble
How to read the SNLI dataset
Easy to install pyspark with conda
Smooth flow by optimizing signal control
Easy to use E-Cell 4 Advanced Edition
Easy to use Jupyter notebook (Python3.5)
Easy to see difference of json
Easy to draw graphs with matplotlib
Easy Python to learn while writing
Article to read after Locust introductory article
[Python Tutorial] An Easy Introduction to Python