Coding Style (Loop processing)

graviti

Senior Member
Joined
May 8, 2006
Messages
665
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:

graviti

Senior Member
Joined
May 8, 2006
Messages
665
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.
 

graviti

Senior Member
Joined
May 8, 2006
Messages
665
Option 2 does use up a few extra clock cycles though, but nothing super serious.
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
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:
 

Nerfherder

Honorary Master
Joined
Apr 21, 2008
Messages
29,703
You need to compact your code as much as possible when writing big progams.

Using option 1 helps a lot.

Searching 10 000 lines = suck
 

graviti

Senior Member
Joined
May 8, 2006
Messages
665
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.
 

Nerfherder

Honorary Master
Joined
Apr 21, 2008
Messages
29,703
Okay then would you agree that this is still better than option 1:
Code:
string str = "";
for (int i = 0; i < Data.Length; i++) { str += (char)Data[i] ; }

Not sure, I don't do C#

But I would prefer to do it the origonal way.
 

grayston

Expert Member
Joined
Jul 24, 2007
Messages
3,733
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.
 

CorneN

Expert Member
Joined
Sep 20, 2008
Messages
1,406
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.
 

davemc

Executive Member
Joined
Apr 8, 2009
Messages
6,518
Option 3:
Code:
string str = [B]string.Empty[/B];
for (int i = 0; i < Data.Length; i++)[B]{[/B]
    str += (char)Data[i];
}
 

hyperian

Expert Member
Joined
Apr 17, 2008
Messages
1,958
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.
 

hyperian

Expert Member
Joined
Apr 17, 2008
Messages
1,958
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.
 

Necuno

Court Jester
Joined
Sep 27, 2005
Messages
58,567
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.
 

Gnome

Executive Member
Joined
Sep 19, 2005
Messages
7,208
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.
 

Gnome

Executive Member
Joined
Sep 19, 2005
Messages
7,208
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.
 

risingtide

Senior Member
Joined
Mar 19, 2007
Messages
693
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.
 

davemc

Executive Member
Joined
Apr 8, 2009
Messages
6,518
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