• Hanzito (unregistered)

    I've been bitten by an assignment to some (boolean) field called 'Open'. Testing it would tell you if there was a database connection. Assignment was the way to open or close it. No idea why.

  • (nodebb)

    And here I am, thinking that a function should at least do something. Like, the size is width * height, or its required space on disk... or...

    misunderstanding the assignment, indeed.

  • TopTension (unregistered)

    Since C++ allows the overriding of = operators, any assignment of structs or classes is suspicious. Plus they might contain instances of other classes, that might throw exceptions in their = operator.

    While I still think this is overdone for the SIZE struct, I can see the logic behind it.

  • (nodebb)

    maybe tomorrow this assingment

    An assing ment, indeed.

  • Gnasher729 (unregistered)

    Swift doesn’t allow try/catch when there is nothing to catch. If you called a function that returns a size or throws but you don’t care which exception is then your function wouldn’t return true/false but on optional size, like

    return try? FunctionReturningSize()

    The try? means an exception is turned into returning nil.

  • Tim Ward (unregistered)

    It was supposed to do clever sums taking into account the sizes of borders, shadows and suchlike, but they haven't written that bit yet.

  • TheCPUWizard (unregistered)

    (admittedly too lazy to check this morning)... but if the reference is to protected memory, then an exception should be thrown...)

  • Nonymous (unregistered)

    Are we just going to ignore the questionable naming? Surely you have a right to expect a function named CalculateBmpImageSize to return something representing a size. But here we get a boolean instead.

  • Foobie Bletch (unregistered)

    Nobody's mentioning the use of BOOL? Regular plain old "bool" has been standardized for over 20 years at this point. Unless you really NEED to pass it to some windows API function that expects a BOOL, there's no excuse.

    Also, for such a short function, if that could actually throw an exception (ignoring the whole swallow-exceptions-so-we-can-return-a-BOOL thing), why wouldn't you just return false right there instead of mucking about with setting some local return value variable? Functions should be more like "check prerequisites, if they aren't met, then just fail right there instead of pretending everything's ok for the rest of the function for the sake of having a single return point." The "single return point" thing may make sense in a language where you don't have something like RAII, but when you do, USE IT instead of having manual cleanup at the end and forcing the function to only have a single return point!

  • (nodebb) in reply to TheCPUWizard

    but if the reference is to protected memory, then an exception should be thrown

    Depends on what exactly you mean by "protected memory". Given that NewImageSize is a writable reference (implicitly because it isn't a "reference to const"), it can't be read-only without severe shenanigans, and in any event, that just causes UB. In some contexts (notably Visual C++ starting with version 6), the "segfault" gets turned into a "catch-with-dotdotdot" exception because C++ exceptions got switched from some C++-y mechanism in VC++5 to riding on the back of NT's SEH in VC++6.

    But it's UB in any case.

  • (nodebb) in reply to Foobie Bletch

    The class appears to follow MFC naming conventions, so this sounds like a case of "When in Rome, do as Romans do."

    On the other hand, I wholeheartedly agree with you on the whole "Checking prerequisites should trump Single-Entry-Single-Exit", including in languages without RAII/try-catch like C: There's no harm in returning early if your function hasn't done anything yet.

  • LZ79LRU (unregistered) in reply to Medinoc
    Comment held for moderation.
  • (nodebb) in reply to Nonymous

    Are we just going to ignore the questionable naming? Surely you have a right to expect a function named CalculateBmpImageSize to return something representing a size.

    Actually a common naming pattern when using output parameters. "Calculate" is what the function does, not what it returns. By contrast a function name bmpImageSize or getBmpImageSize would more conventionally indicate a return value, though I've sadly seen the latter also used with output routines - though mostly in Fortran, which often doesn't give you mich of a choice there.

  • (nodebb) in reply to Gnasher729

    Swift also requires you to explicitly declare when a function can throw an error and also explicitly mark each expression where a throwing function is called with try. It means you - and the compiler - always know if you've got to handle exceptions. In my opinion, the former is a prerequisite for any language that gives you the ability to catch exceptions and handle them. Otherwise, you can never be sure if you need an exception handler and, for safety, you will always have to ut one in.

  • (nodebb) in reply to jeremypnet

    They tried it with Java... and if I understood correctly, the reason they didn't do it in .NET is that Java's use case ended up too annoying for devs, so every function was either declared throws Exception or contained a big try/catch that swallowed all of them.

Leave a comment on “Misunderstood the Assignment”

Log In or post as a guest

Replying to comment #:

« Return to Article