Archived

This topic is now archived and is closed to further replies.

Theory of C programming (as opposed to object-oriented)

This topic is 5581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

A lot of C++ programmers choose to maintain that sort of style that was shoved into their heads as wee lil'' C++ coders back when they were small... We all know this one: Try to made everything modular. Construct everything into a class, and common attributes would root into a single class, and unique objects would be derrived from those classes." When I look at C++ source code, no matter who wrote it, it is generally clean, modular, classed out, and nice and structured... When I look at C code (Look at Quake 2 for a good example), everything is stretched into multiple files, there are functions everywhere, and generally it''s hard to find anything (for me). Now my question is why do the two have different styles? And why, oh why, do those C people stay strict about how they code and the way they do it (with all those files)? Just thinking out loud really; Hope for at least a little feedback.. Thanks! ~ Jesse The gl2D Project | Free programming books

Share this post


Link to post
Share on other sites
Why do some people like Apple juice, and some people don''t? Why do some people listen to country music, while others listen to rap?

It''s a matter of personal preference. Plain and simple.

Share this post


Link to post
Share on other sites
Hmm. I would actually posit that a lot of C++ programmers that learned C first have not shed their C ways, resulting in suboptimal code. That is becoming rarer and rarer because there are now more programmers for whom their first langage was C++ or another object-oriented language.

The cardinal philosophy in C, to my mind, is modularity, albeit in terms of processes that take varied data rather than the data-centric habits of the object-oriented crowd. In C++, you write object definitions and operations on those objects; in C you write functions and supply parameters for those functions. Generally speaking, that is.

Keep in mind, however, that it is very possible to write OO code in C. I''ve done it, and sometimes write C-style OO code in C++ because it suits the situation better.

Share this post


Link to post
Share on other sites
I''ve started with C. After two years i''ve started C++ well. Every new feature in C++ is simply what i need. but yet the c-style coding is my favorit to write. but its not my favorit to read ... IMO you have to understand the programmer before digging in his code in C. but in C++ its a lot easier to understand the code. scense its style is well structured.
quote:
Originaly posted by DWarf with Axe
Now my question is why do the two have different styles? And why, oh why, do those C people stay strict about how they code and the way they do it (with all those files)?


I strict to the C-style code because I feel there is no need for the extra C++ magic when solving small problems. But yes i agree stricting to C when making a special system handler such as a UI system would be hard to manage. and way too comlicated for other programmers to understand.
About these files. yes its a problem, any C++ class can be with other classes in the same file no problems at all. but speaking C if you implement a lot of "C-Classes/projects" in one same file it will be hard to imagin the complexity of this code.

quote:
Orignaly posted by Oluseyi
Keep in mind, however, that it is very possible to write OO code in C. I''ve done it, and sometimes write C-style OO code in C++ because it suits the situation better.


Though i bet that only few programmers can understand it and debug it. and they would be C programmers for sure. Its not a good idea to write such insane code

I think the goal of C++ style is to make an obsolete programming style.

Just a thought...

Share this post


Link to post
Share on other sites
I think you point out a key difference between the languages. It could be said that C++ "forces" good coding habits on the coder, while C doesn''t. I hedge in saying that, because one can write sloppy code in C++ too. I''m also tempted to say that Quake source isn''t clean from a C perspective either, but then I''ve looked at some of the gnu sources too and found them messy as well.

As for the many files in C - the rule of thumb is to group together similiar functions and functions that operate on the same pieces of data. I think the Quake sources (Q1 and Q2) reflect that. I don''t think "strict" is the right word to describe it though, as C isn''t as strict a language as C++ - hence the complaints from C++ coders about the lack of type safety and the like in C.

Share this post


Link to post
Share on other sites
quote:
Original post by LessBread
I think you point out a key difference between the languages. It could be said that C++ "forces" good coding habits on the coder, while C doesn''t. I hedge in saying that, because one can write sloppy code in C++ too. I''m also tempted to say that Quake source isn''t clean from a C perspective either, but then I''ve looked at some of the gnu sources too and found them messy as well.

As for the many files in C - the rule of thumb is to group together similiar functions and functions that operate on the same pieces of data. I think the Quake sources (Q1 and Q2) reflect that. I don''t think "strict" is the right word to describe it though, as C isn''t as strict a language as C++ - hence the complaints from C++ coders about the lack of type safety and the like in C.


It''s not that C++ forces anything at all. It does, however support extra semantic constructs that allow you to force yourself to write modular code. (exceptions, protection levels, and references come to mind)

Share this post


Link to post
Share on other sites
Const correctness & stricter type-checking are the two big additions that let you write better code.

It''s not entirely a matter of personal preference - the problems that OO programming address crop in when coding in C. In C have you to manually maintain the code to handle those cases, and use ad-hoc conventions (aka gentlemen''s agreements) to solve them (e.g. bsd sockets). In C++ there''s built-in language features to address them. The flipside is that it''s now a lot easier to use OO when it''s unneeded (resulting in unneccassary complexity). e.g Writing a fully functional singleton is hard and generally unneccassay. (Though now that Loki offers a canned implementation, it''s not so hard or complex.)

I''ve always thought that the Quake source was rather clear - it''s good modular C code.

You''ll have just as many (if not more) C++ source files. You''d at least write one class for each module (or perhaps declare a namespace for each module, with many classes).

Share this post


Link to post
Share on other sites