Coding Style (Loop processing)

CorneN

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

It makes for easier reading as well. With K&R I find myself scrolloing alot, where the code above a bit more compacted.

Wiki on different Indent Styles: http://en.wikipedia.org/wiki/Indent_style
 

Necuno

Court Jester
Joined
Sep 27, 2005
Messages
58,567
i suggest a 5th option

Code:
{
;[ı]ɐʇɐp(ɹɐɥɔ) =+ ɹʇs    
}(++ı ;ɥʇbuǝ1.ɐʇɐp < ı ;0 = ı ʇuı) ɹoɟ
;ʎʇdɯǝ.buıɹʇs = ɹʇs buıɹʇs
 

greggpb

Expert Member
Joined
Apr 22, 2005
Messages
1,818
Option 2 for readability but is this instance the { is not necessary

Code:
string str = string.Empty;
for (int i = 0; i < Data.Length; ++i)
    str += (char)Data[i];

I normally use foreach over a loop..
 

greggpb

Expert Member
Joined
Apr 22, 2005
Messages
1,818
IEnumerator should be the fastest and best but that all depends on the implementation of the IEnumerator interface, for custom classes you see the wierdest stuff.

It internally should declare a pointer to the starting memory address and the ++ should increment the pointer by the correct byte offset.
 
Last edited:

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
Option 4 has large amounts of fail attached to it :p, because of Data was already a char[] you could just say:
Code:
string str = new string(Data);
*grumbles* But the topic of the thread was for LOOPS, not string construction. :p

foreach (byte b in Data)
{
sb.Append((char) b);
}
string str = sb.ToString();
[/code]

Points reassigned:
FarligOpptreden = 0
Rest Of Thread = 1

There's some flawed logic there. The thread dealt with "Coding Style (Loop processing)" and you affirmed the best looping method was a "foreach", which I was the first to recommend.

So, it still stands:
FarligOpptreden 1 - Rest of Thread 0

:D
 

Necuno

Court Jester
Joined
Sep 27, 2005
Messages
58,567
*sigh* I can see where this is going :D At least the MyBB Friday theme is back...

Anyway I actually affirmed that a foreach is the best looping solution when doing a direct mapping to Char and building a string.

If we must assign points:
graviti = ¼ (Source Syntax Nazzi)
Necuno + Gnome = ¼ (StringBuilder)
FarligOpptreden = ¼ (Foreach)
Rest of thread = ¼

hey ****er that is more than just ¼ i also included flip text :mad:
 
Top