News for hackers. Stuff that matters.
Re:Why I Write Games in C (yes, C) by jackjeff on Saturday September 21, 2019 @03:45PM (#21037466)
> C++ is as complex as you want to make it

I second that. I always find the attitude of “C++ is bad so I’m going to stick to C” really bizarre. You can use C++ as a better C.

- use type inference and references instead of pointers. Writing C style code with these features makes it more readable.

- Don’t like OO programming. Stick to struct with all members public. It’s going to be a lot better than doing the same thing in C and you shall not have void* casts.

- use exceptions instead of return code for errors, so that your code is not peppered with if statements at every line you call. With an exception you’ll get additional info about crashes in dumps.

I wrote for an embedded system where the C++ standard library was not available, in a previous life. I ended up writing my code in C++ and “re-inventing” a couple of useful classes like std::string and std::vector. For the most part my code was very C like...

Re:Why I Write Games in C (yes, C) by yellowapple on Saturday September 21, 2019 @05:50PM (#21038125)
> use exceptions instead of return code for errors

I happen to have the exact opposite opinion. Exception handling tends to feel too "magical" (read: non-deterministic, hard to behaviorally predict, etc.) relative to just returning an error code.

Re:Why I Write Games in C (yes, C) by oppositelock on Saturday September 21, 2019 @07:14PM (#21038499)
Exception handling doesn't work very well. It requires exception frames on the stack, and those go missing sometimes, leading to leaked exceptions and unexpected crashes. For example, if you pass a C++ callback function into a C callback, say in libcurl, the C library doesn't have exception handler frames in the stack, so exceptions are lost. If your C++ code throws from within that C callback, any catch above the C language boundary won't work. This is just one of countless gotchas. Don't use exceptions.
Re:Why I Write Games in C (yes, C) by ncmncm on Sunday September 22, 2019 @04:06AM (#21040233)
Don't pass functions to libcurl that throw.

Otherwise, exceptions make code simpler, cleaner, and more reliable. They will never "go missing" unless you do something to make them go missing. Code that does those things is bad code. Don't write bad code. Do use exceptions.

Re:Why I Write Games in C (yes, C) by oppositelock on Sunday September 22, 2019 @09:19AM (#21041228)
Well, of course, that's how you fix such a bug, however, when using third party libraries and other people's code, you don't necessarily know that this is happening until you have missing exceptions.

In my opinion, exceptions also complicate code by taking error handling out of the scope of the code you are writing, since you can't know whether anything you call throws, so you may not catch, and some higher level code will catch an exception, where it's lost context on how to handle it. If exceptions work well in a language; Python, Java, by all means use them, but in C++, they've caused too many problems for me to continue doing so. Even Google bans them in their C++ style guide.

Re:Why I Write Games in C (yes, C) by ncmncm on Sunday September 22, 2019 @11:24PM (#21045695)
If you don't know what functions you are passing to third-party libraries, you have a much bigger problem than any language can help you with.

Google's proscription on exceptions is purely historical. Once they had a lot of exception-unsafe code, it was too late to change. Now they spend 15-20% of their CPU cycles working around not being able to use RAII.

Re:Why I Write Games in C (yes, C) by oppositelock on Monday September 23, 2019 @12:21PM (#21050362)
When you write modern software, most of your executable ends up being third party code for commonly done things, it's both wonderful since you can move fast, and a curse, because you inherit others' bugs.

My example with libcurl was very simple, but in real systems, things are less clean. You may be using a network tool that hides libcurl from you, and your code is being passed into a callback without you even knowing it until it causes a problem. Other places where C++ exceptions fail would be when you dlopen() a module, or one of your binary C++ dependencies is compiled without exception handlers. The code will compile just fine, but exceptions will be lost, there's not so much as a linker warning.

Google uses RAII just fine, it's just you have to be careful with constructors/desctrucors since they can't return an error. There's no way it burns 15-20% CPU not using RAII - where are you getting these numbers? I used to work there, in C++ primarily, so I'm well familiar with their code base.

Re:Why I Write Games in C (yes, C) by ncmncm on Tuesday September 24, 2019 @12:59AM (#21056714)
In other words, you can't do RAII.

Instead you call a constructor that sets a dumb default state, and then another, "init()" function, that sets the state to something else, and returns a status result. But first it has to check if it is already init'd, and maybe deinit() first, if so, or maybe fail. Then you check the result of init(), and handle whatever that is, and return some failure. Otherwise, you do some work, and then call deinit(), which has to check if it is init'd, and then clean up.

I knew someone else who worked at Google. 15-20% was his estimate. Bjarne ran experiments and found overheads approaching that.

Re:Why I Write Games in C (yes, C) by yellowapple on Monday September 23, 2019 @02:00PM (#21051568)
> Don't pass functions to libcurl that throw.

Unless you know the details of how every function (and every function that function calls, and so on) handles errors, the easiest way to not pass functions to libcurl that throw is to not write those functions in a programming language with exceptions :)

Re:Why I Write Games in C (yes, C) by ncmncm on Saturday September 28, 2019 @07:41PM (#21103858)
That is easy only if you are writing nothing but callbacks. Every single other thing gets harder.
Re:Why I Write Games in C (yes, C) by jcelerier on Sunday September 22, 2019 @02:47AM (#21040022)
> Exception handling tends to feel

do you program a lot with your feelings ?

Re:Why I Write Games in C (yes, C) by yellowapple on Monday September 23, 2019 @02:24PM (#21051871)
Yep.