Jtest logo




Contents  Previous  Next  Index

OPT.IFAS


Use conditional assignment operator for "if (cond) a = b; else a = c;" statements

Description

This rule flags any "if(cond)-else" statements that could be replaced by "var = (cond ? x : y)".

The conditional operator provides a single expression that yields one of two values based on a boolean expression. The conditional operator results in a more compact expression.

Example

 package OPT;
 public class IFAS {
     void method(boolean isTrue) {
         if (isTrue) {
             _value = 0;
         } else {
             _value = 1;
         }
     }
     private int _value = 0;
 }

Repair

 package OPT;
 public class IFAS {
     void method(boolean isTrue) {
         _value = (isTrue ? 0 : 1); // compact 
 // expression.
     }
     private int _value = 0;
 }

Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft