Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Working in C# right now and agonizing over the class hierarchy...


What does it even mean? You can dump everything into one class hacky Python way if you want. You can use dynamic keyword if you don't care about type safety.


We're not talking about monolithic, do-everything, hacky classes. You could do the same thing in C#.

Static languages (usually) force you to use interfaces and sub-typing just so common code can be reused. Duck typing is a much nicer way of working - without having to jump through seemingly unnecessary hoops. The situation is even worse when you code for testability. Things that really shouldn't have an interface now require one, so you can mock out the object appropriately. This is all avoided in a language with duck typing.

This is actually where Go has a great impact. You get your static type checking without being forced to use explicit interfaces. It essentially uses duck typing for interface implementation.

You're free to enjoy static typing over dynamic typing all you like - but you shouldn't make the mistake of thinking dynamic typing is inferior in any way. It is different - just like imperative vs functional is different. You make a series of tradeoffs, that is all.



Yes, but it's not idiomatic C#. The point I've been trying to make is that both kinds of languages (dynamic and static) have their different virtues. You can drop down to dynamic in C#, but unless you have a very good reason, your co-workers will lynch you.


Makes sense.


I love C#, but when moving between it and python, I find I focus too much on the types and hierarchies.


I used to be like this before I realized I used class inheritance way too much and shifted to using composition in the majority of cases.


And .NET framework tend to overuse subclassing/overriding pattern where delegation is more appropriate. That frequently leads users also overuse subclassing/overriding.


++ this, I moved from C# to Typescript and see that pattern change as being the biggest difference as to the code style i write.

I started Typescript by subclassing but as I work more and more with it, I subclass less and less, and delegate more and more.


Where's the best place to brush up on composition vs inheritance?


If you're familiar with C#, Real World Functional Programming: With Examples in F# and C# [1] is an excellent resource for learning how and when to use composition over inheritance.

[1]: http://www.amazon.com/Real-World-Functional-Programming-With...


I thought composition vs inheritance was strictly an OO, not a functional thing?


Functional programming usually leans towards composition, and OO programming towards inheritance. "Hybrid" functional languages, like F# and Scala, allow you to use both styles, mixing them in whatever way is most useful for the particular problem you're solving; "pure" functional languages, like Haskell don't offer OO-style inheritance, since composition is a better fit for combining side-effect free ("pure") functions.

tl;dr -- Composition is to functional programming as inheritance is to OO programming.


Yeesh, $40 for the ePub directly from Manning, or $33 from Amazon for the print edition with a free ePub.


There are a few chapters of the book online for free at MSDN, and if you're going to buy the book, they also have a coupon code: http://msdn.microsoft.com/en-us/library/vstudio/hh314518%28v...


I agree, any tool must be used correctly.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: