How to write an if statement to improve readability-java

Training memo. (For myself) ** * I have received a total tsukkomi, so I will correct it. Thank you for pointing out. 2019/11/14 **

When developing as a team, the readability of the program, that is, the readability, is important. ・ Actively comment. ・ It is better to make a habit of putting comments in the condition of the if statement.

For example, in the following conditions

sample


//a is equal to b or c is equal to d and e is not equal to f and g is not equal to h
if (a == b || (c == d && e != f) && g != h) {
 //Processing content
}

Because there are multiple conditions and it is complicated Organize the conditions.

sample



	        int a = 0;
	        int b = 0;
	        int c = 0;
	        int d = 0;
	        int e = 0;
	        int f = 0;
	        int g = 0;
	        int h = 0;


	        if (a == b || (c == d && e != f) && g != h) {
	            System.out.println("true1");
	        }


	        if (a == b) {
	        	System.out.println("true2");
	        } else if (c == d & e != f) {
	        	if (g != h) {
	        		System.out.println("true2");
	        	}
	        }

result: true1 true2

sample



//Wrong processing
	        if (g != h) {
	            if (a == b) {
	                System.out.println("true2");
	            } else if (c == d && e != f) {
	                System.out.println("true2");
	            }
	        }
	    }

It depends on the processing content, but even if the number of lines increases a little It is said that there are many sites that emphasize simplicity and simplicity at a glance.

Recommended Posts

How to write an if statement to improve readability-java
How to write an RSpec controller test
How to write Rails
How to write dockerfile
How to write docker-compose
10 Corresponds to if statement
How to write Mockito
How to write migrationfile
How to write to apply gem Pagy (pagination) to an array
How to write an external reference key in FactoryBot
How to create an application
How to write good code
Bit Tetris (how to write)
How to write java comments
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
[Ruby] How to write blocks
How to handle an instance
How to write Rails routing
Studying Java # 6 (How to write blocks)
[Rails] How to write in Japanese
Baseball ball count (how to write)
How to insert an external library
How to write a ternary operator
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write Java variable declaration
Y-shaped road tour (how to write)
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
How to write ruby if in one line Summary by beginner
How to crop an image with libGDX
Write a null case using the Optional type without using an if statement
How to blur an image (super easy)
[Basic] How to write a Dockerfile Self-learning ②
I'm still exhausted with redundant if statements! ?? Write an if statement like this
[Java] How to turn a two-dimensional array with an extended for statement
Summary of how to write annotation arguments
How to publish an application on Heroku
[Introduction to Java] How to write a Java program
[Java] How to output and write files!
Write a third If statement that is neither an if statement nor a ternary operator
How to define an inner class bean
How to write Spring AOP pointcut specifier
if statement
[SpringBoot] How to write a controller test
How to write and explain Dockerfile, docker-compose
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
If the parameter is an array, how to include it in Stopara's params.permit
How to check if an instance variable is defined in a Ruby class
JDBC promises and examples of how to write
[JavaFX] How to write Eclipse permissions in build.gradle
How to write offline 15th reference question answer
Java Development Basics ~ How to Write Programs * Exercise 1 ~
How to use an array for HashMap keys
JUnit 5: How to write test cases in enum
Offline real-time how to write F06 implementation example