4 ms·
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 requir
by jpollock 11y ago
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.
- SideburnsOfDoom 11y agoRight - 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.
- jdmichal 11y agoJava 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(); }