The C example could have implemented a lot of validation just by checking the return value of sscanf():<p><pre><code> if (sscanf(user_input, "%4u-%2u-%2u", &year, &month, &day) != 3) {
// return an error
}
</code></pre>
This still does not catch trailing garbage, but you could check for that as well:<p><pre><code> if (sscanf(user_input, "%4u-%2u-%2u%c", &year, &month, &day, &dummy) != 3) {
// return an error
}
</code></pre>
The result would be 4 if there was at least one trailing character. Too bad there is still no std::scan() companion to C++23's std::print().
I don't see how this is in any way preferable to having an ordinary default constructor that does the same thing:<p><pre><code> // There are a few ways to let API callers bring their own
// memory, as they would in a no-malloc environment and this
// stack-friendly c'tor is a stand-in for that.
static Birthdate epoch() { return Birthdate(1900, 1, 1); }</code></pre>
C is perfectly capable of type-driven design. He's already got the type (struct), and although C is a bit limited, he can:<p>* return pointer-or-null<p>* choose "invalid" sentinel values and then use birthdate_is_valid(...) to check validity.<p>* Add an is_valid bool field (or even an error enum like in the C++23 example)<p>* Add an out field in the constructor function for the error code (similar to how ObjC does things).
Cool, incredibly low bar.<p>All four of your examples are <i>validate</i>.<p>Know any languages that are worse than C at this?
Or use an out field for the type itself, and use the return value for an error code (or just a bool). A common pattern in C#.
The C++11 example is the weakest in the article by its own thesis. Public throwing constructor, no year check, no leap-year check, so Birthdate(0, 2, 30) constructs cleanly. The C++17/23 shape (private ctor + static factory) is the actual mechanical insight from King's essay. Make the constructor a function that can fail, so the type itself carries the proof.
exactly, use std::expected as the return type, avoid exceptions, and make a failable factory constructor to build your type. Make invalid states unrepresentable!!!
Author has used LLMs to generate Java code in C++. It detracts from his point.
What Java code?<p>Regardless of how they might have used LLMs, I tend to have an issue with this kind of complaint, given the C++ example code on the Design Patterns: Elements of Reusable Object-Oriented Software book, released in 1994, 2 years before Java was made public.<p>Or the examples from "Using the Booch Method: A Rational Approach", "Designing Object Oriented C++ Applications Using The Booch Method", or "Using the Booch Method: A Rational Approach".<p>Additional there are enough framework examples starting with Turbo Vision in 1990, MacAPP in 1989, OWL in 1991, MFC in 1992,....<p>Somehow a C++ style that was prevalent in the industry between 1990 and 1996, that I bet plenty of devs still have to maintain in 2026, has become "Java in C++".
> Somehow<p>There's not much mystery about that - Java took that approach and ran with it, and now has much greater mindshare than C++.<p>Also, the mid-90s were before most software developers working today were born, I suspect. They'd have to go find a graybeard and ask them to tell them tales of yore, to find out about any of this.
No, it doesn't.
First thought, assuming that birth year starts at 1900 is bad for a number of reasons; one of which, "process this list of authors and ..."<p>What about everyone born before 1900?
It’s a contrived example. And I have to assume the author intended it to be contrived given that he also put an upper bound at 1999 in an article written in 2026 in an industry that skews young.<p>But the pattern applies regardless of the validation logic.
Or what if they were born after 1999?<p>It's just a toy example not a production ready birthday validation library.
Assuming it is necessarily known which is the birth year of anyone assumed to have been in existence is already a big hypothesis if we go in that direction.
C++ could use some do-notation
Disregarding the article for a second, has anyone else had the pattern that "parse don't validate" makes sense in object oriented style, but less sense in functional style programming? Like parsing and validating blurs into each other.
In my experience it makes even more sense in functional programming languages, not less, since they usually also have more powerful type systems that help with actually representing parsed vs unparsed data.
> Disregarding the article for a second, has anyone else had the pattern that "parse don't validate" makes sense in object oriented style, but less sense in functional style programming?<p><i>Parse, don't validate</i> was written around Haskell!
The tl;dr is that instead of representing emails as type String and manually sprinkling is_email(str) throughout your code, you represent as type Email, which has a function parse(String) -> Option<Email>. The type system then ensures the checks are present whenever they have to be, and nowhere else.<p>This is extremely natural to do in a language like Haskell or Rust. And incredibly unnatural to do in C++ for instance.
I hope this is not trolling so I'll bite. It is incredibly natural to represent an object, such as an email, as an Email class in object oriented languages like C++. It'd then have a constructor that accepts a string and constructs the email object from said string, or maybe a parse(string) -> Option<Email> thingy. The type system then ensures the checks are present whenever they have to be, and nowhere else.<p>Tl;dr: there's nothing extra that functional or OO programming give you here. Both allow you to represent the problem in a properly typed fashion. Why would you represent an email as a string unless you are a) deeply inexperienced or b) have some really good reason to drop all the benefits of a strongly typed language?
[dead]