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 00:27:15 +0200 Lines: 50 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 rBS4IeuF/TFbYvsek2MX4wmxyim5jqUb3twfyNPqoK4TQusVk= Cancel-Lock: sha1:W9B4u0i+tjaT9SDt2twLSkY8cdE= 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:37154 On 12.08.2017 02:33, Arne Vajh=C3=B8j wrote: > I would go for: >=20 > 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; > } > } >=20 > 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. And another remark: we do not see the SQL but in this case I would make=20 sure that OLDEST_USED actually returns exactly one column and access=20 that via index instead of name. IIRC this tends to be slightly quicker. = But it may be just an old habit of mine. Kind regards robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/