5 ms·
The F# code is no doubt more elegant because of discriminated unions and pattern matching, but the C# code in this comparison is a bit lazy/sloppy if you ask me
by alkonaut 11y ago
The F# code is no doubt more elegant because of discriminated unions and pattern matching, but the C# code in this comparison is a bit lazy/sloppy if you ask me.
I know it's a made up example to showcase discriminated unions, but it becomes a bit of a strawman to suggest that C# doesn't even allow representing discriminated unions. It does: through inheritance and tons of boilerplate code.
The correct demo I think would have been to make the correct type (which doesn't allow invalid states to be represented) in both languages, to show how in C# it required maybe hundreds of lines, and in F# it's just a few lines.
Under the hood, almost exactly the same types are created from F#, where the F# types when seen in C# or IL of course look very much like the types you had to meticulously type in C#.
- DanielBMarkham 11y agoBut no. The point was to show how when you're thinking in a language that supports algebraic types and pattern-matching, you automatically exclude states that can't exist. It's not that the underlying CLR or IL is any different given the same solution -- that's just measuring a language's verbosity. It's that some language constructs automatically lead you down the path to correct code without having to boil the ocean. Sidebar: I would have tightened the F# code up a bit. No need to have the Name string repeated in both types.
- inglor 11y agoOr, you could use a class Hirarchy with a User abstract class and ActiveUser and DeletedUser subclasses and use the type system to enforce the constraints. Using a nullable field and an enum is hardly the standard way in C#.
- nbevans 11y agoBut unfortunately it is the one a lot of C#'ers reach for because it is the smallest amount of code in that language and often results in the least refactoring effort. A properly designed model in C# like suggested is a large number of lines of code. And then because there is no pattern matching, you have to hand-roll your type checks/casts and make sure have that method throw an unconditional exception at the end for literally impossible states. Then if you make a change to this model, i.e. you add a new "SuspendedUser" subclass, you have to make sure you go through the codebase to update all those places you wrote a hand-rolled "pattern match" against that type. Because the compiler won't warn you about an unrepresented state on your type checks. And so then you have to make sure the whole project team is aware and you have to make sure new hires are familiar with the patterns being used. So yeah, the problems just snowball and then you wonder why you didn't just use a more appropriate language in the first place that has already taken complete care of all these trivial programming issues.
- rottyguy 11y agoBut then the article should be titled "Functional vs Non Functional approach to solving xxx" as opposed to c# vs f#. I think the OP was saying you could do it functionally in c# albeit less elegantly.
- 15155 11y agoDiscriminated unions, pattern matching are common but not universal features of functional programming languages.
- alkonaut 11y ago> The point was to show how when you're thinking in a language that supports algebraic types and pattern-matching, you automatically exclude states that can't exist. That should be the default line of thought in OO too! I admit it's easier to do with discriminated unions, but that doesn't change the fact that the C# class is still a common but poorly designed type in C#. The job of any type system is to make invalid states unrepresentable. F# has powerful tools for that, and make it intuitive and succinct. C# has fewer and less powerful tools, but it's still possible (and usually necessary) to use those tools! The equivalence of F# DU's are C# class hierarchies, and the equivalent of pattern matching is virtual methods. Inheritance and virtual methods is a really verbose and horrible way of going about it, but it's the way of going about it in C#. You have to use classes for the state here Since there is a deleted time only useful for the "Deleted" state of the user the user state shouldn't be an enum. It's one of the pillars of decent OO design: a you don't have a field in a type that is only used for some state of the type: that state should then be a subtype. The possible state values for the user is ActiveState and DeletedState { DeletedTime }. If a person wrote the C# class in that way out of ignorance rather than laziness, I bet they would write the F# type as a normal record type too!
- geocar 11y agoThis is an interesting topic I have been thinking about. I've come to the conclusion it is difficult to be fair when trying to introduce someone to an idea: An APL-er recently mused that palindrome-detection was a good example of array-thinking, specifically: {⍵≡⌽⍵} (or in K: {x~|x}) However another programmer made a quick check of the Rosetta Code[1] and observed almost all of the examples used that same method without "array-thinking". That is, unless we state that x == reverse(x) is succinct in many languages, it is easy be accused of being disingenuous, and we run the risk of letting the argument get away from us: After all, the atom solution actually requires fewer operations than the vector solution: for (i = 0, n = floor(length(x) / 2); i < n; ++i) if(x[i] != x[n-(i+1)]) return false; return true; and despite being longer, may actually be faster if we ever need to test very large strings. C# programmers often use Contracts. Sometimes they use "if+printf-debugging", sometimes they use "lots of boilerplate code and class inheritance", and sometimes they invent their own runtime assert+log mechanism. The discussion about how C# programmers prevent programs from entering invalid states should not have to admit this any more than I should have to test for very large palindromes. That is to say that contrived examples are contrived. So let us observe that many C# users don't define a method: delete :: User -> DeletedUser They could, but they don't. Now why don't they? [1]: http://rosettacode.org/wiki/Palindrome_detection http://rosettacode.org/wiki/Palindrome_detection
- masklinn 11y ago> They could, but they don't. Now why don't they? One of the bothersome issues is with a `delete :: User -> DeletedUser` method is you now have both a User and a DeletedUser, seemingly as valid as the other. Most languages share the issue too, the type-encoding of state change doesn't really work because an instance in the old state is still accessible, at best it might blow up at runtime. That's a place where affine types (à la Rust) are an improvement, because with `delete(User) -> DeletedUser` you'd completely lose access to the User instance.
- seanmcdirmid 11y agoSome research languages allow for dynamic inheritance in static settings, so ActiveUser -> DeletedUser is a valid transition that can be encoded typefully. However, without affine types and with aliasing, some dynamic checking is obviously needed.