Search code examples
javajls

What does it mean for an expression to contain "at most one side effect, as its outermost operation"?


In Java Language Spex 15.7:

Code is usually clearer when each expression contains at most one side effect, as its outermost operation

What does it mean?


Solution

  • It means that each expression should do one task at a time.

    Consider the following two declarations:

    int a = 10;
    int b = 20;
    

    Now the task is to add these two ints and increment b by 1. There are two way to do it.

    int c = a + b++;
    

    and

    int c = a + b;
    b++;
    

    JLS prefers and recommends the latter one.