by wvenable · 14y ago · 0 comments
Yes, it's much simpler to store RGB color in a three-element tuple when you're working on simple code that you've written all yourself. But what happens when get a tuple from somewhere containing {289, 345, -1}? Or god forbid {200, "Monkeyspit", {}}!
The measure of simplicity and complexity is a double-edged sword. Making everything a class for a small project, as the author concludes, is a recipe for confusion and pain. However, not using strictly defined self-contained data types for a large project is also recipe for confusion and pain.
"there's often an aesthetic insistence on objects for everything all the way down"
I don't think there's anything wrong with that -- in many languages arrays are objects, dictionaries are objects, and tuples are objects and they let you be as unstructured as you want to be -- but it's still objects all the way down.
data RGB = RGB Word8 Word8 Word8
The point made here has nothing to do with weak typing. It just so happens his example sort of implies that he might be using it, since the author rather likes Erlang and that appears to be the syntax he reaches for, but the entire article applies without loss to strongly-typed languages as well. (Of which Haskell is merely a convenient example. Even more conventional manifest typing, as in C, works well enough here.) If you want to declare that you have a map of Booglies to Wangdoodles, it's trivial, but you're still using off-the-shelf maps and not bashing together trees yourself. module Temperature (
TempCelsius
tempCelsius
) where
newtype TempCelsius = TempCelsius Double
absoluteMinimum :: Double
absoluteMinimum = -273.15
tempCelsius :: Double -> Maybe TempCelsius
tempCelsius t
| t < absoluteMinimum = Nothing
| otherwise = Just $ TempCelsius t
In other words, by hiding the constructor, you can control all access to a datatype. You could do the same with opaque pointers in C, etc.You just move type check to runtime and use some kind of contracts... but what does it have in common with OOP? It's closer to classic dynamic vs. static typing argument and as someone pointed out OO type systems are mostly inferior to Haskell's. The point being - it's not relevant here.
Not using well defined data types for in a widely exposed interface in a large project is suboptimal.
Overly formalizing data types for entities of limited scope is suboptimal on small and large projects.
We need to be careful about adding unnecessary weight to large projects, because it costs more there.
> in many languages arrays are objects, dictionaries are objects, and tuples are objects
I am not aware of a canonical definition of object. One thing that is commonly included is the idea that objects have identity. So "two" arrays with the same values are distinguished in the object world. This is not what people mean if they create an RGB from an array (or dictionary).
I don't think that is the case at all. In fact, it is specifically implied in the sentence that you chose to quote. Why is the top reply to every HN story always so darn contrarian and nit-picky (in this case, not even validly so)? Why can't it just once be "Yep, you nailed it." (or something insightful)
Insubstantial praise isn't really worthwhile unless invited. If it's invited, it'll be through comments on the article itself, not on an aggregator like HN.
I don't disagree with your overall point: the nitpicking is nearly as worthless: but if you don't have anything substantial to say, it isn't worth saying anything. Just upvote and move on.
In simplest terms, OOP combines opaque data structures with the operations performed on them. This should, if done correctly, prevent a whole lot of different kinds of easy to make mistakes. It also helps simplify and organize code.
I create a type for it? Why do you think RGB being bytes makes it special? Bools aren't bytes, but I can't pass anything other than True and False for a Bool.
>OOP combines opaque data structures with the operations performed on them.
That does nothing to prevent errors. Having the operations require the correct types does prevent errors on the other hand, regardless of whether or not the operations and data are put into an "object" together or not.
Bools are special too because the compiler handles ensuring that they are true and false (and you don't even get to choose to representation).
How would you handle a real complex type like Customer or Order or Window or Button?
> That does nothing to prevent errors.
Yes, it does. It's effectively the DRY principle -- you can't repeat the data manipulation code throughout the codebase. OOP formalizes some of the best practices for procedural programming.