2 Replies - 3110 Views - Last Post: 11 January 2013 - 03:11 PM Rate Topic: -----

#1 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,767
  • Joined: 12-June 08

[link]8 Most common mistakes C# developers make

Posted 11 January 2013 - 12:35 PM

http://blog.goyello....evelopers-make/

A great blog post on somethings I know I forget to do and have seen others miss.

Quote

While working with (young) C# programmers I’ve noticed that some mistakes are being repeated by almost every one of them. These are mostly the mistakes, which once you point them, are quite easy to remember. However, if a developer is not aware of them, they can cause many problems with the efficiency and quality of the developed software.

1. String concatenation instead of StringBuilder
2. LINQ – ‘Where’ with ‘First’ instead of FirstOrDefault
3. Casting by means of ‘(T)’ instead of ‘as (T)’ when possibly not castable
4. Not using mapping for rewriting properties
5. Incorrect exceptions re-throwing
6. Not using ‘using’ for objects disposal
7. Using ‘foreach’ instead of ‘for’ for anything else than collections
8. Retrieving or saving data to DB in more than 1 call


Is This A Good Question/Topic? 2
  • +

Replies To: [link]8 Most common mistakes C# developers make

#2 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: [link]8 Most common mistakes C# developers make

Posted 11 January 2013 - 02:32 PM

1. I'm pretty sure that most people using StringBuilder are using it wrong (micro-optimizing it).

What most people should know is that literal constant concatenation is performed at compile time (compiler optimization).

I've seen huge 'string building' code that performs countless calls of Append() to concatenate literal constants.

6. If this list is ordered, I believe this should be the #1.

7. I disagree, this is micro-optimization IMO.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2148
  • View blog
  • Posts: 3,297
  • Joined: 21-June 11

Re: [link]8 Most common mistakes C# developers make

Posted 11 January 2013 - 03:11 PM

I disagree with 2 - at least as a general rule to be followed blindly.

If the Enumerable can be empty, you should first think about what should happen if that is the case and then implement that logic appropriately. That might involve calling FirstOrDefault, but it certainly doesn't have to. Just using FirstOrDefault without thinking about whether that's the right thing in that situation seems ill-addvised.

If you think the result of Where will be non-empty, but it turns out to be empty, then just using FirstOrDefault instead of First will at best delay and obscure the error a bit (i.e. replace the exception thrown by First with a NullReferenceException thrown when you try to use the object returned be FirstOrDefault) and at worst hide the error completely (e.g. you use FirstOrDefault on an empty Enumerable of ints and you treat the 0 returned by it as if it was part of the Enumerable, never noticing that the Enumerable was empty).

If you think the Enumerable can't be empty, but it is, that means there's an error in your code and/or your assumptions. If such an error exists, you want to be made aware of it as soon as possible. You should not follow a "best practice" that would hide such an error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1