Use Guard Condition

Properties

Property Value
First seen in jSparrow version 2.7.0
Minimum Java version 1.1
Remediation cost 5 min
Links S3776

Description

Replaces, when possible, the last if-statement of a method body with a guard-if and unwraps its body.

Benefits

Reduces the cognitive complexity of a method.

Requirement & Tags

Requirements

None

Tags

Java 1.1, Coding Conventions

Code Changes

Void method ending with an if statement

Pre

	public void voidMethod() {
		doSomething("what ever");
		if(numericCondition() > 0) {
			doSomething("Should create guard condition with less");
			doSomething("Should transform");
		}
	}

Post

	public void voidMethod() {
		doSomething("what ever");
		if (numericCondition() <= 0) {
			return;
		}
		doSomething("Should create guard condition with less");
		doSomething("Should transform");
	}

Method with return type

Pre

	public int methodWithReturnType() {
		doSomething("what ever");
		if(condition()) {
			doSomething("Should be moved out of the if");
			doSomething("should transform");
			return 1;
		}
		return 0;
	}

Post

	public int methodWithReturnType() {
		doSomething("what ever");
		if (!condition()) {
			return 0;
		}
		doSomething("Should be moved out of the if");
		doSomething("should transform");
		return 1;
	}

Limitations

Multiple if-then-else branches

Pre

	public void multipleElseBranches() {
		if(numericCondition() > 0) {
			doSomething("Should create guard condition with less");
			doSomething("Should not transform");
		} else if(numericCondition() == 0) {
			doSomething("on else-if branch");
		} else {
			doSomething("Else branch");
		}
	}

Post

	public void multipleElseBranches() {
		if(numericCondition() > 0) {
			doSomething("Should create guard condition with less");
			doSomething("Should not transform");
		} else if(numericCondition() == 0) {
			doSomething("on else-if branch");
		} else {
			doSomething("Else branch");
		}
	}