Every day talented people strive toward making software faster, more stable, more secure. And at the same time, even more talented people are looking for ways to break it :-)
Hardware manufactures come up with new components that open up possibilities that were unimaginable just a several years ago. And at the same time these advances make certain ‘industry standard’ patterns and practices dangerous and obsolete.
Keeping up with technology is very important for any kind of engineer, but for The Software Engineer it is an absolute necessity.
Every one of us have a favorite way of doing things and but time to time we have to re-evaluate our coding habits.
In this (hopefully) weekly blog I’m going to illustrate patterns, practices, code snippets that I find interesting, dangerous, cool, exciting ,etc.
Just to illustrate my point, I’m going to give you a simple example. I’m pretty sure that every one of you already knows about this coding pattern and never ever write code like that. You don’t, don’t you? ;-)
So, 11 years ago, in .NET 1.1, this snippet was perfectly fine:
Every one of us have a favorite way of doing things and but time to time we have to re-evaluate our coding habits.
In this (hopefully) weekly blog I’m going to illustrate patterns, practices, code snippets that I find interesting, dangerous, cool, exciting ,etc.
Just to illustrate my point, I’m going to give you a simple example. I’m pretty sure that every one of you already knows about this coding pattern and never ever write code like that. You don’t, don’t you? ;-)
So, 11 years ago, in .NET 1.1, this snippet was perfectly fine:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fine with .NET 1.x | |
IDictionary hash = new Hashtable(); | |
hash["0"] = "a"; | |
hash["1"] = "b"; | |
hash["2"] = "c"; | |
// some code here | |
// some code here | |
// some code here | |
var result = hash["z"]; | |
if (result != null) | |
{ | |
// do something | |
} | |
// continue code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IDictionary hash = new Dictionary<string, string>(); | |
hash["0"] = "a"; | |
hash["1"] = "b"; | |
hash["2"] = "c"; | |
// some code here | |
// some code here | |
// some code here | |
var result = hash["z"]; | |
if (result != null) | |
{ | |
// do something | |
} | |
// continue code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// let's use var !!! | |
var hash = new Dictionary<string, string>(); | |
hash["0"] = "a"; | |
hash["1"] = "b"; | |
hash["2"] = "c"; | |
// some code here | |
// some code here | |
// some code here | |
var result = hash["z"]; // KeyNotFoundException here | |
if (result != null) | |
{ | |
// do something | |
} | |
// continue code |
But let's say that you are not aware of this change, and you keep writing code that relies on dictionary to return null, when key is not found. Yes, of cause if you have a solid set of unit tests to cover every line of your code and every possible combination, then you’ll see this problem right away. But we all know that unfortunately it is not a reality and code like that can sneak into production.
So the new, modern code should probably look like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dict = new Dictionary<string, string>() | |
{ | |
{"0", "a"}, | |
{"1", "b"}, | |
{"2", "c"}, | |
}; // inline initializers are SO COOL !!!! | |
string value; | |
if (dict.TryGetValue("z", out value)) | |
{ | |
// do something | |
} | |
// continue code |