C vs. C++; efficiency vs. convenience?

You can talk about almost anything that you want to on this board.

Moderator: Moderators

User avatar
Punch
Posts: 365
Joined: Sat Feb 16, 2013 11:52 am

Re: C vs. C++; efficiency vs. convenience?

Post by Punch »

C++ has the potential to make code unreadable to everyone but the person who worked on it, misuse can make program maintenance almost impossible, etc. If there are programming guidelines being enforced to the whole dev team then it's OK, otherwise...
Bregalad wrote:<RETURN> key
you gave away your age :lol:
This is a block of text that can be added to posts you make. There is a 255 character limit.
Rahsennor
Posts: 479
Joined: Thu Aug 20, 2015 3:09 am

Re: C vs. C++; efficiency vs. convenience?

Post by Rahsennor »

I think C++ is a terrible language and refuse to use it under any circumstances. My reasoning is very simple: whenever I Google algorithms or other programming-related topics, none of the results using C++ are ever about the subject at hand. They're always discussions about about how to fit an existing square algorithm into C++'s round paradigm, fighting the compiler instead of solving the problem.

It's perfectly possible to write good programs in C++, and anyone who does so has my respect. I'm just not smart enough to use it without shooting myself in the foot, and I suspect a lot of other people who do use it aren't either. My brainpower is limited enough without wasting half of it trying to wrestle the language into submission. C isn't a good language by any stretch of the imagination, but it is vastly less complicated.

My personal favourite programming language is Forth. Discovering I could write a hex dump utility in three short lines changed how I code forever. I think everyone should try it at least once, even if they never use it again; less code to debug and maintain is a good thing in any language.
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: C vs. C++; efficiency vs. convenience?

Post by rainwarrior »

Here's a practical example of why I'd use C++ instead of C.

I'm working on a project for a long time, just doing things "C" style. Maybe there's an array of data that I've been using all over the code, thousands of times. At some point I get a bug, and think maybe I've got an out-of-bounds array access for that array? How do I cope with this situation? There's tools like valgrind that try to help with some types of bugs like that, but what if the interactive nature of games, or some other factor makes those tools impractical?

What I really want to do is add bounds checking to all access of that array, but as an afterthought. How do I do it, though? Try some massive regex operation on my whole codebase? Hope it can replace all instances of that array being accessed with some macro, without causing a hundred errors I have to correct by hand?


If it's C++ though I can use the features of classes, operator overloading, and return-by-reference, and use them to write a class that replaces that array without having to change any code except its definition. I just change the type in two places, and all the thousands of instances around the code will now use the bounds checking array class instead, which seamlessly behaves just like the original but with a run-time check. The whole operation will be inlined, and I could remove the bounds check in release mode to remove its runtime overhead too.


So of course a lot of C++ people would say "well you should have used std::array from the start" or even in C you might have said "you should have created inline functions to protect access to that array" or however you want to solve it with hindsight, or you might even say "no, you should review every single instance manually" if you don't believe in the relative value of the time involved, but my whole point is that features like this often make it possible to do stuff like this late without being able to see the future, without knowing the mistakes you're making right now.

There's a whole lot of features of C++ that are about leveraging old code to do new things. Not just the object oriented idea of allowing old code to call new code, but things like templates and operator overloading give you a lot of power to do similar things on bit of a different operating plane. I really can't think of any language that gives you as much power to change how the code "looks" as C++ does.

Yes, you can use those same features to make undecipherable nonsense-- this comes along with that power-- but you can't really have one without the other. It takes time and experience to learn what to use and when, but I've seen tons of "good" C++ code over the years, and I've seen tons of "bad" code in C++ and every other language I've used too. (Often the bad code was written by me... but trying the wrong way is an important way to gain experience.)
Rahsennor wrote:I think C++ is a terrible language and refuse to use it under any circumstances.
I find it interesing that you feel that strongly about it. I don't think I'd refuse to use any programming language. I like C++ for a lot of purposes (I use C++ and Python the most), but if I'm joining a project I'll gladly use whatever they've already got going. If it's a paying gig, I probably wouldn't have a choice of language except to turn down the job. (If it's a personal project, then you can use what you like, but I guess the OP has asked us to compare C and C++...)
Rahsennor wrote:My personal favourite programming language is Forth. ... I think everyone should try it at least once, even if they never use it again; less code to debug and maintain is a good thing in any language.
I have yet to try Forth, but once you have enough experience in a first language, I think there's a lot of value in trying many programming languages, especially ones that use different paradigms from the ones you know well. (I don't think you need to catch 'em all, but there's languages like Haskell which are a fundamentally different experience than C.)
User avatar
Bregalad
Posts: 8055
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Re: C vs. C++; efficiency vs. convenience?

Post by Bregalad »

Rahsennor wrote: My personal favourite programming language is Forth. Discovering I could write a hex dump utility in three short lines changed how I code forever. I think everyone should try it at least once, even if they never use it again; less code to debug and maintain is a good thing in any language.
I tried getting into Forth several times and always failed. The problem is that it seems all of the 4-5 different FORTH books I had seemed to suck really hard, but if I'll ever get my hands on Starting Forth and Thinking Forth, I'll try again. Yet, it seems very nonstandard and every implantation seems to differ. Classic forths used 16-bit by default and double words would be 32-bit but GFORTH seems to use data sizes twice as big. Concepts like code pages are included in the language but I don't know even what it is but it seems extremely obsolete and not needed for modern development (not even cross-development targeting a small platform).

Forth is nice for doing simple things such as calculations or simple loops. However I have a hard time seeing me use this for any kind of real development. I have no idea how I could possibly use this for, let's say, read a NES controller, or compress a WAV file, or something like that.
Garth
Posts: 246
Joined: Wed Nov 30, 2016 4:45 pm
Location: Southern California
Contact:

Re: C vs. C++; efficiency vs. convenience?

Post by Garth »

Definitely get the book, "Starting Forth" (or, easier to get but not as good as an actual book IMO is to see it online, here). When I initially met Forth in about 1989, I liked it, but I thought there were things it would not be able to do gracefully, or at all; but as I gained experience, I found that it not only can do them all, but with unimagined elegance.

At Samuel Falvo's recommendation, I'm going through the K&R book on C. I'm about half way through, and I can see I will never program in C. My biggest problem with it all is just that there are too many rules, and I don't always understand the examples. I have to keep referring back to other parts of the book to look up something I'm just supposed to remember but I don't remember because there's no pattern to it (which is why I have a bazillion pencil notes in the book), and that sometimes I just can't understand what it's doing at all. (I never had this problem when learning Forth.)

The C thing is causing me to be more of the opinion that some people really are destined to think more one way than another, like it's inborn, not just a matter of background. I had TI algebraic calculators before an HP RPN one, and I did a lot of BASIC also, and even took Fortran in school, before meeting Forth, yet the way I think definitely tends toward RPN and Forth. Others have some trouble with it. With those who use the calculators, they often want the stack displayed so they can see what's there. I don't need that at all, and feel that for me it would be pointless.

Following the trend line, I expect my long list of beefs with C will get even a lot longer before I'm done with the book, and I'm not sure a second read will be enough to make me successful at all.
http://WilsonMinesCo.com/ lots of 6502 resources
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: C vs. C++; efficiency vs. convenience?

Post by tepples »

If a language you speak where at the end the verb comes, more natural Forth and PostScript will seem. Desu.

(I get it, Yoda. Let's jump franchises for a moment.)

This is why var'aq, a programming language inspired by the Klingon language created for Star Trek, is stack-based. The object-verb-subject order of Klingon, with implicit subject possible, makes the PostScript-like stack-base structure of var'aq feel more familiar.
User avatar
Drew Sebastino
Formerly Espozo
Posts: 3496
Joined: Mon Sep 15, 2014 4:35 pm
Location: Richmond, Virginia

Re: C vs. C++; efficiency vs. convenience?

Post by Drew Sebastino »

tepples wrote:Desu.
Weeb.
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: C vs. C++; efficiency vs. convenience?

Post by tepples »

Espozo wrote:
tepples wrote:Desu.
Weeb.
les wobble but they don't fall down.

Image

I mentioned "desu" because Japanese is another verb-final language familiar to certain fandoms. So is Hindi, but it has too much cultural baggage with the recent Windows technical support scams.

Likewise: Welsh, Irish, Hawaiian, Tagalog, and some Arabic dialects put the verb at the front. Their speakers might find Lisp-family languages more approachable.
adam_smasher
Posts: 271
Joined: Sun Mar 27, 2011 10:49 am
Location: Victoria, BC

Re: C vs. C++; efficiency vs. convenience?

Post by adam_smasher »

...English (and many other European languages) also put the verb first in the imperative, which probably explains why the BASIC command 'PRINT "Hello World!"' effectively reads as an English sentence, and also why C, LISP, Pascal, Algol, FORTRAN, Haskell, SML, etc. all put the verb first too.

Java/C++/C#/etc object oriented method invocation syntax is kind of like indicating the topic before an imperative: 'Console.print("Hello World")' => "Console, print Hello". This is probably most evident when using method chaining.
Rahsennor
Posts: 479
Joined: Thu Aug 20, 2015 3:09 am

Re: C vs. C++; efficiency vs. convenience?

Post by Rahsennor »

rainwarrior wrote:Here's a practical example of why I'd use C++ instead of C.
I've never had to handle projects that large, and I'm pretty sure I flat-out can't. I can't reason about arrays referenced as often as you describe. I golf my code constantly to keep complexity down; it's the only way I can get anything done. I imagine most programmers would consider it premature optimization and a gigantic pain in the ass, but it's just how I work. I did say I liked Forth.
rainwarrior wrote:I like C++ for a lot of purposes (I use C++ and Python the most), but if I'm joining a project I'll gladly use whatever they've already got going. If it's a paying gig, I probably wouldn't have a choice of language except to turn down the job. (If it's a personal project, then you can use what you like, but I guess the OP has asked us to compare C and C++...)
I don't think I'm cut out for commercial work in the first place. I can't wrap my head around large codebases and my obnoxious coding habits would preclude me working on a team. Most of the stuff I've written has never left my computer; there's no audience for silly stuff like movie players with realtime MC-FRUC. Nobody even uses my NSF player. And I'm cool with that; I work on stuff because it interests me, not because I'm out for fame and fortune.
rainwarrior wrote:I have yet to try Forth, but once you have enough experience in a first language, I think there's a lot of value in trying many programming languages, especially ones that use different paradigms from the ones you know well. (I don't think you need to catch 'em all, but there's languages like Haskell which are a fundamentally different experience than C.)
Definitely. APL is another eye-opener, if you can stomach the character set.
Bregalad wrote:Yet, it seems very nonstandard and every implantation seems to differ.
There are as many dialects of Forth as there are Forth programmers. Writing your own compiler is pretty much a rite of passage, and the language's entire shtick is domain-specific customization. Portability is unheard of.
Bregalad wrote:Concepts like code pages are included in the language but I don't know even what it is but it seems extremely obsolete and not needed for modern development (not even cross-development targeting a small platform).
It's a filesystem, basically, and I agree that it's mostly obsolete.
Bregalad wrote:Forth is nice for doing simple things such as calculations or simple loops. However I have a hard time seeing me use this for any kind of real development. I have no idea how I could possibly use this for, let's say, read a NES controller, or compress a WAV file, or something like that.
That's the fun bit: what is a program but a collection of calculations and simple loops? Forth isn't about writing complex programs. It's about making complex programs simple.
User avatar
Punch
Posts: 365
Joined: Sat Feb 16, 2013 11:52 am

Re: C vs. C++; efficiency vs. convenience?

Post by Punch »

Rahsennor wrote: I don't think I'm cut out for commercial work in the first place. I can't wrap my head around large codebases and my obnoxious coding habits would preclude me working on a team.

There are as many dialects of Forth as there are Forth programmers. Writing your own compiler is pretty much a rite of passage, and the language's entire shtick is domain-specific customization. Portability is unheard of.

Forth isn't about writing complex programs. It's about making complex programs simple.
Ok, maybe it's just me and I'm not fit for Forth but it seems that, actually, it's about solving problems in a "clever" way in detriment of ease of use (the usage of reverse polish notation illustrates that notion pretty well). Doesn't seem as human-readable as C, for example.
This is a block of text that can be added to posts you make. There is a 255 character limit.
User avatar
Bregalad
Posts: 8055
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Re: C vs. C++; efficiency vs. convenience?

Post by Bregalad »

Punch wrote:
Ok, maybe it's just me and I'm not fit for Forth but it seems that, actually, it's about solving problems in a "clever" way in detriment of ease of use (the usage of reverse polish notation illustrates that notion pretty well). Doesn't seem as human-readable as C, for example.
And C isn't very human readable compared to Pascal for instance.
Rahsennor
Posts: 479
Joined: Thu Aug 20, 2015 3:09 am

Re: C vs. C++; efficiency vs. convenience?

Post by Rahsennor »

Punch wrote:Ok, maybe it's just me and I'm not fit for Forth but it seems that, actually, it's about solving problems in a "clever" way in detriment of ease of use (the usage of reverse polish notation illustrates that notion pretty well). Doesn't seem as human-readable as C, for example.
RPN isn't the most readable syntax for English-speaking programmers, no. It does make execution/compilation trivially easy, and completely removes the need to name variables, which makes the code even more compact. YMMV on whether terseness is good or bad, of course.

As for being "clever", well, I can't really define what it was about Forth that blew my mind without sounding like an ass. It's less about the syntax and more about the form of problem-solving it encourages; you can write Forth-style code in C, just like you can write C-style code in Forth. The best example I can come up with is the stack juggling everyone complains about: a Forth programmer simply reorders the code until the stack juggling is no longer necessary.

The same principle of rewording the question until the answer becomes trivial works for a very large class of problems. It's hardly unique to Forth, but by requiring it in the basic syntax, Forth forces you to think about it, and practice it until you're good at it. Once you've got the hang of it the syntax becomes a nonissue.
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: C vs. C++; efficiency vs. convenience?

Post by tepples »

Rahsennor wrote:It's less about the syntax and more about the form of problem-solving it encourages; you can write Forth-style code in C
Could you show an example of C-style C and the corresponding Forth-style C?
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: C vs. C++; efficiency vs. convenience?

Post by rainwarrior »

Rahsennor wrote:The best example I can come up with is the stack juggling everyone complains about: a Forth programmer simply reorders the code until the stack juggling is no longer necessary.
I thought "stack juggling" was an assembly language concern, not a C concern?

C abstracts away and hides the use of the stack (you create local variables, and the compiler decides where/how to store them), and the compiler sort of converts expressions to RPN automatically as part of the compilation process (or more realistically probably some symbolic computational-tree form that it can optimize well, but RPN conversion is often the "simple" example of an expression evaluation implementation).

You're making it sound like Forth transfers C's expression evaluation from the compiler to the programmer, though I'm not sure why this is viewed as an advantage. Functions in C are basically backwards-RPN with some (,) decorations, just it also happens to use infix operators for arithmetic by default. (Alternatively, Lisp does away with infix operators and makes all the arithmetic operators look just like other functions.)
Post Reply