Coding Style (Loop processing)

Copy and paste is evil.

Option 2 will never work.


Code:
[B][U]Option 2[/U][/B]
string str = "";
for (int i = 0; i < Data.Length; i++)
{
    str += (char)Data[i++]) ;
}

Should read

Code:
[B][U]Option 2[/U][/B]
string str = "";
for (int i = 0; i < Data.Length; i++)
{
    str += (char)Data[B][COLOR="Red"][i][/COLOR][/B] ;
}

Bold red bits are changed areas
 
Last edited:
Still got the round bracket after the data which shouldn't be there.

Prefer option 2 for readability. Option 1 is neat, but put that into 1000 lines of code, and ask someone to troubleshoot your code and they probably going to want to shoot you.
 
Still got the round bracket after the data which shouldn't be there.

Prefer option 2 for readability. Option 1 is neat, but put that into 1000 lines of code, and ask someone to troubleshoot your code and they probably going to want to shoot you.


Only if you don't comment your code :rolleyes:
 
Commenting is a BIG plus, obviously.

10 000 lines of code in a single file just doesn't make sense. Break it up into smaller bits. Easier to read, easier to track. Part of the point of object-orientation, in my opinion. I do and don't agree in compacting your code. Sometimes compacting it, like in the above example is fine, but I've seen programmers shove huge amounts of code into arg3 of the for loop before, in such a way that it takes up 3 lines anyway. Then you have to try and figure out what they trying to do. As a STYLE, I prefer 2.
 
Any comments on this coding style?

Code:
[u][b]Option 1[/b][/u]
string str = "";
for (int i = 0; i < Data.Length; str += (char)Data[i++]) ;

As apposed to this:
Code:
[u][b]Option 2[/b][/u]
string str = "";
for (int i = 0; i < Data.Length; i++)
{
    str += (char)Data[i] ;
}

My vote is for option 2, option 1 is just nasty.

Does C# not have a string copy function? Because that's all that's happening here.

I don't like either option because it's not (immediately) obvious what is going on, other than a loop is being run.
 
I've never done development for living, so this might be stupid question. Doesnt your project manager / leader / corrdinator etc decide on a style beforehand? If read through code I like seeing the style being applied right through, nevermind how small a particular statement might be. So option 2 for me then.

If you have a project that might potentially end with a 10,000 lines, you could use 1BTS instead of K&R, which will save you alot of space. However, I do agree with Graviti, when you hit 10,000 lines of code, its possible you have a God object on your hands.
 
I've never done development for living, so this might be stupid question. Doesnt your project manager / leader / corrdinator etc decide on a style beforehand? If read through code I like seeing the style being applied right through, nevermind how small a particular statement might be. So option 2 for me then.

Coding style is usually predetermined by the company. They will have a set of developer guidelines/documents that you need to familiarize yourself with and abide by.
 
I'd say option 2 - Code needs to be maintainable, so if half the devs that need to work with the code in the future do not understand what is happening in the loop, then the performance gains will be offset by the extra time they take to understand the code.
 
Any comments on this coding style?

Code:
[u][b]Option 1[/b][/u]
string str = "";
for (int i = 0; i < Data.Length; str += (char)Data[i++]) ;

As apposed to this:
Code:
[u][b]Option 2[/b][/u]
string str = "";
for (int i = 0; i < Data.Length; i++)
{
    str += (char)Data[i] ;
}

My vote is for option 2, option 1 is just nasty.

option 2 with StringBuilder.
 
Actually I think the best method (in this example) is to use StringBuilder.

Yep, much faster, we actually did a test of C (printf), C++ (cout) and Java (string concatenation) and Java (Stringbuilder)

C > C++ > Java (String Built) > Java (String concatenation)

String concatenation was incredibly slow compared to the other 3.
 
I'm willing to bet that has something to do with the fact that strings are immutable in Java (and c#);

Yeah, each time a new string is allocated on the heap, and the old string is marked for GC, it's such a process for just one statement.
 
Prefer option 2 for readability. Option 1 is neat, but put that into 1000 lines of code, and ask someone to troubleshoot your code and they probably going to want to shoot you.

I always write code so that when I need to come back to it in the future I won't have to shoot myself.
 
I apologise for the confusion. Note the position of the { in Option 3.

Code:
string str = string.Empty;
for (int i = 0; i < Data.Length; i++){
    str += (char)Data[i];
}
Bracketing like that is a compromise that helps to reduce the lines of extra code generated by {'s.

A consultant who worked for Anderson Consulting once bragged about a C program he wrote that was 15000 lines of code. I asked him how many of those lines are { and } and he just went red.
 
Top
Sign up to the MyBroadband newsletter
X