News for hackers. Stuff that matters.
Re:Early exit is a tail call optimization of procedural lang... by jkot on Monday December 14, 2015 @12:21PM (#10732357)
Original code seems like really bad way to check validity of parameters. Is this type of programming common in some languages? I have never seen similar code in Java.
Re:Early exit is a tail call optimization of procedural lang... by jpollock on Monday December 14, 2015 @12:36PM (#10732461)
Some groups have a "Functions can only have a single entry and single exit point" rule, meaning you have to jump through a lot of hoops.

Sometimes it is required. In Java, before try-with-resources, if you had any resource management (locks, file descriptors, sockets), a single exit point made resource management simpler. C code tended to have similar rules (or GOTO's to jump to the exit block in the function). C++ just used RAII, and made it the destructor's problem.

Re:Early exit is a tail call optimization of procedural lang... by SideburnsOfDoom on Monday December 14, 2015 @01:02PM (#10732606)
Right - that single-exit-point rule is generally a holdover from C.

It is pointless in languages like C# or Java which have GC and 'try' or 'using' blocks. In some cases the single return is nicer, but this is not universal nor is single return "required" in all cases.

In functional languages such as F#, Haskell or Erlang where the method is usually side-effect free and uses pattern-matching, multiple return-points are completely normal and no-one even sees an issue.

Re:Early exit is a tail call optimization of procedural lang... by jdmichal on Monday December 14, 2015 @03:25PM (#10733555)
Java has always allowed try-finally blocks, which is what a try-with-resources compiles down to anyway.

    Resource resource = new Resource();
    try {
        if (a)
            throw new Exception("Oops!");
        else if (b)
            return resource.doBStuff();
        else
            return resource.doStuff();

    } finally {
        resource.close();
    }