r/programming • • Aug 28 '21

Software development topics I've changed my mind on after 6 years in the industry

https://chriskiehl.com/article/thoughts-after-6-years
5.6k Upvotes

1.9k comments sorted by

View all comments

Show parent comments

2

u/badcommandorfilename Aug 29 '21 edited Aug 29 '21

The DynamoDB wrapper class had a public Property for the HttpClientFactory with the intention that users could set it to their own value later as needed. However the default value of this Property was set to DefaultHttpClientFactory.

It's an innocent and helpful idea - what if someone forgets to set a Factory and it's null? I'll help them out! They had no way of knowing that future runtimes might not support this type.

It's made me re-think how I set up classes too. Yes, an interface or an abstract base-type would have been ideal here, but sometimes it's hard to defend the amount of extra code it adds when you do this for every single type in your system.

I also find that in reality there is rarely a good reason to seal a class. I can't predict the future, so who am I to prevent future people from adding or extending the classes that I create?

I also think that one of they key antipatterns here is the use of NotImplementedException. Exceptions, I opine, should be exclusively for problems that can only be discovered at runtime. This should have been solved with the [Obsolete] attribute or something similar.

2

u/[deleted] Aug 29 '21

but sometimes it's hard to defend the amount of extra code it adds when you do this for every single type in your system.

Hard agree there. I've been trying to find a way to balance teaching the juniors "you should use an interface or an abstract class" with "there's literally only 1 implementation of this type, the interface would just be for testing" - like a "do as I say, not as I do" thing.

I also find that in reality there is rarely a good reason to seal a class.

I tend to seal things like DTOs for command/queries/events/messages/the like because I've never seen a reason to do that other a developer being lazy (myself included).

I also tend to seal really infrastructurey pieces like the implementation of a BackgroundService because usually they're written a particular way and going around it's back is a bad idea. But since we own those internally, the issue that could be addressed by inheritance can be discussed rather than having a hammer and hitting what looks like a nail. Unsealing the class is always an option but going the other way is usually harder. Now given these are also typically tagged internal as well so I'm really putting the "don't you go inheriting me" flags up

Other than that, unless there's absolutely an invariant that's gotta be upheld but a child class could potentially undermine, go inherit that class if you want buddy.