Using prefix incremented loops in C#

Back when I started programming in college, a friend encouraged me to use the prefix incrementation operator ++i instead of the postfix i++, citing that there was a slight chance of better performance with no real chance of a downside. I realize this is true in C++, and it’s become a general habit that I continue to do.

I’m led to believe that it makes little to no difference when used in a loop in C#, regardless of data type. Apparently the ++ operator can’t be overridden. Nevertheless, I like the appearance more, and don’t see a direct downside to it.

It did astonish a coworker just a moment ago though, he made the (fairly logical) assumption that my loop would terminate early as a result. He’s a self-taught programmer, and apparently never came across the C++ convention. That made me question whether or not the equivalent behavior of pre- and post-fix increment and decrement operators in loops is well known enough.

Is it acceptable for me to continue using ++i in looping constructs because of style preference, even though it has no real performance benefit? Or is it likely to cause confusion amongst other programmers?


  • Note: This is assuming the ++i convention is used consistently throughout all code.

6

I think you should stick with the conventions that your co-workers are used to. The principle of least surprise. You don’t want to sit and figure out why a simple operation is written in an uncommon manner for no reason, so why should the next guy who is reading your code?

4

Unoptimized, i++ makes a copy, so j = i++ translates to:

iTemp = i;
i = i + 1;
j = iTemp;

Whereas j = ++i translates to:

i = i + 1;
j = i;

That extra copy into a temp variable is what causes the theoretical performance hit with the post-increment. However, since the new value of i doesn’t depend on j, it can be optimized to:

j = i;
i = i + 1;

Which, barring any pipeline oddities, has exactly the same runtime as the pre-increment version. It turns out there are very few real world circumstances where a similar compiler optimization is not possible, so there is practically zero reason to choose pre-increment for optimization reasons. Standalone increments, such as in a for loop, are even easier to optimize down to the same instruction.

In other words, follow the coding convention unless it’s syntactically significant. But it doesn’t hurt to educate your colleagues either if the only reason they don’t use it is based on an erroneous understanding of how for loops work.

1

Good practice is to use the coding convention that your team members/colleges are agreed on. It does not really matter how you decided to do that as far as it works and inconstantly implemented. Keeping code as simple and as clean (KISS principle) should be the main goal.

It is far more important to have consistent style of coding in big projects to ease the maintenance at later stages.

Here you are a MS guidelines link – C# Coding Conventions (C# Programming Guide)

++i and i++ are different operations.

Check this Link out it gives a better example than what I had. but I was on the Right Track.

int i = 1;

Console.WriteLine(++i); // <-- will output 2



i = 1; // <-- reset i

Console.WriteLine(i++); // <-- will output 1

3

I would say that depends upon exactly what you mean by “style preference”. If you mean that you want to use “++i” everywhere including loops like for(i=0; i<x; ++i), then that’s fine. If on the other hand you want to specifically use it in loops while elsewhere using i++ or i+=1 (or viceversa) then that’s a terrible idea, you should be consistent through-out the program.

As long as you are consistent and aren’t using it in a comparision expression or multiple times in a method call, this should not be a suprise to anyone. In particular the for loop is

  1. Assignment
  2. Comparison
  3. Body, if, true, and Finally
  4. Increment

As long as you aren’t doing your increment in either 1, 2 or 3, there should be no surpise.

On the other hand, if you sometime use post increment and sometimes pre increment, or do it in a non standard manner f(i++, ++i);, then you’re going to give someone pause, and you’re likely to make a mistake yourself or lead someone else into making a mistake.

Note that if you do choose ++i, I would recommend that you Never do this if(++i<x)

It’s true that i++ and ++i are different operations. They have the same side effect (modifying the stored value of i), but they yield different values, and as Karl Bielefeldt’s answer points out an unoptimized implementation of i++ may have to store the previous value of i in a temporary.

But if you’re using them in a context where the result is not used, there should be no difference at all.

For example, in a C-style for loop, the third expression in the loop header is evaluated only for its side effects; any value it yields is quietly discarded. These two loops:

for (i = 0; i < COUNT; i ++) {
    // ...
}

for (i = 0; i < COUNT; ++ i) {
    // ...
}

have identical semantics, and if your compiler doesn’t generate identical, or at least equivalent, code for both you should probably find a better compiler.

So in this context, the choice between i ++ and ++ i should be purely one of style. If you have a coding standard, follow it. If you don’t, just be consistent.

If you’re using either i ++ or ++ i in a context where the result is used, then performance isn’t going to be a factor; use whichever one gives you the answer you need. Or consider whether your code might be cleaner if you rearrange it so you’re not using the result of i ++ or ++ i.

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