Maintenance wise, is `else while` without intervening braces considered safe?

Is else while without intervening braces considered “safe” maintenance wise?

Writing if-else code without braces like below…

if (blah)
    foo();
else
    bar();

…carries a risk because the lack of braces make it very easy to change the meaning of the code inadvertently.

However, is below also risky?

if (blah)
{
    ...
}
else while (!bloop())
{
    bar();
}

Or is else while without intervening braces considered “safe”?

11

That reminds me this code:

if ( ... ) try {
..
} catch (Exception e) {
..
} else {
...
}

Every time you are combining two types of blocks, forgetting braces and not increasing indentation, you are creating code very difficult to understand and maintain.

3

Perhaps it is because I learnt my trade (way back when) using the Jackson Entity Structure Diagram method, but I subscribe to the view that the only correct unbraced term after an if or an else is a subsequent if (ie allowing an else if ladder)

Anything else (no pun intended) leaves potential for misunderstanding and/or maintenance issues. That is the central aspect of the OPs idea being “unsafe”.

I’d also be very cagey about including the while() on the same line as the else – whether braced or not. It doesn’t read right to me… the lack of any extra indentation masks that it is the else clause. And lack of clarity makes for misunderstandings (see above).

So in the example I would strongly advise/recommend (and insist, in my team) on:

if ( blah )
{
    ...
}
else
{
    while ( !bloop() )
    {
        bar();
    }
}

Of course, I would also expect to see suitable comments too.

— Edit —

Recently Apple suffered from an SSL vulnerability, caused by a poor maintenance fix – that added a second line to an un-braced single line. So let’s put to bed the idea that unbraced single lines are OK?

5

I have always been taught to keep everything in braces, indented and commented. I believe it’s much easier to read and spot errors. Personally i feel modularity is key so i would always write the code like this :

    if(blah)
    {
     ....
    }
    else
    {
       while(!bloop()
       {
        bar;
       }
    }

I would extract method and make it

if ( blah )
{
    ...
}
else
{
   newMethod();
}

See “extract method” approach explained at Refactoring Catalog site:

You have a code fragment that can be grouped together.

Turn the fragment into a method whose name explains the purpose of the method.

void printOwing() {
    printBanner();

    //print details
    System.out.println ("name:    " + _name);
    System.out.println ("amount    " + getOutstanding());
}

                                                                                                        

void printOwing() {
    printBanner();
    printDetails(getOutstanding());
}

void printDetails (double outstanding) {
    System.out.println ("name:    " + _name);
    System.out.println ("amount    " + outstanding);
}

2

I would consider it as a bad habit of coding. Sometimes it works perfectly fine and you will have no problem in compiling and running the code. At other times it may cause serious bugs and you will end up spending hours in fixing that bug.

It is always recommended to modularize your code. If you have to put a while loop in else part put it in a block so that others, when working on your code can easily understand the logic.

if ( blah )
{
    ...
}
else
{
    while ( !bloop() )
    {
        bar();
    }
}

It’s a good practice to put code in blocks. It makes it simpler and easier to understand and debug as well.

It might be fine if it stays simple like that although personally I don’t like it and my preference is to use braces even for the simplest if/else code blocks. It’s just tidier for me.

However, where this gets messy is when you have nested if/else loops some with braces some without. A While loop in the middle of all that spaghetti! I’ve worked with code that bad and it’s a nightmare to debug and to understand. This follows on from the first point it’s fine when it stays simple and you’re happy with it, but then other programmers come along and add things to it, probably an if else within the while loop. If it’s written clean in the first place then there’s less chance of this happening.

The point is to make stuff clear so that other programmers can see at a glance what’s right and wrong. Write code so that the pattern looks right and doesn’t jar.

The flip side of this is that maybe I could see some people claiming that in certain instances this reads well. If I’ve got the resource I need do this processing else while I’m waiting for something do this. Even then to me there’s still no difference in nesting the while loop inside the else block.

Well, despite everyone argues about using braces and seem to love them, I actually prefer the opposite. I only use braces when I have to because I find it more readable and concise without.

if (...)
   foo();
else while(...)
   bar();

…I actually find this “else while(...)” remarkably readable! It even reads like plain English! But I guess people will all find it strange because it’s unusual to say the least.

In the end, we all tend to make it idiot-proof with braces though …because, well, you know.

5

What is so wrong with braces that so many people is trying to avoid writing them ?

What problem does exactly else while solve ?

Braces are cheap and good, and they make code intention evident and plain as opposed to clever and witty.

Quoting Unix Philosophy:

Rule of Clarity: Clarity is better than cleverness.

Because maintenance is so important and so expensive, write programs
as if the most important communication they do is not to the computer
that executes them but to the human beings who will read and maintain
the source code in the future (including yourself).

I always put braces, just because you might add another line when in hurry, indent it, forget the braces and scratch your head what’s happening. I keep the opening bracket on the same line, but it doesn’t matter. I both cases it’s better to have them.

if(blah) {
    foo();
}
else {
    bar();
}

2

There’s nothing wrong with

if (blah)
    foo();
else
    bar();

just like there’s nothing wrong with

if (blah) foo(); else bar();

in the right circumstances (your circumstances may vary, but that particular style is best used when you have a lot of repetitive code – then the view of each line is more important than the stylistic indentation)

But if you choose one way, keep to it – consistency is king. So your second example is very bad, as the if expression had brackets for its statement, the while statement should be inside the brackets for the else clause, not outside it.


besides, I have seen this code before:

if (x) foo()
{
  bar();
}

and it was written by the coding standard nazi himself (who insisted on brackets for everything).

Modern IDEs can easily be configured to reformat (including removing or adding unnecessary braces) and/or reindent the code on saving a file. Thus your example would automatically look like for instance

if (blah) {
  blub();
} else
  while (!bloop()) {
    bar();
  }

The indentation always makes the nesting easily visible, even without unneccesary braces. So, if you manage to enforce such IDE settings, I see no risk.

Personally, I find code that omits the braces and linebreaks much more readable since it avoids clutter:

if (blah) blub();
else while (!bloop()) bar();

But of course, many developers are very happy to argue about such things forever and a day.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật