How Languages Compare?

Started by
36 comments, last by ApochPiQ 10 years, 4 months ago

I have been wondering about how the practice of coding in C and C++ compare, in terms of a high-level view since I doubt at this point I would really benefit from a lot of the technical detail. I am not asking this for the purpose of "choosing a language", or anything like that. I just felt curious about opinions on benefits and drawbacks if comparing the two.

Most of what I have done to this point has been in either C, C++, or Java, with dabbling in various other areas. (Haskell and Prolog were pretty fun I thought, but I never got very far with them...no reason really, they were a sidetrack themselves, or just for fun)

I became familiar with C mostly from a course in computational physics that I took. I had used it prior, but most of my (occasional) effort had been directed at C++ up to that point. The thing that that use of C highlighted to me was that it seemed like C was a great deal more...hmm, not sure how to put this. Direct, maybe? When comparing the two, it feels quicker to write stuff in C. I would add that I have yet to write anything sizable. Even the final project for the course was not very big in terms of code. I am aware of statements regarding C++ code being organized in ways that makes it convienient for large projects, but I am not really familiar with the specifics of why that is. (Okay, maybe I could come up with an acceptable answer to that, but I would not be very confident in it, having not seen such things for myself.)

In writing some C++ code today, I saw that there were member functions of classes that could, due to the nature of classes, be written in a way that no arguments were needed to the function. I realized that if I were to write the same thing in C, there would be arguments required. I admit that I wrote some code in the above mentioned course that had some painfully large amounts of arguments. The flip side to the nice (at least I felt) no arguments benefit was that I had to write the class were in C I would have written two funtions alone. Perhaps this is a concept with importance of either greater, or lesser magnitude than I realize. I don't know how this type of thing (included as an example to hopefully clarify the nature of my question) would scale when you are talking about major projects, as opposed to something small, which is pretty much what I have written to date.

Or maybe I am horribly misunderstanding all kinds of things, lol. ["lol" is included in my typing language as a provisional inclusion, and I am not sure if it will make the cut for the standard]

Advertisement

First off, great job either starting a holy war or getting your thread locked. I'm pretty sure that's what happens when this question gets asked, because it gets asked a lot. ;)

But since neither of those things has happened yet, here's my cent and a half (adjusted for inflation):

In writing some C++ code today, I saw that there were member functions of classes that could, due to the nature of classes, be written in a way that no arguments were needed to the function.

This is true, and classes are pretty great. I'm developing almost exclusively in C nowadays, and while the arguments needed are sometimes lengthy, it isn't a dealbreaker for me. The biggie in C++ is encapsulation - that an object's methods and properties have restricted access from outside - which, in theory, means that the language reinforces correct design. This strikes me as weird, because having the discipline to manage memory but not to make sensible modularity and good separation of concerns seems a lot like trusting someone to do brain surgery but not to own an electric kettle.

In my C codebase, I have logic split up into eight separate domain-specific (that's problem domain, not web!) files, with prefixed function names. For the most part it's all coordinated by the central logic in game.c, and there are only three functions that call functions between domains (specifically, it's for the entities system to ask the sprite system for sprites and the script system for scripts.) It's very much like the OO version of the code would be, because I'm disciplined and I want to be able to maintain my code.

lol. ["lol" is included in my typing language as a provisional inclusion, and I am not sure if it will make the cut for the standard]

I see your "lol" and raise you a "trololol." ;)

C and C++ are really very similar to each other but with one big difference, classes. Classes are like a big new introduction to C and C++ is by miles better just because of this. I really don't know if there are any more big projects written in C since it seams pretty hard to do it without classes.

Classes just make it easier for bigger project because you just get lost in all that functions and you start to write things that you already have. With classes its much more organized and you know where each member belongs.

C allows functions without arguments. This is possible in C and C++, and I think all languages support that.

I apologize if I have asked something that can be the cause of a legendary holy war. That was certainly not my intent.

Perhaps I was not clear. I was not asking for whether one or the other is better. I wanted to know (if such a response is even possible with reasonable effort) if anyone had any high-level explanations regarding benefits in writing code in one or the other. Not really a better thing, but more specifically if there are certain things that are a bit easier in one language versus the other.

My question was meant to try and obtain objective responses, but I did use the dirty word "opinions" in my original post. Probably bad form when seeking an objective response. Perhaps the nature of my question can not easily be answered objectively. I mainly just want to try and determine if there are things here and there that may be easier in one language versus the other.

I play with both of them (which occasionally leads to me trying to do riduculous things in one because I forget I am not using the other), and just wanted to know a bit more about usage of the languages in general.


C allows functions without arguments. This is possible in C and C++, and I think all languages support that.

I am sorry if I did not make it more clear what I was saying. I am aware that C allows functions without arguments, I was trying to say that I was doing something with C++ where I would have needed arguments in C, or (if I understand correctly) I would have needed to be using global variables in C.

Yea, you are right. In C there wound have been needed more arguments since a class manages all that.


I'm developing almost exclusively in C nowadays

This is interesting for me to see. I am sure that there are people developing in all kinds of languages that I would not expect, but I usually see people insisting that you cannot use C, and you need to use C++. I would assume that it is a "toolbox" issue, and nothing is actually quantifiably superior in all ways, with the selection being a matter of choosing which benefits are most important. I am currently kind of a fan of C, but that is probably due to familiarity, since I have spent the most time using it at this point.

Thank you, by the way, for warning me about asking this kind of question. I will either be a lot more careful with how I approach such matters, or I will avoid asking potentially inflamatory questions in the future.

This is interesting for me to see. I am sure that there are people developing in all kinds of languages that I would not expect, but I usually see people insisting that you cannot use C, and you need to use C++. I would assume that it is a "toolbox" issue, and nothing is actually quantifiably superior in all ways, with the selection being a matter of choosing which benefits are most important. I am currently kind of a fan of C, but that is probably due to familiarity, since I have spent the most time using it at this point.

Thank you, by the way, for warning me about asking this kind of question. I will either be a lot more careful with how I approach such matters, or I will avoid asking potentially inflamatory questions in the future.

I don't want to tell you to stop using C, but when your code starts to have 1 000 000 lines I think you will have a hard time to manage around.


I don't want to tell you to stop using C, but when your code starts to have 1 000 000 lines I think you will have a hard time to manage around.

I understand what you are saying there (not really, as I lack the experience of writing large programs to claim understanding). I imagine managing any large programming project is not trivial, but I am curious about what it is that makes managing the larger programs in C++ easier? I have heard the quick answer of "classes", and perhaps this is another question that I need to experience as opposed to hear an answer to, as the quick answer does not really feel like it enhances my understanding (I often take this as a sign that I am missing some fundamental understanding that would make the quick answer more complete, if I had the knowledge). I assume that what Winfield said above is a Edit::workable good::Edit option, so I guess I am wondering if there is something else that I am not familiar with in C++ that makes it easier to manage code for bigger projects, or if perhaps I need to write more C++ before I will really see the benefits?

Sorry if some of these questions are kind of silly (perhaps). I presume the wonderous shield of 'newb', which should be obvious from said questions, should afford me some pity, assuming that I manage to improve....eventually.

Edit::I felt like "workable" may have been communicating a negative or perhaps diminished appraisal of the option that I did not intend.

In general, C and C++ both support exactly the same high level conceptual patterns of software architecture (OOP, functional, event driven, entity-component, message passing, whatever floats your boat). They also allow the same low level access to the nitty gritty implementation details. But the key difference to me is that C++ supports multiple powerful ways for the developer to intelligently delegate a lot of the tedious low level work to the compiler, without need to fear that the compiler is going to handle it poorly. (And this has gotten way better with C++11 than it already had been.) This enables the developer to spend a larger portion of time thinking about code on a high level, and less time being concerned with the low level details, which I think is a win.

(Keep in mind, however, that for this benefit to really be properly used, the developer should still understand what the compiler is doing, and be aware of the low level implementation details when they are likely to matter. Basically, the developer can't be lazy or sloppy just because the compiler will make it all right. Being able to program high quality C code will probably make for a better C++ coder.)

The delegation to the compiler means that things can be automated also, rather than relying on the developer to always do the right thing in all the right places. Deterministic destruction is huge in this regard, and well written constructors also. Violations of invariants are more likely to be caught at compile time, reducing the cost of programming mistakes.

So given that the same high level architecture is being implemented in either C or C++, and given that the developer is properly skilled and experienced with good design and programming practices, I would say that C++ tends to save time. However, if you have a tendency to apply strong architecture to C++ projects, while using a much looser architecture for C projects, then it might feel like C saves time. But you're not really comparing apples to apples at that point. After all, if you really wanted to, you could use a loose architecture with C++ too, and you'd still probably find miscellaneous conveniences where C++ delegates tedious work to the compiler and saves you time.

"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

This topic is closed to new replies.

Advertisement