Path: csiph.com!eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Never using booleans as parameters Date: Sun, 22 Nov 2015 12:41:14 -0500 Organization: Aioe.org NNTP Server Lines: 70 Message-ID: References: <87lh9tysy8.fsf@Equus.decebal.nl> NNTP-Posting-Host: 1lM8B9/qdtKBc/uIRK2Gjw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:33177 On 11/20/2015 12:17 PM, Stefan Ram wrote: > Cecil Westerhof writes: >> compareObjects(a, b, useVerbose); > > That's alright. Just don't use boolean > literals, such as: > > compareObjects( a, b, false ); > > . Instead write: > > { final boolean verbose = false; > compareObjects( a, b, verbose ); } The literals question is not specific for boolean at all. engine.process(1, false); is difficult to read (you either need to remember what those parameters means or rely on an IDE to provide the information as mouse over), but the int is just as difficult to read as the boolean. Furthermore the solution specified is not optimal in my opinion. The best solution is probably the classic: private static final int ONE_THREAD = 1; private static final boolean VERBOSE_OFF = false; ... engine.process(ONE_THREAD, VERBOSE_OFF); as it documents what the parameters are. final int ONE_THREAD = 1; final boolean VERBOSE_OFF = false; engine.process(ONE_THREAD, VERBOSE_OFF); is not as good in my opinion. For two reasons: 1) it adds extra unnecessary lines to what the reader have to read when reading the code 2) it is not (as) ready for using the same value for multiple method calls Note that the readability argument against literals can be almost eliminated by a different design: engine.setThreads(1); engine.setVerbose(false); engine.process(); Some will still dislike the literals, but I do not see a readability issue. The arguments against the construct will be the reusability and the consistency. But I do not see that construct as really bad. Note that many other languages allows named parameters to do the same. Syntax like: engine.process(threads := 1, verbose := false); Arne