Chapter 1: The changing face of C# development: 1.1.1
Last updated: 6/21/2008
Product class in listing 1.1 leaves a few things to be desired. These are partly stylistic, but still far from arbitrary.
- The class isn't sealed. Unless a class is specifically designed with inheritance in mind, I believe it should be sealed - just like methods should only be made virtual after careful consideration. There's a price to pay when designing a class for inheritance, and most of the time you don't really need it. I wish classes were sealed by default (as methods are) - I often forget to make classes sealed when they really should be.
- The class isn't immutable, but potentially could be. In this case making it immutable would have made it impossible to demonstrate some of the new features of C# 2 and 3 later on in the chapter, but in real life that's not usually a consideration! Immutability is good in a number of ways. Look out for an article about it some time in the future...
- The private fields aren't explicitly private. This is definitely a matter of style, and one where Eric and I disagree. I like using the defaults of C# - wherever I specify an access modifier, it's making it more public. Given that things should usually be as private as you can sensibly make them, it's a sort of reminder to constantly think about members which have been publicised more widely. On the other hand, it's slightly less immediately obvious what the accessibility is.
All of these points are ones to think about for real code, which has different motivations than sample code in books. In this case, I didn't want to add modifiers like private and sealed, in order to keep the code as simple as possible.