[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: What's the easiest and/or simplest part of Linux Kernel?





Le 28.08.2013 22:49, Jerry Stuckle a écrit :
* which is doable in C. People saying that C can not do OOP are
incompetent or lie. I do not really like C programming, but paradigm
have nothing related to syntax, only with writer's minds.
Some people may not like OOP, and they will be true, it is not always
good to use it.



Definitely NOT doable in C - you just don't have the tools.  While
you can do some parts of object based programming like message passing
(i.e. calling functions to operate on structure members instead of
accessing them yourself), and can even use tricks to "hide" structure
members (i.e. the known interface is just a dummy
char[appropriate_size] while the real structure is hidden), this can
be sensitive to compilers and in some cases even compiler options.

I do not consider that the public/protected/private keywords are the most important things in OOP.

You can't even define constructors and destructors for structures in
C, and have to take extra steps to ensure structures are constructed
and destroyed properly.

What are constructors and destructors? They are only functions which creates of free an instance of a structure. It's not automated in C, I agree, but it's doable. (And I have often seen C++ classes with Init and Clear methods, which just stole CTors/DTors roles. To be honest, I made a lot of that crap myself, in the past. I was a C programmer then, not accustomed to think in C++. Just different, not better.) They are not even mandatory in OOP: they are only used for RAII, which is not the way of "modern languages" like Java or C# (they does not have destructors, which is one the the reasons for which I do not like them. I love RAII, because I love efficient programs. Even if RAII have to be hand-crafted.)

Take the SDL, for example, with SDL_Surface structure.
You never use malloc and free to control the lifetime of those objects, and they have a bunch of functions made to manipulate them, which only does that. An object is exactly that: a structure with functions to manipulate it. In C, the only problem is that you have to explicitly give the instance of the object you want to manipulate, as a parameter. In Java, you can not overload operators, and that does not avoid doing OOP stuff, right?

You can name that problem a lack of type safety if you want, but it's not new that C does not make as many controls as C++ (which is, technically, incorrect, since there is, for example, reinterpret_cast... which should never be used except if you perfectly know what you are doing, because it's the equivalent of C-style cast.).

And there is no way you can do inheritance or polymorphism in C.

Here, you are wrong.
I won't explain myself, that link, with source code, will do it better than me: http://stackoverflow.com/questions/8194250/polymorphism-in-c I remember that I understood how C++ works only when I found a document where the author was, by it's own words, reinventing the wheel. He explained how to do each C++ feature in C. Sadly, I can not find it anew, I should have saved it at that time ( in 10 years or internet, documents vanishes, I guess).

What I meant is, OOP is not just a built-in feature of a language. It's a paradigm, a way of thinking. You can use JAVA which is a "full OOP" language (unlike C++, which explicitly allows to not use OOP technics) and not doing OOP, because you are doing things the wrong way (for OOP, I mean). When Linus said that Linux must stay in C to avoid C++ devs to contribute, I agree with him (and I really love C++, for sure). Because I noticed something: bad C programmers often go to C++, thinking it will be easier to do good job. They do dirty code, often with lot of copy/pasted code, (recently I have seen C++ code with memset/memcpy to copy classes's instances, sounds like a nice example, but looking better in the code it might not be one of the worse I have seen.). And they call that C++ code, because they use the class keyword, and sometimes few instances of std::vector and std::string. Note that I do not pretend to be a good C++ programmer, simply an average one. As in programming in general, in fact. But I can really say when I see dirty code (copy/paste of code is a really good measurement for that. By example.) Lot of people forgot that if, in C, you can shot in your foot easily, C++ is worse: in C++, you can then reuse the bullet for your other foot.

Let me quote wikipedia as a last argument (yes, authority argument is bad, I know ;) ):
============
An object oriented program may be viewed as a collection of interacting objects, as opposed to the conventional model, in which a program is seen as a list of tasks (subroutines) to perform. In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects. **Each object can be viewed as an independent "machine" with a distinct role or responsibility.** Actions (or "methods") on these objects are closely associated with the object. For example, OOP data structures tend to "carry their own operators around with them" (or at least "inherit" them from a similar object or class)—except when they must be serialized.

Simple, non-OOP programs may be one "long" list of commands. More complex programs often group smaller sections of these statements into functions or subroutines—each of which might perform a particular task. With designs of this sort, it is common for some of the program's data to be 'global', i.e., accessible from any part of the program. As programs grow in size, allowing any function to modify any piece of data means that bugs can have wide-reaching effects.
============

Here is the important part: "Each object can be viewed as an independent "machine" with a distinct role or responsibility.". Nothing forbid to do that in C. I took the example of the SDL, previously. Does not it really match to that phrase? When I use SDL in a C++ program, I create 1 class with a constructor which call SDL_CreateSurface, while the destructor calls SDL_FreeSurface. I simply use the automated RAII stuff, which is the feature C lacks. Well... at least, in C, you can use RAII, by hand. Not only with Java or C#, or at least, not as easily (those languages are designed to use a garbage collector...can be useful sometimes.).

About macros... I have still never met anyone I could name a good programmer, so maybe it's the reason for which I think they contribute to make code hard to maintain. Of course, I know about the uppercase names for defines, but that does not makes the code inside easy to read, nor it makes compiler's errors or program's bugs made by them easy to understand and fix. But, yes, as I said, they have to be used, sometimes. I myself use them sometimes, often to generate C++ code that I can not do with templates, or which is very verbose for nothing, like for a plugin mechanism.


Reply to: