4 ms·
> I'm not saying it's impossible to write a bad printf line and never test it, only to have it fail years later in production. It's absolutely possible and it h
by tialaramex 3y ago
> I'm not saying it's impossible to write a bad printf line and never test it, only to have it fail years later in production. It's absolutely possible and it has happened to me. Lessons learned.
A modern compile time type checked formatter would have prevented this mistake, you are deliberately choosing to use poor tools and calling this "pragmatism" because it sounds better than admitting you're bad at this and you don't even want to improve.
In fact C++ even shipped a pair of functions here. There's a compile time type checked formatter std::format, which almost everybody should use almost always (and which is what std::println calls), and there's also a runtime type checked formatter std::vformat, for those few cases where you absolutely can't know the format string until the last moment. That is a hell of a thing, if you need one of those I have to admit nobody else has one today with equal ergonomics.
- jstimpfle 3y agoThanks for the ad hominem, but let's put that into perspective. My current project is a GUI prototype based on plain Win32/Direct3D/Direct2D/DirectWrite. It currently clocks in at just under 6 KLOC. These are all the format calls in there (used git grep): fatal_f("Failed to CreateBuffer(): %lx", err); fatal_f("Failed to Map() buffer"); fatal_f("Failed to compile shader!"); fatal_f("Failed to CreateBuffer(): %lx", err); fatal_f("Failed to create blend state"); fatal_f("OOM"); fatal_f("Failed to register Window class"); fatal_f("Failed to CreateWindow()"); fatal_f("%s failed: error code %lx", what, hr); msg_f("Shader compile error messages: %s", errors->GetBufferPointer()); msg_f("Failed to compile shader but there are no error messages. " msg_f("HELLO: %d times clicked", count); msg_f("Click %s", item->name.buf); msg_f("Init text controller %p", this); msg_f("DELETE"); msg_f("Refcount is now %d", m_refcount); msg_f("Refcount is now %d", m_refcount); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); fprintf(stderr, "FATAL ERROR: "); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); snprintf(utext, sizeof utext, "Hello %d", ui->update_count); snprintf(filepath, sizeof filepath, "%s%s", int r = vsnprintf(m_buffer, sizeof m_buffer, fmt, ap); int r = vsnprintf(text_store, sizeof text_store, fmt, ap); snprintf(svg_filepath, sizeof svg_filepath, "%s", filepath); That's theory and practice for you. The real world is a bit more nuanced. Meanwhile I have 100 other, more significant problems to worry about than printf type safety. For example, how to get rid of the RAII based refcounting that I introduced but it wasn't exactly an improvement to my architecture. But thanks for the suggestion to use std::format in that set of cases and std::vformat in these other situations. I'll put those on my stack of C++ features to work through when I have time for things like that. (Let's hope that when I get there, those aren't already superseded by something safer). (Update: uh, std::format returns std::string. Won't use.)
- spkm 3y agoMaybe if you used a typesafe language, you wouldn't have to track down 100 other more significant problems (:
- jstimpfle 3y agoI'm using C/C++, which do provide a good level of type safety. And no, types are absolutely not my problem. In fact, rigid type systems are a frequent source of practical problems, and they often shift the game to one where you're solving type puzzles -- instead of working on the actual functionality.
- KerrAvon 3y agoCome on, dude. C provides almost no type safety whatsoever. There’s no point in saying “C/C++” here, because you won’t adopt the C++ solutions for making your code actually typesafe.
- jstimpfle 3y agoType safety is not a standardized term, and it is not binary. Being black and white about things is almost always bad. One needs to weigh and balance a large number of different concerns. A lot of "modern C++" is terrible, terrible code precisely because of failing to find a balance. Many C++ "solutions" are broken by design.
- throwaway2037 3y ago(Update: uh, std::format returns std::string. Won't use.) What type would you prefer?
- jstimpfle 3y agoGenerally I use printf/fprintf and snprintf. I would be looking for a replacement for snprintf for the most part, i.e. I want to provide the buffer. Apparently we can template< class... Args > std::print(std::ostream, std::format_string<Args...> fmt, Args&&... args ); (sigh of despair) since C++23, so that could be an option if there is an easy way to make a std::ostream of a memory slice. But I'm not on C++23 yet. Compare to int snprintf ( char * s, size_t n, const char * format, ... ); (sigh of relief) and I think it takes a special kind of person to think that std::print is strictly better.