TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
TIOBE - Java Coding Standard
Search

Rule:  UnusedCode5Checked automatically with code checker

Synopsis:Avoid unused assignments
Language:Java
Severity Level:5
Category:UnusedCode


Description:
Avoid assignments to variables that are never used before the variable is overwritten, or goes out of scope. Unused assignments are those for which the variable is never read after the assignment, or the assigned value is always overwritten by other assignments before the next read of the variable.
class A {
    // this field initializer is redundant,
    // it is always overwritten in the constructor 
    int f = 1;  

    A(int f) {  
        this.f = f; 
    }
}

class B {
    int method(int i, int j) {
        // this initializer is redundant,
        // it is overwritten in all branches of the `if`
        int k = 0;

        // Both the assignments to k are unused, because k is
        // not read after the if/else
        // This may hide a bug: the programmer probably wanted to return k
        if (i < j)
            k = i;
        else
            k = j;

        return j;
    }
}

class C {
    int method() {
        int i = 0;

        checkSomething(++i); // variable i isn't use anymore after this
    }
}