4 ms·
Can a "class", eg in Python or Java, be considered an example of the "data structure" Linus and others are talking about here? Or are they only talking about t
by antipaul 2y ago
Can a "class", eg in Python or Java, be considered an example of the "data structure" Linus and others are talking about here?
Or are they only talking about tables in databases and such?
- layer8 2y agoThe class interface (interface contract) falls under the general point being made, but not the class implementation (implementation details).
- RangerScience 2y agoIMO, “wrong question”, but thanks for asking. IMHO - “class” definitions are really just a data structure (abstractly, a possibly-nested hash) with associated functions, and that’s it. This is extremely apparent in Perl, where that’s literally what a “class” is - a hash data structure “blessed” with a class name. JS is almost identical (a class is a hash where some of the values are functions), and “under the hood” both Ruby and Python are similar (although in very different ways - consider that the first arg in a member function in Python is the instance). Database schema is (usually/mostly) just that structure without those functions; then you maybe use an ORM to provide them again. So, IMO, on a theory and design level, both the database table and the data part of a class definition are “data structures” (note that you should include in considerations the relationships between data structures)
- jghn 2y agoTo take this a step further, IMO a big issue with how OOP is taught & practiced is that people approach it from the wrong direction. As classes provide both data and functionality, it's better to first model your data and then think about the functionality to provide on those data. But people tend to think about relationships & actions (i.e. functionality) first and model their data to that. The two approaches will tend to produce very different data structures. I'd agree with the sentiment from the OP that being data-first is ideal.
- 0xbadcafebee 2y agoYes, classes are data structures. A data structure is the construct by which you organize, process, retrieve, store, etc data. A traditional "struct" or "table" is how people think of a data structure, as a sort of dumb layout of 1s and 0s in a file or memory somewhere. But how that data is separated, accessed, what constitutes "valid" data, how it is presented to you, what you can do with it, etc is all part of what constitutes the "structure" of the data, because the "structure" enforces what that data looks like, how it behaves, etc. A class fits that purpose aptly. If water is data, then a straw, a vase, a kettle, a pool, a water pump, even a hydroelectric dam, are all data structures. They enforce the form of the water, what goes in, how you access it, how it comes out, etc. You might say "but doesn't a function do the same thing?", and, well.... yes. A data structure is kind of like a tightly-bound collection of functions that take in data and put out data in particular ways. A data structure in memory or on disk, without program logic attached to it to enforce the structure, is a "data format". The hope is that a program will do the right thing with the data according to its intended structure.
- mrkeen 2y agoYes, but I'd read it with less emphasis on the behaviours (which is what makes a class.) So, more like a struct.