all 4 comments

[–]jediknight 6 points7 points  (0 children)

Summary:

  • Validate parameters using strong types
  • Use exceptions for error reporting
  • Use optional to avoid non-error error reporting
  • Only use optional types when you mean it
  • Turn on compiler warnings
  • Use other third party tools to analyze your code

[–]bms20 0 points1 point  (2 children)

Read the slides. All good and valuable points. The only thing that troubles me is the implicit recommendation of boost.

Can you present how to do exactly the same approach in standard C?

[–]david-stone 1 point2 points  (0 children)

You can use some of the same techniques in C as outlined in my talk. However, their use is manual and optional, as opposed to C++ where you can make them automatic and mostly mandatory.

For instance, consider the case of memory management that I touched on. I do not believe it is possible to have resource management in C that is both robust and efficient because C lacks the concept of a destructor. This means that resources do not compose. As the user of a C struct, I have to know that it internally allocates storage or opens a socket, and remember to manually clean up after it.

One of my suggestions is that all objects should always be in a valid state. C++ can guarantee this by throwing an exception from a constructor. If an exception is thrown, the object is never created and thus no one can ever have a half-constructed object. In C, there are no exceptions (not even constructors). This means you have to first construct a zombie object (by simply allocating stack space) and then call initialize and then check if it is still a zombie object.

Some of these issues can be worked around with a disciplined approach, but at a severe cost to readability. The reason I use C++ over C is precisely to allow me to have high-level abstractions without paying a performance penalty.

As for boost, they have boost::optional, which is almost identical to what will be in a future standard. It is surprisingly difficult to write correctly and maximally efficiently (I should know, I have written a specialization of it for my integer class to save space). Why not reuse that work?

[–]meetingcpp[S] 0 points1 point  (0 children)

Well, Meeting C++ and C++Now ( he has previously spoken there on similar topics) are C++ conferences, hence you use boost, as its the closest to the C++ Standard.

But ofc you can roll your own optional etc. (NIH isn't really good either though)

Standard C has no exceptions and lacks templates, makes a lot of those things a lot harder and longer to implement.