Jump to content

  • Log In with Google      Sign In   
  • Create Account


How Languages Compare?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
37 replies to this topic

#1 Mercury Filter   Members   -  Reputation: 273

Like
3Likes
Like

Posted 14 November 2013 - 10:46 AM

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]



Sponsor:

#2 Winfield   Members   -  Reputation: 179

Like
2Likes
Like

Posted 14 November 2013 - 11:34 AM

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



#3 ryt   Members   -  Reputation: 294

Like
-1Likes
Like

Posted 14 November 2013 - 11:40 AM

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.



#4 Mercury Filter   Members   -  Reputation: 273

Like
1Likes
Like

Posted 14 November 2013 - 11:58 AM

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.



#5 Mercury Filter   Members   -  Reputation: 273

Like
1Likes
Like

Posted 14 November 2013 - 12:02 PM


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.



#6 ryt   Members   -  Reputation: 294

Like
1Likes
Like

Posted 14 November 2013 - 12:16 PM

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



#7 Mercury Filter   Members   -  Reputation: 273

Like
1Likes
Like

Posted 14 November 2013 - 12:20 PM


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.



#8 ryt   Members   -  Reputation: 294

Like
-2Likes
Like

Posted 14 November 2013 - 12:29 PM

 

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.



#9 Mercury Filter   Members   -  Reputation: 273

Like
1Likes
Like

Posted 14 November 2013 - 12:48 PM


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, 14 November 2013 - 12:54 PM.


#10 Andy Gainey   Members   -  Reputation: 1759

Like
6Likes
Like

Posted 14 November 2013 - 01:09 PM

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

#11 Winfield   Members   -  Reputation: 179

Like
1Likes
Like

Posted 14 November 2013 - 01:59 PM


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.



#12 rpiller   Members   -  Reputation: 651

Like
0Likes
Like

Posted 14 November 2013 - 02:15 PM

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.



#13 ryt   Members   -  Reputation: 294

Like
1Likes
Like

Posted 14 November 2013 - 02:37 PM

 

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, 14 November 2013 - 02:39 PM.


#14 patrrr   Members   -  Reputation: 931

Like
2Likes
Like

Posted 14 November 2013 - 02:49 PM

 

 

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, 14 November 2013 - 02:59 PM.


#15 ryt   Members   -  Reputation: 294

Like
0Likes
Like

Posted 14 November 2013 - 02:59 PM

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, 14 November 2013 - 03:04 PM.


#16 patrrr   Members   -  Reputation: 931

Like
0Likes
Like

Posted 14 November 2013 - 03:01 PM

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, 14 November 2013 - 03:02 PM.


#17 ryt   Members   -  Reputation: 294

Like
0Likes
Like

Posted 14 November 2013 - 03:06 PM

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


Edited by ryt, 14 November 2013 - 03:14 PM.


#18 patrrr   Members   -  Reputation: 931

Like
2Likes
Like

Posted 14 November 2013 - 03:16 PM

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.



#19 Karsten_   Members   -  Reputation: 1365

Like
1Likes
Like

Posted 14 November 2013 - 05:56 PM

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_, 14 November 2013 - 06:08 PM.

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.


#20 HScottH   Members   -  Reputation: 432

Like
3Likes
Like

Posted 14 November 2013 - 06:13 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS