4 comments on “Tippet: Using lambdas to make input and validation safer

  1. I just stumbled over this now and I think your solution is ugly.
    The flow of code is interrupted by a class definition and moreover the nested lambdas.

    I think it would be a lot nicer if you created actual functions using the comments as names.
    Your code would boil down to:
    auto n = readSampleCount(in);
    auto limits = readSampleValueLimits(in);
    return readSamples(in, n, limits);

    Now it’s flow is very easy to understand.
    And to not spoil global scope these functions and the bracket class could be in an anonymous namespace.
    In fact all that could also be put into a class, but I think that is just a matter of tastes.

    I think lambdas are overused. I’d hate to have to look at code like yours years later, especially if this style was used everywhere.
    In my experience parsing stuff tends to be similar amongst projects and within, code reuse is possible and advisable. You just made that harder though.

    Moreover especially bracket seems like an ideal class to be reused.
    With an overloaded stream operator reusable and flexible code becomes possible:

    Bracket Bracket::createFrom(std::istream &in) // a static function
    Bracket result; // default ctor is private
    in >> result; // throws on failure, fulfills strong exception guarantee in regard to bracket
    return result;

    • The example used was just to illustrate the technique. I chose it because it is easy to understand without too much explanation, not because it’s supposed to represent a perfect situation to use the technique generally. Nitpicking about the details means you’ve missed the point.

      Perhaps some kind of clamped value type with validating extractor is a generally useful tool… but that’s beside the point here. The point is just to illustrate the technique.

  2. It is easy to tell others they missed the point … good way of arguing.
    You illustrated the technique and I pointed out disadvantages. What’s bad about that?

    We both agree that variables should not be in an undefined state.
    Lambdas can be useful in this regard but easily overused.

    • If you agree that variables should not be in an invalid state and that lambdas can be useful to prevent that, that is the point of the post. Overuse of lambdas is a problem for another post. You’re welcome to write it if you like, and even link to it here.

Leave a Reply

Your email address will not be published. Required fields are marked *