Dave Chestnutt: SparklingCode and CodeGaffes

Writing better code; for fun and profit

  Home  |   Contact  |   Syndication    |   Login
  18 Posts | 1 Stories | 208 Comments | 20 Trackbacks

News

Tag Cloud


Article Categories

Archives

Post Categories

CodeGaffes

Coding Practices - both good and bad. Look to understand the reasons why these are recommended or bad. In a sense, these are like design patterns, only in miniature. By and large, they apply to all three languages, C#, Java, and C++.
A Null Reference Exception bug is reported. You check the code and find the problem happened because a public field wasn't set. You avoid the bug by modifying the code to check for a null. And you make it home in time for dinner. Original Code: myClass.MyField.MyMethod(); // Gets NullReferenceException on MyField “Corrected” Code: // Avoids NPEif ( myClass.MyField != null ) { myClass.MyField.MyMethod();} Seems like a reasonable solution. But guess what–this is nearly always the...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati