Amusing Bad Programming Tip

Say you have a class with a property that’s a string. Using object initializers you can do this little nugget:

Product p = new Product { ProductName = Console.ReadLine() };

It will work as you’d expect (prompt you for input, then assign it to ProductName). However, that’s pretty easy to miss when you’re quickly scanning code. I’m generally in favor of fewer lines of code, but in this case it would be good design to just break this into two lines.

Tags:

Copyright © 2017 - Scott Williams - Powered by Octopress