News for hackers. Stuff that matters.
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by jupp0r on Wednesday June 21, 2023 @10:53PM (#36427493)
It's weird that so much of OOP back then was centered around deep class inheritance hierarchies as a means to share code when my favorite more modern languages (Rust and Go) intentionally leave out this part of OOP (thankfully, imho).

An eye opener in respect to this was Sean Parent's excellent talk "Inheritance Is The Base Class of Evil" at GoingNative 2013 [1].

[1] https://www.youtube.com/watch?v=2bLkxj6EVoM

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by rramadass on Wednesday June 21, 2023 @11:10PM (#36427590)
I really don't get why people keep repeating "class inheritance hierarchies are bad". If one understands Implementation vs. Interface inheritance and the intent behind each (the former strongly couples the code while the latter does not) then one can judiciously use inheritance techniques correctly.

I recommend a study of Bertrand Meyer's OOSC2 and language Eiffel - https://en.wikipedia.org/wiki/Object-Oriented_Software_Const...

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by jupp0r on Wednesday June 21, 2023 @11:17PM (#36427619)
The way I'm thinking about it (just a different expression of the exact same thought) is that inheritance as a means to share code is bad, while inheritance as a means to leverage polymorphism is good. Polymorphic classes can still share code, but this should be done using composition.
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by rramadass on Thursday June 22, 2023 @02:36AM (#36428689)
> inheritance as a means to share code is bad,

Not necessarily; if they are strongly coupled related concepts within the same module then it is perfectly fine (eg. private inheritance in C++). Here you are basically structuring for code reuse and not is-a relationship.

>inheritance as a means to leverage polymorphism is good.

This is the "standard" idea of inheritance i.e. sub-typing using Liskov Substitution Principle.

> Polymorphic classes can still share code, but this should be done using composition.

Not necessarily as a combination of the above two should make clear.

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by Fire-Dragon-DoL on Thursday June 22, 2023 @12:40AM (#36428068)
The reason is implicit makes really easy to miss things and the majority of software devs have very poor understanding of coupling. Consequence is if inheritance is a thing, you'll see monsters in the code.

If inheritance is not available, the problem doesn't exist.

Same goes for immutability, in the end our tools are tuning for the lowest performers

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by jupp0r on Thursday June 22, 2023 @01:23AM (#36428276)
The ability to reason about code as you are reading it is not only important to "lowest performers".
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by Fire-Dragon-DoL on Thursday June 22, 2023 @01:26AM (#36428299)
Absolutely agree, I very much prefer it
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by rramadass on Thursday June 22, 2023 @02:46AM (#36428762)
Not true.

The basic idea behind "Inheritance" is simple and can be understood by all programmers. Its realization through various language features and their combinations is what makes it somewhat confusing. Once explained and practiced with discipline, inheritance is very powerful.

See my other comment: https://news.ycombinator.com/item?id=36428689

See also: https://thevaluable.dev/guide-inheritance-oop/

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by lelanthran on Thursday June 22, 2023 @05:58AM (#36429910)
I agree.

> The basic idea behind "Inheritance" is simple and can be understood by all programmers.

Not just by programmers. Just about everyone says "give me something like this, but with the following changed."

That's the basic premise of inheritance and it clicks with everyone.

I think the fact that when listing languages sorted by popularity, you can draw a line in the list, and everything above that line supports traditional OO while everything below that line provides band aids for it says a lot about how much OO clicks with programmers.

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by jupp0r on Friday June 23, 2023 @01:21AM (#36443109)
Just because something is easy to understand at first does not make it suitable for designing maintainable software. I can understand the concept really well and it sounds completely appealing. When digging through the 18th code base with 12 deep inheritance hierarchies and various overwritten or not overwritten or combination thereof member functions where all you can do to follow control flow is to employ a debugger do you get the notion that this simple idea might not be so great after all.
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by rramadass on Saturday June 24, 2023 @01:21PM (#36460680)
Your comment doesn't seem to be related to "Inheritance" at all but merely a opinion drawn from your anecdotal experience and hence not generalizable. As a counter-point i have read/written my fair share of hairy OO-code (mostly C++) and i can assure you that while it was a-lot-of-work sometimes (due to the complexity of the codebase itself) it was never "throw-up-my-hands" difficult once i applied some system and method. I hardly ever use a debugger but use tools like Doxygen/Cflow/cscope/etc. to get the static view of class hierarchy/call graph and profiler/printf trace/etc. to get the runtime dynamic call graph. These give me enough insight into the architecture to become productive.

It is also a fact that OOD/OOP has been a enormous success in the real world in structuring "Frameworks" which give you a generic driver skeleton application for free into which you plugin your application specific code using (you guessed it!) "Interface/Implementation Inheritance".

Finally i recommended Bertrand Meyer's OOSC2 book because nobody explains OOD/OOP better then him in a thorough and precise manner which clears almost all of one's doubts.

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by Fire-Dragon-DoL on Saturday June 24, 2023 @03:15PM (#36461801)
I really appreciate your comment and it's absolutely true that I have no data. I don't think it's entirely anecdotal though, most newer languages and in case of JS newer paradigms are all strongly pushing against any form of inheritance, so this is an industry-wide change. I'm thinking of Go, Rust and the whole JS ecosystem that tries to write functional code (there might be others).

I wouldn't put "class inheritance" in the same ballpark as "interface implementation", one is sharing code, the other is adopting a protocol. In my statement I was referring strictly to class inheritance for the purpose of code sharing.

Unfortunately it's hard to bring up any meaningful numbers when talking about programming languages.

Define success too: OOP has been used widely, but it's way harder to find numbers representing if the codebase is easier to navigate and maintain versus this new industry trend. Also, there is massive failure with OOP, it's just less documented than the success (look at all the companies that died due to poor software).

And yes, all I can bring is an "industry trend", but the "enormous success" you are talking about in real world is effectively an industry trend too, but of course that's been going on for way longer.

The tools you are talking about sound very interesting, but those are effectively "undoing" the inheritance, isn't that the case? The code should be written for humans, and you seem to be using a tool to literally put all the dynamic calls back into one place so that it's easier to follow.

P.S. I'll check the book

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by rramadass on Sunday June 25, 2023 @12:32AM (#36465157)
>The tools you are talking about sound very interesting, but those are effectively "undoing" the inheritance, isn't that the case?

Oh, No!

Any system (OO or not) has two views;

1) A static view which gives the structure of the system (which is what we try to find out by browsing the codebase). Eg. class diagrams, ERDs, Sequence diagrams, State diagrams, static call graph etc. My first category of tools help you with this.

2) A dynamic view which gives the runtime call graph and state changes as the code executes. Obviously, this is harder to understand if "dynamic dispatch" of function calls are involved (which is the case in OO inheritance). My second category of tools helps you with this.

Remember, OOP is just another way of architecting Procedural/Imperative code and hence we have the same challenges (and a little extra :-)

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by Fire-Dragon-DoL on Sunday June 25, 2023 @10:06PM (#36474883)
I've been going through the book all day long, but I'm not too enthusiastic about it. It focuses so much on the "coding" that it seems to transcend the human aspect of it, that is, there will be developers that are not able to properly split classes, there will be teams, there will be incorrectly shaped boundaries, time constraint and even language limitations.

The book seems to be great at doing an incredible analysis of an OOP language, but it doesn't seem to consider the fact that the human portion of it matters a lot.

Sometimes generalizing the code so that it supports all use-cases is more expensive than just rewriting the part of the code that will change. This statement is what feels my thought as I read through the book. And even in the analysis, it seems to focus on the ideas, but not why (and numbers) that brought to this.

I bring a simple example. I have no idea what this class does:

    class Foo < Bar
    end
I know exactly what this one does:

    class Foo
      def initialize
        @bar = Bar.new
      end
    
      def something
        @bar.something
      end
    
      def whatever
        @bar.whatever
      end
    end
With the tools you described, it makes sense, you can achieve this, but without them, the problem becomes serious.

The "advantage" of the first one is that when Bar changes its public interface, Foo will change too. I don't think this is a good idea. It's nice you have to write less, but whenever a public interface changes, it should break things.

Then again, I definitely didn't write a book or have any numbers. I'll keep going through the book, but it's been quite frustrating. Very academic, so I have to skim chunks of it.

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by rramadass on Monday June 26, 2023 @12:32PM (#36482020)
It is great that you are looking into OOSC2. Yes Meyer is somewhat difficult to read since he is so thorough, precise and detail-oriented that it can become tedious at times. Take your time going through the book to get the big picture of his OO design/implementation arguments rather than the details of the Eiffel language (not necessary during the first pass). It is a huge book so skip topics as needed to focus on the essential concepts.

You should also look into Meyer's "Design By Contract" technique for writing "correct" OO and other-paradigm programs. This is of great value in real-world programming.

PS: You might also find another Meyer's book Agile! The Good, the Hype and the Ugly quite instructive - https://learning.acm.org/techtalks/agile

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by Fire-Dragon-DoL on Saturday June 24, 2023 @11:30AM (#36459522)
Others have responded, but yeah, it's easy to understand inheritance, but doesn't make it suitable for designing software.
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by rramadass on Saturday June 24, 2023 @01:23PM (#36460704)
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by Fire-Dragon-DoL on Saturday June 24, 2023 @03:17PM (#36461816)
Thank you!
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by DarkNova6 on Thursday June 22, 2023 @03:29AM (#36429045)
I think inheritance is the most convenient thing which came out of OOP, but interfaces are the most valuable one.

At least for business applications, you want to keep your behaviour and state separate. And people following OOP dogmatically will (ab)use encapsulation for use-cases where your data should be transparent.

Contrast this to interfaces, which are essentially composable specifications on code-level (and I absolutely love that Java uses interfaces to describe anonymous classes due to the power it gives you for documentation and specification).

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by rramadass on Thursday June 22, 2023 @09:27AM (#36431845)
You might find Program Development in Java: Abstraction, Specification, and Object-Oriented Design by Barbara Liskov with John Guttag interesting.
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by DarkNova6 on Thursday June 22, 2023 @11:59AM (#36434203)
I am always a sucker for good book recommendations. Thank you!
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by rramadass on Thursday June 22, 2023 @12:57PM (#36435191)
The book is by the great Barbara Liskov herself (https://en.wikipedia.org/wiki/Barbara_Liskov) the pioneer of Abstract Data Types, Subtyping, OOD etc. It is basically a rewrite of their classic text Abstraction and Specification in Program Development (1986) with the example language now being Java. It is not a popular cookbook but a fundamental programming concepts book which needs serious study. I highly recommend it.
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by gyrovorbis on Friday June 23, 2023 @02:00AM (#36443337)
It's just the latest hipster language trend. When OO was new, structural C was evil. When functional programming happened, everything before that was evil...

Because, yes, sharing state among classes for code reuse, is fundamentally evil... How dare you want to couple state and logic?

Almost like every single UI framework in every mainstream language has converged upon OO as the best way to model the problem at hand... GTk even invented a similar type system to what was mentioned in this PDF just to give C OOP constructs... Now witness as the Rust language has failed to produce a compelling UI/widgeting framework or paradigm, and the best ones out there are Qt-based or GTk-bindings (letting C hilariously handle the OOP for Rust).

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by rramadass on Friday June 23, 2023 @02:00PM (#36450232)
>It's just the latest hipster language trend. When OO was new, structural C was evil. When functional programming happened, everything before that was evil...

Right.

By any measure OOD/OOP has been enormously successful in the real world and hence it is annoying to see blanket statements like "OOD/OOP/Inheritance/etc. is bad" on HN (i expected better). It is one thing to discuss cautions/caveats/different approaches but to dismiss the whole thing is quite silly. Hence my attempt at edification of the readers :-)

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by Kamq on Wednesday June 21, 2023 @11:21PM (#36427649)
Yeah, that still weirds me out. Especially considering the other obvious mechanism of code reuse, the function.
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by jupp0r on Wednesday June 21, 2023 @11:41PM (#36427772)
State can also be shared and reused together with functions operating on it - via composition.
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by Kamq on Thursday June 22, 2023 @12:15AM (#36427949)
Sure, but that's desirable with, maybe, 5% of the code re-use opportunities I run across.

I'm not saying to never use classes. I just got really confused about them being pushed as one of the dominant forms of code-reuse.

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by jupp0r on Thursday June 22, 2023 @01:32AM (#36428330)
Agreed, I think it's been a hype at the beginning of OOP, then got overused in Java in the 90s and subsequently taught to every CS college student and it kind of stuck because in college, you don't get to maintain large software projects.
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by vaughan on Thursday June 22, 2023 @07:24AM (#36430472)
OO is the king of the contrived example

    const dog = new Dog
    dog.woof()
Now you see how simple OO is, let's build banking software.
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by lelanthran on Thursday June 22, 2023 @02:21AM (#36428590)
> modern languages (Rust and Go) intentionally leave out this part of OOP (thankfully, imho).

Why thankfully?

Some solutions are a natural fit for a class hierarchy. Having to hack in workarounds using traits or receivers is, like any band-aid, ugly as hell.

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by jupp0r on Friday June 23, 2023 @01:41AM (#36443229)
There's a sizable chunk of people who think that the concept of inheritance for code sharing is causing more harm than good overall. These languages make it hard to do inheritance for exactly that reason.
Re:Object-oriented Programming with ANSI-C (1993) [pdf] by lelanthran on Friday June 23, 2023 @05:57AM (#36444792)
> There's a sizable chunk of people who think that the concept of inheritance for code sharing is causing more harm than good overall.

Maybe. What I've seen online is that there is a sizable chunk of people who think that the concept of inheritance is causing more harm than good overall. I don't see many adding the qualifier "for code sharing".

> These languages make it hard to do inheritance for exactly that reason.

It makes those languages a poor fit for some problem domains, like GUIs, for example. Or games. Or many enterprise systems.

Which is why I am wondering why someone would be thankful that creating programs in certain very popular domains was made more difficult than the alternatives.

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by gyrovorbis on Friday June 23, 2023 @02:04AM (#36443366)
"Thankfully" he says.

Now witness as Rust struggles to produce a compelling widgeting environment that isn't Qt or GTk bindings to other languages which support OO constructs. Witness as apparently creating a "Rust-friendly" OO paradigm or framework is an ongoing academic endeavor, because of this...

Believe it or not, for some domains, object-orientation is the right tool for the job... as some problems are just inherently modeled better with such a paradigm, and are able to take advantage of what it has to offer (encapsulation, polymorphism, inheritance).

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by mikro2nd on Thursday June 22, 2023 @04:05AM (#36429277)
OOP was never "centered around deep class inheritance hierarchies". It was centred around message passing and dynamic method dispatch. The only significance of inheritance is to resolve the receiver of a message.

I feel strongly that most of the people who think OOP was bad are people who have only ever experienced C++/C#/Java or some even weaker instantiation of OOP.

What do you suppose you're writing when you implement a (micro)service? It's nothing different than an object -- some implementation whose details you don't need to care about when you use it, which details can be changed at any time without disturbing clients (modulo idiocy), and an interface that you use to communicate with it via messages.

And don't even get me started on the OO vs. Functional nonsense...

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by gpderetta on Thursday June 22, 2023 @06:24AM (#36430074)
Simula67, arguably the first OO language, was centered around inheritance.

The other early major OO language was obviously Smalltalk which focused on message passing.

Both C++ and Java were majorly influenced by Simula, while more dynamic languages take on aspects of Smalltalk.

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by pjmlp on Thursday June 22, 2023 @08:51AM (#36431360)
Java is clearly influenced by Objective-C and Smalltalk in semantics.

Not only their authors are on the record saying so, Java EE is inspired in an Objective-C framework (Distributed Objects Everywhere), and Hotspot is the evolution of Strongtalk JIT.

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by gpderetta on Thursday June 22, 2023 @09:12AM (#36431633)
I'm sure that Objective-C and Smalltalk have also been strong influences, especially in the libraries. But I would say that the OO semantics are very Simula like.

Can you really claim as Smalltalk inspired a language without an easy to use DoesNotUderstand? That's pretty much the basis for first class messages and transparent delegation.

Also: https://www.youtube.com/watch?v=ccRtIdlTqlU

Re:Object-oriented Programming with ANSI-C (1993) [pdf] by pjmlp on Thursday June 22, 2023 @01:47PM (#36435975)
Java authors are quite clear where the semantics come from,

https://cs.gmu.edu/~sean/stuff/java-objc.html

The C++ like syntax was to make it more marketable.

Java, like Smalltalk and Objective-C, and others in Xerox linage, is a full stack experience, naturally one cannot dissociate the language from the JVM, that is programming with one arm tied behind the back. Language and platform were designed to work together.

As for doesNotUndertstand, for some limited use cases you would use dynamic proxies,

https://docs.oracle.com/javase/8/docs/technotes/guides/refle...

Tutorial is for Java 8, but java.lang.reflect.Proxy exists since Java 3 (1.3).

And as for reaching out to JVM low level stuff, there is invokedynamic, JVM version of .NET DLR.

Also this meta-level programming is quite common in Smalltalk, when playing around with metaclasses, and Smalltalk APIs for the VM.