Skip to content

Latest commit

 

History

History
45 lines (33 loc) · 1.07 KB

300_limit_scope.md

File metadata and controls

45 lines (33 loc) · 1.07 KB

Limit Variables to the Smallest Possible Scope

Summary

Variables should be declared as late as possible so that they have the narrowest possible scope.

Details

Bad

public void foo(String value) {
    String calculatedValue;
    if (someCondition()) {
        calculatedValue = calculateStr(value);
        doSomethingWithValue(calculatedValue);
    }
}

Better

public void foo(String value) {
    if (someCondition()) {
        String calculatedValue = calculateStr(value);
        doSomethingWithValue(calculatedValue);
    }
}

Better still

public void foo(String value) {
    if (someCondition()) {
        doSomethingWithValue(calculateStr(value));
    }
}

Sometimes, assigning to well-named, temporary variables will result in more readable code than calling a method inline because it helps the reader follow the data and logical flow.

As a rule of thumb, if you are unable to come up with a name for a variable that does little more than mirror a method from which its value was retrieved, then the variable should be eliminated.