7 ms·
Objects are essentially structs with namespaces and methods with implicit this params. If you can't have state then you throw structs out too. If you're going
by jayd16 6y ago
Objects are essentially structs with namespaces and methods with implicit this params. If you can't have state then you throw structs out too.
If you're going this far, you're essentially throwing away typing as well.
Are you actually just opposed to private fields?
- JackMorgan 6y agoSeparating functions and state has nothing to do with the type system. Otherwise how could you put types on the parameters to static functions? Check out F#, OCaml, or Haskell for plenty of examples of how you can have strong static types without mixing state and functions in a scope together.
- jayd16 6y ago>Otherwise how could you put types on the parameters to static functions? Object methods are just sugar for this, so whats the difference to you? When you say "separate" do you mean you just want it in different files? Different curly braces? Different namespaces? Is it inheritance and virtual functions?
- winstonewert 6y agoI think the key differentiater is that OOP (in some versions) wants to hide the data and not just implementation details. For example, a hash map data structure hides the details of how the hash map is implemented, but it doesn't hide the data. In some versions of OO, the object should not only hide the implementation details but also the data. Any function that needs that data MUST be a method of that object. That's where things go haywire.
- jayd16 6y agoHash maps hide lots of internal state though. I think the difference is hash maps have a clear, battle tested API and its obvious what should be promised by the interface and what should be internal implementation. Devs aren't clairvoyant so when we're making a new type we might sometimes get this wrong. But this is a fundamental issue with type systems in general and not OOP, is it not?
- addicted44 6y agoHash maps also don't (usually) have external effects. There arent many hash map methods that will actually modify the data. Standard OOP, on the other hand, seems to encourage such behavior. So, for example, a Company object could contain a bunch of Department objects, each of which contained a bunch of Employee objects. These people objects could also be referenced in areas outside of the company object. If I called a company.giveFinanceDepartmentARaiseInDollars(10000) function on the Company object, it would have an unpredictable impact on the Department/Employee objects. It may also have cascading effects in other parts of the application that may not be clear. On the other hand, if you represented this as a company hashmap, with the keys the department structs, you would have to do something like the following instead: company["finance"].employees.forEach(x => x.salary = x.salary + 10000); company["finance"].base_salary = company["finance"].base_salary 10000 This contrived example shows some of the pros/cons. OOP allows us to hide a lot of behavior/changes, which may mean less repetitive code. OTOH, it hides a lot of behavior/changes.
- meheleventyone 6y agoI think the problem is that your example is too contrived and you can hide implementation details by just having a function giveFinanceDepartmentARaiseInDollars(company, dollarAmount) that exhibits the same issues as the “object oriented version”. More generally the issue of global mutable state and keeping your own sanity as a developer is a problem across paradigms.
- addicted 6y agoThe real problem with my example was that I modified the strict instead of returning a new one. My “functional” code wasn’t actually functional. If I had written that correctly, by returning a modified copy using a map instead of modifying the strict in a loop, your example would also be covered, because the function you’re describing would not modify any of its inputs, but instead would return a new copy. And the function wouldn’t have any side effects either, so it would once again be completely clear what’s happening.
- karpierz 6y agoIn what versions of OO does a hash map hide the data? The definition of a map requires that you can access the data.
- tsimionescu 6y ago'the data' is a vague concept. Usually, a hash map doesn't expose, for example, the bu met array, or the length of the bucket array. Sure, you can ask the hash map to give you the contents associated with a hash key, but you can't access the entire data structure that the hash map represents. By contrast, in an 'extreme procedural' style, you would have a struct that is an array of buckets and an integer, and functions that can take such a struct and return information from some particular place inside it. In normal use you would just use the function, but if you're passing your hash map to someone else, there is no longer a guarantee that the length field still matches the actual buckets, or that objects are still arranged into buckets based on their hashes.
- winstonewert 6y agoA hashmap wouldn't hide the data. But say, a Customer object might. A purist OO might insist that you shouldn't have a GetName() method because any code which needs the Customer's name should be method on Customer.
- UnFleshedOne 6y agoThis particular example is probably a bad illustration, with all the recent rage about PII and privacy. In some cases (such as this) you want to make sure data doesn't leak all over the place and anything accessing it follows a strict path. Anybody modifying Customer object would hopefully be in the right mindset regarding personal information, but once it gets out as a string from GetName or GetEmail, all bets are off, it gets into all kind of logs. Anything security related is the same.
- winstonewert 6y agoI'm having a hard time seeing how OOP style data hiding could be effective at controlling PII. Is this a method you've put into practice?
- waffletower 6y agoIt is important to note the difference between state and mutable state as I believe you are conflating the two here. There is no reason that you cannot have structs.
- lmm 6y agoYou should have data structures, but not mutable state or reference identity (two sides of the same coin). Having a bundle of structured data with functions is great. Having those functions secretly change other data somewhere else in your application (you wanted a banana but got the gorilla holding the banana and the whole jungle) is not great.