It takes me by surprise to realize that peer developers (.net,C#) are writing code but not making use of some of the native/simple features that could help in writing better (re-usable, maintainable, readable, and sometimes performing ) code!!
For instance, when was the last time you have seen (or written) an operator overloading in a custom class? A class written with private access modifier? Use of native attributes like Conditional, Flags ?
It's not necessary that a code written without making use of these features is bad, but then these features exists for some good reasons! I am stating below some of such features that I have been using and "miss" while reviewing others code. I am sure you will find scenarios in your existing code where you could use these features for good reasons.
1. implicit/explicit type casting operator.
I find it really useful for POCO scenario where I don't want the users to write scattered code to create a related type from instance of my class.
2. Conditional attribute.
I find it really useful to write a helper method that only stays in DEBUG code base (and hence is not deployed to production). It gives me assurance that the code would still be good without my method and the extra functionality would do the right thing while working in a DEBUG mode.
3. Private class.
I find it really useful to wrap the internal data structure for a utility code or for a business logic in a private class. Assuring that there won't be any external dependencies on my private class and I can alter this class any time. Plus, it keeps the encapsulation logic intact.
4. Resharper
If your company doesn't has licence, then you don't have a choice. But, if you have - then you "must" learn the basic shortcuts and enable them. It's really disappointing to work with a peer on a code review and get to notice that, instead of having it installed/licensed, it's not being used at all.
5. Explicit implementation of interface.
I have only heard of this term when it comes to interviews. It is a good flexibility available while dealing with interfaces. I use this implementation when I want to make sure that all my users are only working on the underlying interface. There are various good reasons to not distribute concrete implementation in an API, and this feature makes it more controlled.
6. Int32.TryParse
I hate to see a Int32.Parse(xx) code! and I see it every now and then. This one avoids the exception and is there for a good reasons. Note: Even if the parsing is mandatory I would like to use TryParse and throw user friendly exception if the parsing fails. Invalid case exception is annoying and doesn't leaves any business reasoning behind.
7. ToString()
I am also bad at this one! But it's such a pleasure to see object details while in debug mode. Especially when the details are will thought of, as in ToString with IFormatable and only provide basic details in default format.
8. Immutable classes
Damn the feature of auto-implemented properties... I always see peers using those properties without considering the existence of a field at all (I know, we can create auto-property with private get but that doesn't leaves the underlying field as readonly). There are real good reasons to write immutable classes (most selling one is when your class acts as a key). In my case, I even prefer to write a simple POCO as a immutable (if possible) class. It's such a delight to watch a class with all readonly fields, a contructor/factory to create a full instance, and then a set of getter properties.
For instance, when was the last time you have seen (or written) an operator overloading in a custom class? A class written with private access modifier? Use of native attributes like Conditional, Flags ?
It's not necessary that a code written without making use of these features is bad, but then these features exists for some good reasons! I am stating below some of such features that I have been using and "miss" while reviewing others code. I am sure you will find scenarios in your existing code where you could use these features for good reasons.
1. implicit/explicit type casting operator.
I find it really useful for POCO scenario where I don't want the users to write scattered code to create a related type from instance of my class.
2. Conditional attribute.
I find it really useful to write a helper method that only stays in DEBUG code base (and hence is not deployed to production). It gives me assurance that the code would still be good without my method and the extra functionality would do the right thing while working in a DEBUG mode.
3. Private class.
I find it really useful to wrap the internal data structure for a utility code or for a business logic in a private class. Assuring that there won't be any external dependencies on my private class and I can alter this class any time. Plus, it keeps the encapsulation logic intact.
4. Resharper
If your company doesn't has licence, then you don't have a choice. But, if you have - then you "must" learn the basic shortcuts and enable them. It's really disappointing to work with a peer on a code review and get to notice that, instead of having it installed/licensed, it's not being used at all.
5. Explicit implementation of interface.
I have only heard of this term when it comes to interviews. It is a good flexibility available while dealing with interfaces. I use this implementation when I want to make sure that all my users are only working on the underlying interface. There are various good reasons to not distribute concrete implementation in an API, and this feature makes it more controlled.
6. Int32.TryParse
I hate to see a Int32.Parse(xx) code! and I see it every now and then. This one avoids the exception and is there for a good reasons. Note: Even if the parsing is mandatory I would like to use TryParse and throw user friendly exception if the parsing fails. Invalid case exception is annoying and doesn't leaves any business reasoning behind.
7. ToString()
I am also bad at this one! But it's such a pleasure to see object details while in debug mode. Especially when the details are will thought of, as in ToString with IFormatable and only provide basic details in default format.
8. Immutable classes
Damn the feature of auto-implemented properties... I always see peers using those properties without considering the existence of a field at all (I know, we can create auto-property with private get but that doesn't leaves the underlying field as readonly). There are real good reasons to write immutable classes (most selling one is when your class acts as a key). In my case, I even prefer to write a simple POCO as a immutable (if possible) class. It's such a delight to watch a class with all readonly fields, a contructor/factory to create a full instance, and then a set of getter properties.
No comments:
Post a Comment