#50 No curly for single line if statements

Future
closed
nobody
None
1
2015-10-22
2002-03-28
No

Allow to omit the curly braces in single line if
statements. For example for parameter checking it is
awkward to enclose everything in curlies:

if( param1 == null ) {
throw new IllegalArgumentException();
}

instead of:

if( param1 == null ) throw new IllegalArgumentException
();

On the other hand stuff like

if( someCondition )
doSomething();

should still be forbidden.

Discussion

  • Oliver Burn

    Oliver Burn - 2002-04-04

    Logged In: YES
    user_id=218824

    I am confused, what is the difference between:

    if( param1 == null ) throw new IllegalArgumentException();

    and:

    if( someCondition )
    doSomething();

    As an aside, I use a utility method so I write:

    Condition.ensureNotNull(param);

    It will throw an exception is param is null.

     
  • Elmar Zander

    Elmar Zander - 2002-04-04

    Logged In: YES
    user_id=211509

    The difference is that the second style often leads to

    if( someCondition )
    doSomething();
    doSomethingMore();

    which is usually not what the programmer intended. While I
    saw this error many times in multiline if statements (and
    far to often, sigh), i never saw something similar in
    single line ifs.

    I have to agree that writing a method to check for valid
    parameters is much more elegant but i don't think that this
    is always applicable. Writing an extra method for a simple
    check is sometimes simply overkill as is bloating your code
    with curly braces which gives you three lines of code
    instead of one.

     
  • Joan Roch

    Joan Roch - 2002-04-27

    Logged In: YES
    user_id=164177

    I do agree that single line 'if' statements are practical
    and should be allowed, as an option of course.

     
  • Ken Arnold

    Ken Arnold - 2002-07-02

    Logged In: YES
    user_id=66520

    You are correct that

    if (foo())
        bar();
    

    can lead to bad things if people are careless. But carelessitude leads to bad things in many places; the question is always one of tradeoff. I personally find

    if (foo()) bar();
    baz();
    

    worse because it looks like baz is attached to the if. But I don't expect to convince you, just that if someone is adding this feature that they should allow my preferred form as well as yours.

     
  • Tim Tyler

    Tim Tyler - 2003-06-15

    Logged In: YES
    user_id=796025

    I've written code to do this.

    It is a modification of the NeedBraces check:

        <module name="NeedBraces">
            <property name="ignoreIfAllOnOneLine"
    

    value="true"/>
    </module>

    FWIW, here's the code I used to see if the children are all on
    the same line.

    /*
    * @return true iff the whole branch is on the same line.

    /
    boolean isAllOnOneLine(DetailAST aAST) {
    int line = aAST.getLineNo();
    ArrayList list = recursivelyGetChildren(aAST);
    for (ListIterator list_iterator = list.listIterator();
    list_iterator.hasNext();) {
    DetailAST ast = (DetailAST) list_iterator.next();
    if (ast.getLineNo() != line) {
    return false;
    }
    }

    return true;
    

    }

    /*
    * @return an ArrayList of all the DetailASTs in the branch

    /
    private ArrayList recursivelyGetChildren(DetailAST root) {
    ArrayList list = new ArrayList();
    getChildren(list, root);

    return list;
    

    }

    private void getChildren(ArrayList list, DetailAST parent) {
    DetailAST child = (DetailAST) parent.getFirstChild();
    while (child != null) {
    list.add(child);
    getChildren(list, child);
    child = (DetailAST) child.getNextSibling();
    }
    }

    Someone asked what the difference between:

    if (someCondition) doSomething();

    and:

    if (someCondition)
    doSomething();

    ...was.

    In summary, permitting former less likely to lead to
    misunderstandings caused by improper indentation;
    it can't /possibly/ lead to (the messy):

    if (someCondition)
    if (someOtherCondition)
    do {
    }

    continue;

    ...and takes up less vertical space - allowing more of the
    program to be visible at once.

    An example where you can argue it is clearer, consider:

    void foo() {
    if (condition1) return;
    if (condition2) return;
    if (condition3) return;

    // get on with the rest of the method...
    }

    vs.

    void foo() {
    if (condition1) {
    return;
    }

    if (condition2) {
    return;
    }

    if (condition3) {
    return;
    }

    // get on with the rest of the method...
    }

    Of course, there are still some problems associated with
    leaving braces off.

     
  • Roman Ivanov

    Roman Ivanov - 2015-10-22
    • status: open --> closed
    • Group: --> Future
     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks