2

There are a lot of questions on why OOP is correct or where it works etc , so this is one of them , however I am asking it from the perspective of Kernel and why it doesn't use as an example .

Kernel is mostly written in C . It hardly uses OOP . General consensus on OOP is that its the best paradigm in programming ( mostly by many of the new comers ) but Kernel despite being one of the most challenging areas of programming - doesn't use OOPS .

So I would like to know what are the cases where OOP works and why it can't work for things like Kernel ? I am trying to get the engineering aspects of why it works in some cases .You could say GUI programming might benefit from OOP - the reason for that from my understanding is that you are going to create more than one case of a particular thing and you might manipulate them - so having different states with same behaviour makes sense .

On a higher level , what are the good cases where OOP can be considered as a natural choice vs where would it just be an overdesign ? I would be happy to get pointers on this question beacuse this is already discussed on many places .

PS: I am looking for an engineering aspect . I understand this question is very broad . Any helpful pointers would be nice .

Some related questions : I can't create a clear picture of implementing OOP concepts, though I understand most of the OOP concepts. Why?

Community
  • 1
  • 1
Nishant
  • 20,354
  • 18
  • 69
  • 101
  • 2
    "OOP is considered to be the best paradigm in programming" an absolute claim that has no context. There are many a context where OOP is not considered the best paradigm in programming, e.g. kernel. –  Feb 13 '14 at 10:35
  • 3
    https://lwn.net/Articles/444910/ http://lwn.net/Articles/446317/ :) – mlvljr Feb 13 '14 at 11:06
  • Sami Laine : I am betting on it , many people think OOP is the best way to solve problems in programming world :-) Atleast in some parts of the world ! You know to write Class , and You know to Inherit and you know what is Abstract Classes - You are treated in a different league . – Nishant Feb 13 '14 at 20:59
  • http://docs.oracle.com/javase/tutorial/java/concepts/class.html --> In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model --> To this my thought would be what does Bicycle have anything to do with programming ! I don't create 100's of items of what i write usually . Nor do I think creating a Prototype of Bicycle is helpful . So what are some use cases - Kernel is more or less not following that approach except for some cases perhaps like mentioned below . – Nishant Feb 13 '14 at 21:39

3 Answers3

3

Most Unix kernels were using v-table-like call dispatch for various subsystems well before OOP made the headlines. Look, for example, at how VFS layer deals with different file system types.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • So can you elaborate ? Do you mean Unix needs to catch up with OOP or do you mean OOP is better solved using simple methods as long as it inhibits multiple behaviours ? I mean often the engineering problem I find personally is what should I consider an object and what I shouldn't - So I am trying to understand that. – Nishant Feb 13 '14 at 19:05
  • 2
    @Nishant - No, he's saying that Unix kernels were using code constructs similar to how OO languages implement oo features, but doing this in C. So many kernels mimic OO features in procedural code. – Erik Funkenbusch Feb 13 '14 at 20:42
1

The OOP is optimized for the programmer - it is easy to use and to debug.

The Kernel should be optimized for speed and memory - up to levels which OOP will never reach.

So, we CAN do low level programming in OOP, but it is bad low level programming.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • Can you explain what you mean by OOP is for programmer ? Do you mean OOP is for "library" codes ? – Nishant Feb 13 '14 at 15:51
  • @Nishant A program has three targets: programmer(s), users and HW. You can't create a code that will be the best for all three, let alone in limited time. So, we have to make compromises. As computers are fast and cheap, in most cases programmers do not optimize code for computer nowadays. Programmers are expensive and so the center had been moving to "better coding practices" that mean more convenience to programmers. User are precious... But alas, we manage to ignore it mostly. – Gangnus Feb 13 '14 at 16:24
  • Awesome , so its a 'philosophical' and not a 'programming' aspect ? I mean your point hits the nail . Computers don't need OOP to achieve its target . So this 'philiopshical' aspect is what is confusing me . I mean depending on the end user we change the code no ? I mean its really not so much discussed topic I think . – Nishant Feb 13 '14 at 18:03
  • @Nishant No, it is ECONOMICAL aspect. As for user, he is much discussed, but Gates sometimes said: "User friendly SW means SW with 'User friendly' written on the box". And for most programmers and managers it is so. – Gangnus Feb 13 '14 at 18:04
  • A program has three targets: programmer(s), users and HW is very sensible and true . I would like to analyse this question in that context . – Nishant Feb 13 '14 at 18:33
1

There are object oriented kernels. In fact, there's at least two kernels written in C# (the projects are called singularity and cosmos).

While it's not technically a problem with performance, as you can write very fast and efficient object oriented code, more often than not it takes more effort to write efficient code in an object oriented language (ie, oop languages often make it very easy to write code that isn't efficient). What's more, often various features of oop don't really help much in kernel programming because you have to write very low level code that fiddles bits or uses registers specifically.

It's the same reason people don't write OS's in other high level languages either... oop or not. C is simply a very efficient language to write kernels in. If there were no C, someone would probably invent it just for kernel development.

EDIT:

One other reason C is often chosen is that C has a very basic function dispatch system. Different OOP languages implement classes and virtuals and what not in different ways, and thus they require "bindings" to define how these objects and methods are called. C doesn't have any of that, and has a very basic function dispatch method. OO kernels would mean that code written in other languages either has to be written in the same language, or create complex bindings to make it interface.

For instance, if you write code in C, and you want to call a kernel function in a C# kernel, you now have to emulate C# to make that call... the other way around is a lot easier.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • But it is possible create classes that work with registers, too. – Gangnus Feb 13 '14 at 16:18
  • @Gangnus - Yes, it's possible. Like I said though, it's typically more effort to do so. – Erik Funkenbusch Feb 13 '14 at 16:27
  • Thank you, I only wanted to check for sure :-) – Gangnus Feb 13 '14 at 18:03
  • So Erik , basically there are two types of programming in the sense that is meant for another Developer to re-use - where OOP makes sense - and there is one for the Computer where logic matters - This distinction is really subtle IMO - I am trying to understand this more - its kind of hard for me to get the context . – Nishant Feb 13 '14 at 18:23
  • @Nishant - no. it's just that C is a more low-level language. You can more easily write very specific code for the assembly level in C, while most oo languages don't care so much about the specific way the code is generated. It's not truly oo vs procedural, it's more that C is a much better suited language to this because of it's low level code generation features. – Erik Funkenbusch Feb 13 '14 at 20:38