Sunday, November 26, 2006

For Itch

Before we start coding, we got an email about best practices for writing code in .NET. There was a line in the email about using ‘For’ instead of using ‘For Each’ loop (in VB .net). People have proven that ‘For’ loop is faster than (or same at max) ‘For Each’ by going unto Intermediate Language.

I don’t want to go in discussion about which one is better. Just see this and tell me which one is better to use. Isn’t it better to write simple and maintainable code than 'possibly' faster code?

'For Each
For Each str As String In BaseString.Split(" "c)
Result += SomeFunction(str)

'For only
For i As Integer = 0 To BaseString.Split(" "c).Length - 1
Result += SomeFunction(BaseString.Split(" "c)(i))

No comments: