Which statement performs best?

I was faced with this question by my team leader.

Statement 1 (written by me):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>lnkbtn1.visible = lnkbtn2.visible = lnkbtn.visible = false;
</code>
<code>lnkbtn1.visible = lnkbtn2.visible = lnkbtn.visible = false; </code>
lnkbtn1.visible = lnkbtn2.visible = lnkbtn.visible = false; 

Statement 2 (written by my team leader):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>lnkbtn1.visible = false;
lnkbtn2.visible = false;
lnkbtn3.visible = false;
</code>
<code>lnkbtn1.visible = false; lnkbtn2.visible = false; lnkbtn3.visible = false; </code>
lnkbtn1.visible = false;
lnkbtn2.visible = false;
lnkbtn3.visible = false;

Which statement is better and why?

4

The performance difference will be so incredibly minor that you should never even think about it.

The only thing that matters here is what one you would rather be using.

The first option might be in one line but oneliners aren’t necessarily better.
It is harder to read and your approach also means that if you want to have false-true-false, you would have to retype everything.

The second option is very clear and each statement is self contained, I would go with this.

1

There’s no difference in performance at all. They will compile to virtually identical IL1.

However, there is a difference in readability and code style. The first option may be more compact, but the second is clear and conforms to most .NET coding guidelines. I personally prefer the second.

1. I ran a simple test in LINQPad. The only difference between the two options was in how the false value was loaded on the stack between calls to set_Visible. The first option produced one ldc.i4.0 and two dup‘s while the second produced three ldc.i4.0‘s.

4

What about a more DRY version, where even the code visible =is not repeated over and over again?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> foreach(var btn in new[]{lnkbtn1, lnkbtn2, lnkbtn3})
btn.visible=false;
</code>
<code> foreach(var btn in new[]{lnkbtn1, lnkbtn2, lnkbtn3}) btn.visible=false; </code>
 foreach(var btn in new[]{lnkbtn1, lnkbtn2, lnkbtn3})
     btn.visible=false;

IMHO that will scale better when the number of buttons increase, or the number of properties of each button which has to be initialized increases.

2

I have to say I prefer the first option. It is crystal clear that all values are being set to false.

With the second option I’m having to read every line and wonder why it isn’t all on one line following DRY principles due the redundant literal.

I’d find it hard to justify the second case on the grounds that the values might change at some point in the future. So much code cruft comes about because of something the programmer thought might happen but never did. This code then has to be tested and maintained. Not to mention being read by potentially dozens of programmers over the course of its life.

6

Just because somebody’s a “team leader” doesn’t mean much.

If .visible is nothing more than a simple boolean variable, the difference in cost is either zero or basically nothing.

However, if .visible is a property, all bets are off.
Assigning to a property is one method call, and retrieving the value is another.
In that case, the separate assignments might be faster, but watch out for another problem.
Properties have some nice properties (!), but I’ve seen them treated as if they were free, performance-wise, when in fact they can be major sink-holes of CPU time if they are implemented recursively.

In terms of style, I take a dim view of giving this more than a nanosecond of thought.
Nearly all serious software has far bigger issues than this.

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