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 14:03:55 -0500 Organization: Aioe.org NNTP Server Lines: 41 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:33180 On 11/20/2015 12:44 PM, leif.roar@dimnakorr.com wrote: > Cecil Westerhof wrote: >> Often is said that you should never use booleans as parameters. But I >> have a function compareObjects that takes two objects and a boolean >> that signifies if the function is verbose. (Sends logging.) >> >> I could of-course define compareObjects and compareObjectsVerbose and >> instead of writing: >> compareObjects(a, b, useVerbose); > > If this is a private method, that's perfectly fine, but if it's a method > that's part of an API, I'd probably want to do it like this: > > Enum LoggingVerbosity { > NONE, > VERBOSE; > } > > > boolean compareObjects( T a, T b ) { > return compareObjects( a, b, LoggingVerbosity.NONE ); > } > > boolean compareObjects( T a, T b, LoggingVerbosity verbosity ) { > switch( verbosity ) { > case NONE: ... > case VERBOSE: ... > default throw new ProgrammingError( "Unhandled LoggingVerbosity type " + verbosity > + " encountered in switch statement. This should not happen." ); > } > } That is a very valid point. boolean only have two value's so if the number of values could increase later then boolean should not be used and enum is the type safe way of doing that compared to int constants. Arne