Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Where to put the return Date: Sun, 13 Aug 2017 13:15:27 +0200 Lines: 55 Message-ID: References: <87zib6zeup.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: individual.net 7Gobqs790Iaj1rhtf5+m8wZVvFJABz+/MCHDLNh/fl1Wy1PXQ= Cancel-Lock: sha1:bQeJ9boyNtO6nz3bVH0Q6z+13J4= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 In-Reply-To: Content-Language: en-US Xref: csiph.com comp.lang.java.programmer:37174 On 13.08.2017 03:41, Arne Vajh=C3=B8j wrote: > On 8/12/2017 6:27 PM, Robert Klemme wrote: >> On 12.08.2017 02:33, Arne Vajh=C3=B8j wrote: >>> I would go for: >>> >>> private static final String NO_VALUE =3D ""; >>> private static String getOldestUsed() { >>> try (Statement stmt =3D sqlite3Conn.createStatement();= >>> ResultSet rsOldest =3D stmt.executeQuery(OLDEST_USED))= { >>> if (rsOldest.next()) { >>> return rsOldest.getString("used"); >>> } else { >>> return NO_VALUE; >>> } >>> } catch (SQLException e) { >>> giveError("Error in displayOldestUsed"); >>> e.printStackTrace(); >>> fatalError(""); >>> return NO_VALUE; >>> } >>> } >>> >>> But there are many acceptable ways of writing this. >> >> Your approach has a few advantages though: >> >> - The compiler will catch the case that you forget to include >> an explicit return in the catch. >> - For the reader the code for the regular case (i.e. without >> exception) is in one place (in the try body) so it is easier >> to digest. >> >> Btw. I'd rather throw in catch than return a special value. That is=20 >> cleaner and does not break if the special value actually occurs in the= =20 >> database. >=20 > Yes. >=20 > Exception instead of "magic value" is better if possible. One more thing: since you used the same "magic value" for "exception"=20 and "not found" caller cannot distinguish. Whether that is an issue=20 depends of course on the other logic. Just wanted to point that out. Cheers robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/