:: Re: [DNG] C vs C++
Etusivu
Poista viesti
Vastaa
Lähettäjä: nick
Päiväys:  
Vastaanottaja: dng
Aihe: Re: [DNG] C vs C++


Didier makes some good remarks re C++. I wholly agree.



Yes, the languages have diverged. The other day I had to port a small test program from C to C++, as there was a radio driver written for Arduino that I wanted to evaluate before potentially translating it to C for my project. In my test program I was doing this:

struct request request = {

.channel = ...;

.command = ...;

...

};

And the C++ compiler kept rejecting it. I was quite puzzled until I remembered that C++ initializes structures in order, this is quite fundamental to the language to allow it to clean up if an exception is thrown during the initialization. So a different syntax is used. And the C99 (?) syntax can't just be bolted on, even though the committees both try to harmonize the languages as far as possible.



C++ has some nice features that C lacks, such as std::vector, std::map (and STL and templates generally), vtables (that is to say, polymorphism and virtual functions), namespaces. These are features that I often have to code manually in C. And I think a C with these features could be useful. So the concept is good in general. Yet after working with C++ for some time I'm not enthusiastic.



There is a fairly restricted use case for C++ which is when you need a very high level language due to the complexity of your project but you absolutely cannot tolerate any slowdown due to abstraction. This was the case in my research group as a postgrad as we were writing constraint solvers that were heavily mathematical but needed to search a combinatorial solution space which could take hours even for small problems. Google also uses C++ for similar niches such as the V8 javascript engine and possibly some of their products like search or maps which must serve billions of requests daily.



Apart from that, please avoid C++, and if you must use it, make class hierarchies very shallow and avoid overloading, templates, template base classes, virtual base classes, mixins and multiple inheritance. When these features are overused the result is a horrible spaghetti where the logic of your code is smashed into a million bits sprinkled through your source files in such a way that it is impossible to see what lines of code execute and in what order. Oh the frustration!!!



I worked with such codebases and it drove me mad. My uni recruited a well known researcher who had written a popular engine in C++, but after having worked with this engine I had grave reservations about the project. When he arrived and started work I could see he planned to recreate the same mess on our behalf. As politely as I could I begged him to reconsider his approach but he pooh poohed my concerns. A few years later his project had evolved into another horrible spaghetti and also had a lot of bugs. I attempted to do some code cleanup and submitted bug fixes but he refused to accept them saying "it's not a bug it's a feature!" ... sigh. Really avoid C++ please please please.



Kind regards, Nick








>
> On 2 Oct 2024 at 5:25 am, dng-request <dng-request@???> wrote:
>
>
> Send Dng mailing list submissions to
> dng@???
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng
> or, via email, send a message with subject or body 'help' to
> dng-request@???
>
> You can reach the person managing the list at
> dng-owner@???
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Dng digest..."
>
>
> Today's Topics:
>
> 1. Re: C vs ADA : advice sought (Johan Helsingius)
> 2. Re: C vs ADA : advice sought (Dan Purgert)
> 3. Re: C vs ADA : advice sought (kc-devuan@???)
> 4. Re: C vs ADA : advice sought (karl@???)
> 5. Re: C vs ADA : advice sought (kc-devuan@???)
> 6. Re: C vs ADA : advice sought (Didier Kryn)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Tue, 1 Oct 2024 18:01:31 +0200
> From: Johan Helsingius <julf@???>
> To: dng@???
> Subject: Re: [DNG] C vs ADA : advice sought
> Message-ID: <c674913c-7404-4056-a0d0-c07aa29e0960@???>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> Then there is the halfway house between C and ADA - Go. Also inspired
> by (besides C) Pascal, Oberon and Modula, but also Smalltalk and
> occam...
>
> Julf
>
> On 01/10/2024 16:41, Hendrik Boom wrote:
> > On Mon, Sep 30, 2024 at 08:18:03PM -0500, o1bigtenor via Dng wrote:
> >> Greetings
> >>
> >> The game thus far:
> >>
> >> 1. I asked why learn C vs python/micropython for embedded systems
> >> (microcontrollers + more)
> >> There was a huge outpouring of learn C. (Thanks to those that
> >> responded.)
> >>
> >> 2. Then I was asking for what to learn C from.
> >> Again there were quite a few responders (and I started digging into
> >> books on C) .
> >>
> >> 3. So I'm continuing my digging around and poking my nose into all kinds of
> >> exotic software
> >> corners then I find that it is possible to use ADA for programming
> >> microcontrollers and
> >> embedded systems and there is a great deal of built in 'safety' so I
> >> might be less likely to
> >> generate poor quality code.
> >>
> >> So the question:
> >>
> >> if I were to want to choose between learning C (and likely adding C++) and
> >> learning ADA for programming microcontrollers and embedded systems what - -
> >> - besides amount of usage would you use to advise me - - - which should I
> >> learn (and why please (this is at least as important as your choice!!!))?
> >
> > Once you know either of these languages the other will be easier to learn.
> > ADA is likely the better choice if you can choose the language to use.
> > It is easier to write correct programs in Ada than in C.
> >
> > But C is much more widespread. And if you end up working for other people,
> > you are more likely to have to work on existing code written in C
> > than in Ada. And so you're more likely to have to know C than Ada.
> >
> > C is harder to use than Ada; it is closer to assembly language, and has many
> > of the pitfalls of assembler.
> >
> > Why is C more widespread if it's worse for most applications than Ada?
> > Because it was there first.
> >
> > Why is Ada a better language? Because it was designed long after a lot of
> > experience with C and other early systems languages, and could benefit
> > from accumulated knowledge. I was in regular communication with some of
> > the designers back when they were putting Ada together. The primary
> > goal was to build a language that would help programmers achieve
> > reliable, efficient software.
> >
> > Which should you learn? I'd advise both. They have very different
> > philosophies. Ada will give you a better idea of what should be possible.
> >
> > Use Ada on your own projects; use what your boss requires when you're
> > working on his project.
> >
> > On the other hand, if you'd like to get the widest picture of what kinds
> > of programming methods are possible, and are not concerned with
> > immediate efficient application, I'd recommend a modern variant of Lisp,
> > such as Racket, just to open your vision to a wider horizon. It can be fun.
> > It's another way of thinking. But you'll likely not find a paid job where
> > you're expected to use it.
> >
> > -- hendrik
> > _______________________________________________
> > Dng mailing list
> > Dng@???
> > https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng
>
>
> ------------------------------
>
> Message: 2
> Date: Tue, 1 Oct 2024 12:13:55 -0400
> From: Dan Purgert <dan@???>
> To: dng@???
> Subject: Re: [DNG] C vs ADA : advice sought
> Message-ID: <ZvwfwwyaKCp2Cfik@???>
> Content-Type: text/plain; charset=us-ascii
>
> On Sep 30, 2024, o1bigtenor via Dng wrote:
> > Greetings
> >
> > if I were to want to choose between learning C (and likely adding C++) and
> > learning ADA for programming microcontrollers and embedded systems
> > what - - - besides amount of usage would you use to advise me - - -
> > which should I learn (and why please (this is at least as important as
> > your choice!!!))?
>
> At the end of the day, you need to be able to read and understand the
> Minimum Working Example ("MWE") code in the datasheets. And, well,
> that's going to be C.
>
> If you don't know enough programming in general to parse that MWE code,
> you're going to have a real hard time rewriting that into the
> "better(tm)" language.
>
> Once you can read the MWE in the datasheet, well, then by all means go
> off to something else if you want to.
>
>
>
> --
> |_|O|_|
> |_|_|O| Github: https://github.com/dpurgert
> |O|O|O| PGP: DDAB 23FB 19FA 7D85 1CC1 E067 6D65 70E5 4CE7 2860
>
>
> ------------------------------
>
> Message: 3
> Date: Tue, 1 Oct 2024 18:00:21 +0100 (GMT+01:00)
> From: kc-devuan@???
> To: dng@???
> Subject: Re: [DNG] C vs ADA : advice sought
> Message-ID: <0902689c-f27c-48b3-b58c-bb9a3bbda91c@???>
> Content-Type: text/plain; charset=UTF-8
>
> 1 Oct 2024 17:01:38 Johan Helsingius via Dng <dng@???>:
>
> > Then there is the halfway house between C and ADA - Go. Also inspired
> > by (besides C) Pascal, Oberon and Modula, but also Smalltalk and
> > occam...
>
> TinyGo for micro controllers. In fact, after a number of questions. It was a tinyGo author who finally gave me the impetus to test out Ada. I never expected to want to replace Go entirely with Ada on the desktop/servers too etc.. Though I was trying to use one language Go everywhere too by also using it on micro controllers instead of C. Neither Ada nor Go can quite deliver what Flutter offers though due to the sheer amount of widgets and platform support etc. despite preferring both languages to Dart.
>
>
> ------------------------------
>
> Message: 4
> Date: Tue, 1 Oct 2024 19:04:29 +0200 (CEST)
> From: karl@???
> To: dng@???
> Subject: Re: [DNG] C vs ADA : advice sought
> Message-ID: <20241001170429.5C54385C3A93@???>
> Content-Type: text/plain
>
> o1bigtenor:
> > The game thus far:
> >
> > 1. I asked why learn C vs python/micropython for embedded systems
> > (microcontrollers + more)
> > There was a huge outpouring of learn C. (Thanks to those that
> > responded.)
> ...
>
> Here is what a friend of mine did with micropython:
> https://blog.kitmatic.com/2017/06/28/electroporation-is-now-quick-high-yield-and-commodity-priced/
> https://github.com/kanzure/culture_shock
>
> If you don't want to invest much time into learning some language and
> if you already know python, I'd guess you'd do perfectly well with it.
> Just make sure you choose a supported mcu/board.
>
> If you want to invest more time and are interested in programming, I'd
> say you learn enough C and one other language. The reason to learn at
> least some C is that "everybody" is using it and a lot of languages have
> similar syntax. The reason to learn some other language is that it might
> suit you better. What other language, just pick one and see if it works
> for you, if not choose another.
>
> Regards,
> /Karl Hammar
>
>
>
>
> ------------------------------
>
> Message: 5
> Date: Tue, 1 Oct 2024 18:06:59 +0100 (GMT+01:00)
> From: kc-devuan@???
> To: dng@???
> Subject: Re: [DNG] C vs ADA : advice sought
> Message-ID: <a025d129-302f-4cf7-9d1b-ff97425bc1b1@???>
> Content-Type: text/plain; charset=UTF-8
>
> 1 Oct 2024 18:00:51 Kevin Chadwick via Dng <dng@???>:
>
> > Neither Ada nor Go can quite deliver what Flutter offers though due to the sheer amount of widgets and platform support etc. despite preferring both languages to Dart.
>
> Clarification: Well the languages can but I can't.
>
>
> ------------------------------
>
> Message: 6
> Date: Tue, 1 Oct 2024 21:24:39 +0200
> From: Didier Kryn <kryn@???>
> To: dng@???
> Subject: Re: [DNG] C vs ADA : advice sought
> Message-ID: <18a77263-690a-4ee1-add6-f23ca6401211@???>
> Content-Type: text/plain; charset="utf-8"; Format="flowed"
>
> Le 01/10/2024 ? 18:13, Dan Purgert via Dng a ?crit?:
> > On Sep 30, 2024, o1bigtenor via Dng wrote:
> >> Greetings
> >>
> >> if I were to want to choose between learning C (and likely adding C++) and
> >> learning ADA for programming microcontrollers and embedded systems
> >> what - - - besides amount of usage would you use to advise me - - -
> >> which should I learn (and why please (this is at least as important as
> >> your choice!!!))?
> > At the end of the day, you need to be able to read and understand the
> > Minimum Working Example ("MWE") code in the datasheets. And, well,
> > that's going to be C.
> >
> > If you don't know enough programming in general to parse that MWE code,
> > you're going to have a real hard time rewriting that into the
> > "better(tm)" language.
> >
> > Once you can read the MWE in the datasheet, well, then by all means go
> > off to something else if you want to.
> >
> ??? Fully agree. You will always need to know some C, even if you
> program in Ada. The kernel interface is generally mediated by a C
> library. Ada features a set of interfaces to other languages such as C,
> C++ and Fortran, which allow easy Ada bindings to some system-calls
> which you would need to invoke, such as IOCTL. Therefore, unless you are
> programming for bare metal, you will always need some familiarity with C.
>
> ??? However I warn you that C++ is not an easy transition from C. This
> idea seems natural but it is an illusion. First the two languages have
> diverged, meaning that C instructions compiled by the C++ compiler may
> produce different results, and vice-versa for C++ instructions which
> look like C. Second, C++ has a very steep learning curve and is a very
> difficult language. Third it keeps, by design, all the pitfalls of C.
> C++ was developped to implement in C all the OO features first
> experimented in Ada, partly because there was not yet a reliable Ada
> compiler. But I consider it was a bad idea from the beginning. I
> attended a one week lecture on C++ in the 90's; I was enthusiastic the
> first day, but got quickly the feeling it was a degradation of C.
>
> ??? I know my words will shock the C++ afficionados. I admire very much
> C++ programmers: they do a very difficult job. But I don't see any good
> reason to do like them. It is possible to do a better job, simply by
> using an easier, safer, and more expressive language.
>
> -- ??? Didier
>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <https://mailinglists.dyne.org/cgi-bin/mailman/private/dng/attachments/20241001/0ef22174/attachment.htm>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Dng mailing list
> Dng@???
> https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng
>
>
> ------------------------------
>
> End of Dng Digest, Vol 121, Issue 4
> ***********************************
>