Live data from Hacker News

"Parse, don't validate" through the years with C++

derekrodriguez.dev

21–30 of 52 posts

Re: "Parse, don't validate" through the years with C++

#21

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.

> 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?

Parse, don't validate was written around Haskell!

Re: "Parse, don't validate" through the years with C++

#22
post #11
post #4

Author has used LLMs to generate Java code in C++. It detracts from his point.

What Java code? 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. Or the examples from "Using the Booch Method: A Rational Approach", "Designing Object Oriented C++ Applications Using The Booch Method", or "Using t…

> Somehow

There's not much mystery about that - Java took that approach and ran with it, and now has much greater mindshare than C++.

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.

Re: "Parse, don't validate" through the years with C++

#23
post #21

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.

> 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? Parse, don't validate was written around Haskell!

What I tried and apparently failed to express with "parsing and validating blurs into each other." was that parsing more easily becomes "just what you do" in functional style of programming. To the point that nowadays I can no longer really remember what I did back when I tried to "validate" things instead of parsing them.

Re: "Parse, don't validate" through the years with C++

#24
I don't see how this is in any way preferable to having an ordinary default constructor that does the same thing:

    // 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); }

Re: "Parse, don't validate" through the years with C++

#25

C is perfectly capable of type-driven design. He's already got the type (struct), and although C is a bit limited, he can: * return pointer-or-null * choose "invalid" sentinel values and then use birthdate_is_valid(...) to check validity. * Add an is_valid bool field (or even an error enum like in the C++23 example) * Add an out field in the constructor function for the error code (similar to how ObjC does things).

Cool, incredibly low bar.

All four of your examples are validate.

Know any languages that are worse than C at this?

Re: "Parse, don't validate" through the years with C++

#26
post #22
post #11

Earlier quoted context omitted.

What Java code? 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. Or the examples from "Using the Booch Method: A Rational Approach", "Designing Object Oriented C++ Applications Using The Booch Method", or "Using t…

> Somehow There's not much mystery about that - Java took that approach and ran with it, and now has much greater mindshare than C++. 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.

We gladly tell bonefire tales. :)

Re: "Parse, don't validate" through the years with C++

#27

C is perfectly capable of type-driven design. He's already got the type (struct), and although C is a bit limited, he can: * return pointer-or-null * choose "invalid" sentinel values and then use birthdate_is_valid(...) to check validity. * Add an is_valid bool field (or even an error enum like in the C++23 example) * Add an out field in the constructor function for the error code (similar to how ObjC does things).

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#.

Re: "Parse, don't validate" through the years with C++

#28

I don't see how this is in any way preferable to having an ordinary default constructor that does the same thing: // 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); }

[deleted]

Re: "Parse, don't validate" through the years with C++

#29
The C example could have implemented a lot of validation just by checking the return value of sscanf():

    if (sscanf(user_input, "%4u-%2u-%2u", &year, &month, &day) != 3) {
        // return an error
    }
This still does not catch trailing garbage, but you could check for that as well:

    if (sscanf(user_input, "%4u-%2u-%2u%c", &year, &month, &day, &dummy) != 3) {
        // return an error
    }
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().
Post reply on HN