Sign in to follow this  
Mercury Filter

How Languages Compare?

Recommended Posts

Mercury Filter    354

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]

Share this post


Link to post
Share on other sites
Winfield    179

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." ;)

Share this post


Link to post
Share on other sites
ryt    294

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.

Share this post


Link to post
Share on other sites
Mercury Filter    354

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.

Share this post


Link to post
Share on other sites
Mercury Filter    354


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.

Share this post


Link to post
Share on other sites
Mercury Filter    354


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.

Share this post


Link to post
Share on other sites
ryt    294

 

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.

Share this post


Link to post
Share on other sites
Mercury Filter    354

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.

Edited by Mercury Filter

Share this post


Link to post
Share on other sites
Winfield    179


I usually see people insisting that you cannot use C

 

People are weird and say crazy, hyperbolic things. 

 


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.

 

Yeah. 

 

C's existed forever, and if you want a library that does something it's probably been completely stable since the 1980s. C is perfectly expressive, has tight and easily memorized syntax, and has extensive library support. Programming a game engine in C is made simpler by the plethora of libraries available: allegro, SDL, SFML, GLFW, the list goes on. 

 

There's a very good discussion here - see the second answer to the question in particular.

Share this post


Link to post
Share on other sites
rpiller    839

I think worrying about function parameters isn't really something to be worrying about. Good design use function parameters in both C/C++. You don't want to access your member variables directly as that defeats one of the points of C++'s encapsulation.

 

I used C for about 2 months on my way to C++. I'm not an expert but I've been working with it for many years now. I just don't like the idea of manually matching up stand alone functions to data. It just seems strange to not have a physical tie like what a class provides. It just seems a little hokey to me to rely on naming or docs to make that link between function and data.

Share this post


Link to post
Share on other sites
ryt    294

 

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.

 

 

Without classes you will end up with a function like GetPositionOfWalkableForOutherSystem(). Where with classes this could be broken into something like this:

 

pWalkable = OutherSystem.GetWalkable();

position = pWalkable->GetPosition();

 

I think that this example is much easier and more readable. Lets say that you have 150 functions as in first example, you will start very soon to struggle to find something you want.

As Agony sad C++ saves a huge time. If you use a lot of functions as in  first example they will start to be dependant of each other and you will not be able to find this dependency very quickly. With classes you just know where the things are as classes provide that for you.

Also Agony sad that learning C might make you better at C++. Its true, and if you know assem things will be a lot more clear to you.

Edited by ryt

Share this post


Link to post
Share on other sites
patrrr    1323

 

 

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.

 

 

Without classes you will end up with a function like GetPositionOfWalkableForOutherSystem(). Where with classes this could be broken into something like this:

 

pWalkable = OutherSystem.GetWalkable();

position = pWalkable->GetPosition();

 

I think that this example is much easier and more readable. Lets say that you have 150 functions as in first example, you will start very soon to struggle to find something you want.

As Agony sad C++ saves a huge time. If you use a lot of functions as in  first example they will start to be dependant of each other and you will not be able to find this dependency very quickly. With classes you just know where the things are as classes provide that for you.

Also Agony sad that learning C might make you better at C++. Its true, and if you know assem things will be a lot more clear to you.

 

This is false. Why would you do it that way, and not like this?

othersystem_getposition(pWalkable, &position);

When you start talking about systems that have 1000000+ lines of code, it doesn't really matter much whether you use classes or "not". Encapsulation is still very much possible in C. You could ask Linus Torvalds what he thinks on the subject.

Edited by patrrr

Share this post


Link to post
Share on other sites
ryt    294

Ok, but for me its a lot easier to have othersystem.GetWalkable(). It works trough IntelliSense and other plugins where with a single function you have to know exactly what you want.

Also if you want to write a variable this way you have to write it like othersystem_somevariable;. Now if you have a lot of things like othersystem it takes ages to write that.

Edited by ryt

Share this post


Link to post
Share on other sites
patrrr    1323

Ok, but for me its a lot easier to have othersystem.GetWalkable(). It works trough IntelliSense and other plugins where with a single function you have to know exactly what you want.

You can have the "othersystem" prefix in C as well. Works well with IntelliSense-like systems.

Edited by patrrr

Share this post


Link to post
Share on other sites
ryt    294

What about inheritance and polymorphism. With polymorphism you have to write a lot less code.

Edited by ryt

Share this post


Link to post
Share on other sites
patrrr    1323

Yeah, polymorphism is useful. But thing is, you can implement that in C as well, which is what the large projects I've worked at have done. Structs might contain function pointers for example, implementing a service.

Share this post


Link to post
Share on other sites
kop0113    2453
Nowadays the trend in game development is to use the component entity system which works really nicely with languages like C which don't actively enforce rigid object orientation.

At work we tend to write fairly typical C code using similar patterns to Gtk+ for our small amount of object orientation but then we attach components to a single type akin to Unity's GameObject.
We do however use the GNU C++ compiler so we can access some of the really useful C++ functionality like std::string, std::vector, smart pointers etc. We also use external C++ and C libraries so using a C++ compiler was the most compatible choice.

Also, it seems easier to interface with C code from a Java or Objective-C shim layer (for mobile devices), than C++.

My personal favorite feature of C over C++ is that if you define the struct in the .c file and just have a forward declaration of the struct and corresponding functions in the .h file, it is much more encapsulated than even a class with everything private in C++ since other developers can't even see the data and you have complete control over any aspect of the data being exposed. This can't seem to be done so well in C++ without rewrapping all the functions because the class contains the functions so has to be exposed. Thus the pimpl idiom which I find a tad messy and convoluted. Edited by Karsten_

Share this post


Link to post
Share on other sites
HScottH    520

I have only read the first couple replies.  But, here's a good answer.

 

At a high level, C++ supports better code design by providing an organizational paradigm that maps more to our world and the things we tend to simulate. C++ compilers also support a few features that C compilers don't.

 

But, the following are true:

* Most C++ code that I have seen (thousands of programs) were written as though they were written in C.  Classes were used for little more than namespace value.  You can spot programs like this as while they may have many classes, their class hierarchy is roughly flat.

 

* You can do about the same thing in C that you can in C++, with a little [mental, and code] effort.

 

On that second one, consider this:

* In C, you define a struct (Player) that is to represent info for a game player (name, position, facing). 

* All of your C functions that relate to the player have the form:

     void someFunc( Player* player, [other parameters])
     for e.g.:

     void movePlayer( Player* player, float x, float y, float z )

 

You have essentially done what C++ does; you have encapsulated player data into a discrete storage unit, and you have written methods that operate on this block of data.

 

In C++, the Player* parameter is hidden from your code, but available inside methods as "this".

 

Inheritance could be done as follows:

 

struct DeadPlayer {

     Player player;

     Point deathLocation;

}

Now, you write methods that take a DeadPlayer* as their first argument (and observe, you can pass DeadPlayer pointers to all methods that expect a Player).

 

So, the C++ language is really about data organization and coding style, not so much about underlying behavior.

 

Oh, and yes, there are a few more details than this, such as V-Table points and the like, but they aren't essential to understanding the difference.

 

One final note: while you can do just about anything in C you can in C++, there are a handful of advantages to C++.  With the example Player above, it might be possible to send a mal-formed or un-initialized Player to a method, whereas in C++, it is easier to isolate control over the classes data so that you can assure it's state is consistent and always good.  You could "hack" around these protections (via static casting), but in general, if you use the language as Stroustrup intended, it can really save a lot of headaches and prevent a lot of bugs.

Share this post


Link to post
Share on other sites
kop0113    2453

struct DeadPlayer {
     Player player;
     Point deathLocation;
}

Now, you write methods that take a DeadPlayer* as their first argument (and observe, you can pass DeadPlayer pointers to all methods that expect a Player).


Nice example.
Usually a fairly junior C# or Java developer will jeer at C because they don't realize or understand how this works. This is how Gtk+ (and many object-orientated C libraries) does it and demonstrates that C is pretty darn flexible. Edited by Karsten_

Share this post


Link to post
Share on other sites

Automatic enforcement of initialising stuff is good in C++ though (in C, you don't want to forget to call an init/deconstruct method).

 

But most things you can do in C++ you can do in C, although C's version of templates via macros is not good ;)

 

C makes you write a lot of boilerplate code and get it right which C++ shields you from.

Share this post


Link to post
Share on other sites
Almost any piece of high level C code can be improved by applying C++ techniques. References, generic algorithms, lambdas, constexpr, smart pointers, move semantics etc. are all really nice. You don't need to write object oriented code to benefit from C++.

Just the fact that C++ has a good assortment of standard containers is a huge leg up on C. If you are working in C, you'll be using containers from some 3rd party library, or have rolled your own from scratch with great time expenditure and possible quality issues, or you are effectively hamstrung by lack of appropriate tools.

Share this post


Link to post
Share on other sites
Mercury Filter    354


Almost any piece of high level C code can be improved by applying C++ techniques. References, generic algorithms, lambdas, constexpr, smart pointers, move semantics etc. are all really nice. You don't need to write object oriented code to benefit from C++.

I hate to nit-pick (ok, I actually love to, it seems very effective in terms of learning), but isn't it a bit misleading to refer to genetic algorithms and lambdas as being C++ techniques? Correct me if I misunderstood what you were saying but the statement seems to associate those techniques with C++, while I am pretty sure the two I mentioned existed before it did. I could not say about the others, as I am less familiar with them, and I am not very familiar with those two in the first place.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this