News for hackers. Stuff that matters.
Re:Object-oriented design patterns in C and kernel development by pavlov on Wednesday August 27, 2025 @09:46AM (#45039604)
In Smalltalk and Objective-C, you just check at runtime whether an object instance responds to a message. This is the original OOP way.

It's sad that OOP was corrupted by the excessively class-centric C++ and Java design patterns.

Re:Object-oriented design patterns in C and kernel development by pjmlp on Wednesday August 27, 2025 @09:53AM (#45039697)
Actually I would say that it is sad that developers learn a specific way how a technology is done in language XYZ and then use it as template everywhere else, what happened to the curiosity of learning?
Re:Object-oriented design patterns in C and kernel development by ryao on Wednesday August 27, 2025 @11:11PM (#45047937)
The curiosity of learning is infeasible given that there are >15,000 programming languages. You might say to only learn the major/influential languages, but I have found my curiosity to wane with every additional language that I learn. So far, I have either used or dabbled in C, C++, FORTRAN, SML/NJ, PHP, Java, JavaScript, Python, Go, POSIX Shell, Assembly and SQL stored procedures. That is not to mention different dialects/versions of those, and the fact that assembly itself is a catch all category in which I have dabbled in multiple languages too. I am also not including languages for which I spent a minuscule amount of time (say a few hours), such as D (as in DTrace), Objective-C, Perl, COBOL and LISP. Then there is the misadventure into C# I took before I actually understood programming where I got stuck and dropped it. I am also not sure if I should mention AWK, as I only use it to select the Nth field in a bunch of lines of text when doing shell scripting and nothing else. Thus, I have used it many times, yet know almost none of its syntax.

Every few years, someone tells me I should learn another language, and in recent years, there just is no desire in my mind to want to learn yet another language that is merely another way of doing something that I already can do elsewhere and the only way I will is if I am forced (that is how I used Go).

That said, I do see what you are saying. C++ for example has an “support every paradigm” philosophy, so whenever someone who has learned C++ encounters a language using a paradigm that C++ assimilated, there is a huge temptation to try to view it through the lens of C++. I also can see the other side too: “C++ took me forever to learn. Why go through that again when I can use C++ as a shortcut to understand something else?”. C++ is essentially the Borg of programming languages.

Re:Object-oriented design patterns in C and kernel development by 1718627440 on Thursday August 28, 2025 @01:28AM (#45048724)
How do you got proficient in so many languages? I think it takes some years, before you start to think in a language.
Re:Object-oriented design patterns in C and kernel development by johnisgood on Thursday August 28, 2025 @02:36AM (#45049122)
I did not know they say the same thing about programming languages.

Based on his comment, I did not think that he is proficient in them, but that he has used them, which is fair enough, so have I, sans all the ones tied to either Apple (Swift) or Microsoft (C#).

I have some projects in Haskell just for curiosity's sake, and because what I wanted seemed like it would be nice in Haskell, and it indeed looks quite elegant to me, for this one particular project. Haskell is not a language I would use generally. OCaml is.

Re:Object-oriented design patterns in C and kernel development by ryao on Thursday August 28, 2025 @05:13AM (#45050069)
You are correct. I am only highly proficient in a few of them. The others are ones that I have used for varying sized projects for varying reasons, but I only learned the subsets I needed for my purposes. I would say my ability in the others in my main list varies within the low to intermediate range of proficiency.
Re:Object-oriented design patterns in C and kernel development by johnisgood on Thursday August 28, 2025 @05:53AM (#45050308)
Yeah, same here. This Haskell project is nothing serious, it just prints you the difference in time between two dates. I thought the Haskell implementation would look nice and it did. I initially made it in bash but I realized this is much more complex than what bash can handle. I had to calculate for particular calendars among a lot of other things. I think it works, more or less, however.
Re:Object-oriented design patterns in C and kernel development by ryao on Thursday August 28, 2025 @04:52AM (#45049933)
My proficiency in them varies. I have used them for projects, such that I can write code in them (provided I have references to read), but when I do, I only learn/use the subset of the language that I need. If I want to read code others have written, then I need to know the subset that they used and that is not always the subset that I know. Most of them are languages that I have used for a handful of projects or in the case of assembly, for very small portions of projects, with at least half of that spend just trying to understand how good of a job the compiler did in critical loops.

There are some languages in which I am extremely proficient. My best language is C, which is my favorite and I have used most features of every version of C from C89 to C11. My second best is probably either C++ or POSIX shell (although I have moments where I forget certain syntax and need to look it up, especially in POSIX shell for variants on variable substitution, e.g. ${VAR%%foobar}). I have used most features of C++98 and some from newer versions. My experiences with C++ have soured me on it, so I now try to avoid C++ whenever I can in favor of C and Python.

My first language was actually PHP 4.2.y, and I was fairly proficient in it, having spent a long time learning it while simultaneously writing my own code for a website as a teenager. However, I never once touched the portions describing objects/classes, namespaces or exceptions. Someone else at work writes Modern PHP code using Symfony and I have taken a peek at it. It looks very different from the PHP I knew because it uses the features I had avoided learning (and probably some new language features too), although I can sort of read it thanks to having learned those concepts in other languages.

I used SML/NJ and Java in college. Years after college, I modified an open source Android TV application written in Java to add some things I wanted, although honestly, beyond that I have not really touched either language. Give me an arbitrary application written in either to improve and I would have some difficulty, although I will probably be able to do it after filling the gaps in my understanding (and doing plenty of head banging if it is a large program).

I have used JavaScript for a few recent projects via electron/nodejs and I have done several small things in Python over the past several years. Each time, I only worked with the subset that I need. I am far from a master of either language that can understand arbitrary code written in them, but I am able to manage as far as my specific needs are concerned.

I could continue listing my experiences doing things in languages (like the time in college that I wrote some basic programs in FORTRAN 90 to try to learn it), but they really are not that interesting. It was often a project here or a small application there, and as I readily admitted, I only used a subset of most of the languages. For programming, a subset of commonly used bits is often all you need.

Re:Object-oriented design patterns in C and kernel development by bluGill on Wednesday August 27, 2025 @12:02PM (#45041478)
Java is excessively class centric. C++ often is, but it need not be and developers are movingiaway.

smalltalk is not original OO - c++ took oo from simula which was always a different system.

Re:Object-oriented design patterns in C and kernel development by mettamage on Wednesday August 27, 2025 @09:53AM (#45039692)
Wait, so in obj-c, could you also write some kijdnof doesnotunderstand method to achieve some dynamic method dispatch?
Re:Object-oriented design patterns in C and kernel development by pjmlp on Wednesday August 27, 2025 @09:55AM (#45039723)
Yes, that is how microservices were implemented in the days of NeXTSTEP. with PDO.

https://en.wikipedia.org/wiki/Portable_Distributed_Objects

Re:Object-oriented design patterns in C and kernel development by mettamage on Wednesday August 27, 2025 @02:37PM (#45043315)
So why did Swift become a thing? Or does Swift has this too?
Re:Object-oriented design patterns in C and kernel development by bitwize on Wednesday August 27, 2025 @05:52PM (#45045781)
Because Objective-C does not have foo.bar() style method calls and that's what everybody else uses and wants.
Re:Object-oriented design patterns in C and kernel development by saagarjha on Wednesday August 27, 2025 @09:01PM (#45047120)
Because Swift adds many other features.
Re:Object-oriented design patterns in C and kernel development by astrange on Wednesday August 27, 2025 @09:10PM (#45047177)
Swift isn't messaging-based, it's protocol-based. (Except it's also messaging-based if you use @objc.)
Re:Object-oriented design patterns in C and kernel development by pjmlp on Thursday August 28, 2025 @02:14AM (#45049000)
Because Objective-C being based on C, means it would never be safe while remaining compatible with C.

Also dynamic runtime dispatch Smalltalk style can never be as fast as the VMT based dispatch, or compile time dispatch via generics, even with all the optimizations in place, that objc_msgSend() has had during its lifetime.

Still, Metal is implemented in Objective-C, so there is that.

Re:Object-oriented design patterns in C and kernel development by 1718627440 on Thursday August 28, 2025 @01:29PM (#45054778)
Objective-C already has a different compiler I fail to see how the compiler is restricted in the checks it does.
Re:Object-oriented design patterns in C and kernel development by ryao on Thursday August 28, 2025 @05:08PM (#45057058)
There is always a subset of the population for which it is fashionable to try to make a new language to make things easier. It is inevitable that someone in a sufficiently large organization will try to make one, provided management supports it. Apple is a very wealthy company that is happy to sponsor R&D, had someone interested in this that they sponsored and the result was appealing enough that they decided to adopt it.

That is my understanding of how the process generally works and what I am willing to guess happened. Prior to this, they had been making incremental changes to Objective-C.

That said, from what I have seen of the syntax of both languages, swift’s syntax is nicer and that is not something that they would have been able to get from Objective-C. They already had tried syntax reform for Objective-C once in the past and abandoned it.

Re:Object-oriented design patterns in C and kernel development by astrange on Wednesday August 27, 2025 @09:11PM (#45047180)
It's how many things are implemented, like UI message routing, IPC, undo, test mocks.
Re:Object-oriented design patterns in C and kernel development by moregrist on Wednesday August 27, 2025 @12:37PM (#45041910)
> In Smalltalk and Objective-C, you just check at runtime whether an object instance responds to a message. This is the original OOP way.

This introduces performance issues larger than the typical ones associated with vtable lookups. Not all domains can afford this today and even fewer in the 80s/90s when these languages were first designed.

> It's sad that OOP was corrupted by the excessively class-centric C++ and Java design patterns.

Both Smalltalk and Objective-C are class based and messages are single-receiver dispatched. So it’s not classes that you’re objecting to. It’s compile-time resolved (eg: vtable) method dispatch vs a more dynamic dispatch with messages.

Ruby, Python, and Javascript all allow for last-resort attribute/message dispatching in various ways: Ruby via `method_missing`, Python by supplying `__getattr__`, and Javascript via Proxy objects.

Re:Object-oriented design patterns in C and kernel development by chunkyguy on Wednesday August 27, 2025 @02:01PM (#45042898)
> This introduces performance issues larger than the typical ones associated with vtable lookups.

Don't know about other programming languages but with Objective-C due to IMP caching the performance is close to C++ vtable

  Name Iterations Total time (sec) Time per (ns)
  C++ virtual method call 1000000000 1.5 1.5
  IMP-cached message send 1000000000 1.6 1.6

https://mikeash.com/pyblog/friday-qa-2016-04-15-performance-...
Re:Object-oriented design patterns in C and kernel development by 1718627440 on Wednesday August 27, 2025 @01:52PM (#45042756)
When you fear about the branch miss, then you can also just call the method and catch the SIGSEGV. I think if you allow for the possibility of there being no implementation, then you can't really not have some decision there. This would also apply for say a C++ virtual method.
Re:Object-oriented design patterns in C and kernel development by frutiger on Thursday August 28, 2025 @07:01AM (#45050686)
A SIGSEGV is not guaranteed when calling an address that does not have a function.
Re:Object-oriented design patterns in C and kernel development by 1718627440 on Thursday August 28, 2025 @12:10PM (#45053924)
Are you talking about C? In C it's not guaranteed whether a non existing function will result in an actual function call. As soon as an actual function call was generated by the compiler, a modern CPU is very likely to trap.

We are talking about an optimization of a language implementation here. This would be very much written in a ASM or another language were this is defined.

Re:Object-oriented design patterns in C and kernel development by pjmlp on Thursday August 28, 2025 @08:24AM (#45051300)
In NeXTSTEP it was fast enough to have the whole OS, including device drivers, written in Objective-C.

In Smalltalk systems that stop being an issue after JITs got introduced.

Re:Object-oriented design patterns in C and kernel development by MangoToupe on Wednesday August 27, 2025 @08:18PM (#45046892)
Sure, but that is a a horrible coding pattern. Even good smalltalk code doesn't rely on this. It's dogshit and lazy
Re:Object-oriented design patterns in C and kernel development by baranul on Thursday August 28, 2025 @04:29AM (#45049817)
This is a very good point. A greater distinction needs to be made about class-based versus class-free OOP. Many of the problems or constraints around OOP that people don't like, come back to classes.

Often class-based programming is confused as being the only style of OOP, superior to all other styles, or heavy-handedly pushed on others. Many programmers are perfectly fine with using objects or only specific features of OOP, without classes, if they are "allowed" to.