Thursday, May 19, 2005

Explicit Code

Introduction

Usually, you will write explicit code in order to avoid ambiguous code or simply because there is no implicit alternative.

Concept: Explicit code is source code that must be expressed completely.

Program

Consider the program:
class Compute {
    public static void main(String[] args) {
        System.out.println(3 + 2 * 2);
    }
}

Output

7

Discussion

Imagine that the programmer thinks that it is unclear which operation, + or *, is performed first.

class Compute {
    public static void main(String[] args) {
        System.out.println((3 + 2) * 2);
    }
}

Parenthesis has been explicitly added around 3 + 2 in order to avoid any ambiguity.

No comments: