How Languages Compare?

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


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.

Advertisement

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.

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.

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.

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.

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.

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

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.

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.
http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

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

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.

This topic is closed to new replies.

Advertisement