Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #33179
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Never using booleans as parameters |
| Date | 2015-11-22 14:01 -0500 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <n2t3di$9lb$1@speranza.aioe.org> (permalink) |
| References | <87lh9tysy8.fsf@Equus.decebal.nl> |
On 11/20/2015 3:29 AM, 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);
> I could write:
> if (useVerbose) {
> compareObjectsVerbose(a, b);
> } else {
> compareObjects(a, b);
> }
>
> But I do not find that cleaner code. But that could because I am not
> used to it. Would the second manner be better?
As already pointed out by others then sending boolean as parameters
are widely used.
But it is true that there are some criticism of the construct at
what I would call "the finer theoretical level".
The criticism goes primarily on the fact that a boolean parameter
indicate that the method does two different things and that it
should be split up in two different methods due to that.
And in theory that is a valid argument.
But if we move into the more practical world, then it becomes
less clear in my opinion.
I would distinguish between scenarios where it is different
main logic and scenarios where it is just some
auxiliary functionality.
public void foobar(int n, String s, boolean flag) {
if(flag) {
// 10 lines of code
} else {
// 10 lines of code
}
}
indeed look like a suspicious design and should be done as:
public void foobarTrue(int n, String s) {
// 10 lines of code
}
public void foobarFalse(int n, String s) {
// 10 lines of code
}
But another example:
public void foobar(int n, String s, boolean flag) {
// 5 lines of code
if(flag) {
// 1 line of code
}
// 5 lines of code
if(flag) {
// 1 line of code
}
// 5 lines of code
}
does not look so bad.
In fact I think it is better than:
public void foobarTrue(int n, String s) {
// 5 lines of code
// 1 line of code
// 5 lines of code
// 1 line of code
// 5 lines of code
}
public void foobarFalse(int n, String s) {
// 5 lines of code
// 5 lines of code
// 5 lines of code
}
as that one duplicates too much code.
Someone then recommend:
public void foobarTrue(int n, String s) {
foobar(n, s, true);
}
public void foobarFalse(int n, String s) {
foobar(n, s, false);
}
private foobar(int n, String s, boolean flag) {
// 5 lines of code
if(flag) {
// 1 line of code
}
// 5 lines of code
if(flag) {
// 1 line of code
}
// 5 lines of code
}
But I am not buying into that paradigm of designing method
API differently depending on visibility.
So from a pragmatic perspective then I would say that it
depends.
For your verbose flag triggering logging I don't see it as
a problem.
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Never using booleans as parameters Cecil Westerhof <Cecil@decebal.nl> - 2015-11-20 09:29 +0100
Re: Never using booleans as parameters Martin Gregorie <martin@address-in-sig.invalid> - 2015-11-20 11:54 +0000
Re: Never using booleans as parameters Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-20 08:30 -0500
Re: Never using booleans as parameters Eric Douglas <e.d.programmer@gmail.com> - 2015-11-20 05:57 -0800
Re: Never using booleans as parameters Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-20 09:17 -0500
Re: Never using booleans as parameters Eric Douglas <e.d.programmer@gmail.com> - 2015-11-20 05:34 -0800
Re: Never using booleans as parameters Silvio <silvio@internet.com> - 2015-11-20 18:18 +0100
Re: Never using booleans as parameters Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-20 16:08 -0500
Re: Never using booleans as parameters Silvio <silvio@internet.com> - 2015-11-23 14:12 +0100
Re: Never using booleans as parameters Silvio <silvio@internet.com> - 2015-11-23 14:23 +0100
Re: Never using booleans as parameters Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-23 08:42 -0500
Re: Never using booleans as parameters Silvio <silvio@internet.com> - 2015-11-23 15:14 +0100
Re: Never using booleans as parameters Eric Douglas <e.d.programmer@gmail.com> - 2015-11-23 06:10 -0800
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-29 15:45 -0500
Re: Never using booleans as parameters Martin Gregorie <martin@address-in-sig.invalid> - 2015-11-30 00:08 +0000
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-29 19:32 -0500
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-29 19:39 -0500
Re: Never using booleans as parameters Brixomatic <brixomatic@yahoo.com> - 2015-12-20 15:08 +0100
Re: Never using booleans as parameters Eric Douglas <e.d.programmer@gmail.com> - 2015-12-21 05:00 -0800
Re: Never using booleans as parameters Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-21 08:53 -0500
Re: Never using booleans as parameters Gene Wirchenko <genew@telus.net> - 2015-11-30 10:33 -0800
Re: Never using booleans as parameters Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2015-12-02 15:51 +0200
Re: Never using booleans as parameters Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2015-11-23 21:55 -0800
Re: Never using booleans as parameters Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-24 08:23 -0500
Re: Never using booleans as parameters Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2015-11-27 22:52 -0800
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-29 15:55 -0500
Re: Never using booleans as parameters leif.roar@dimnakorr.com - 2015-11-20 11:44 -0600
Re: Never using booleans as parameters Eric Douglas <e.d.programmer@gmail.com> - 2015-11-20 10:08 -0800
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-22 14:03 -0500
Re: Never using booleans as parameters Gene Wirchenko <genew@telus.net> - 2015-11-20 10:14 -0800
Re: Never using booleans as parameters Eternal Hope <laughingspam99@gmail.com> - 2015-11-22 14:27 +0000
Re: Never using booleans as parameters Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2015-11-20 20:13 -0800
Re: Never using booleans as parameters Eric Douglas <e.d.programmer@gmail.com> - 2015-11-23 04:59 -0800
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-29 15:53 -0500
Re: Never using booleans as parameters galt_57@hotmail.com - 2015-11-22 09:02 -0800
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-22 12:20 -0500
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-22 12:41 -0500
Re: Never using booleans as parameters Martin Gregorie <martin@address-in-sig.invalid> - 2015-11-22 18:06 +0000
Re: Never using booleans as parameters Gene Wirchenko <genew@telus.net> - 2015-11-25 14:21 -0800
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-29 15:58 -0500
Re: Never using booleans as parameters Gene Wirchenko <genew@telus.net> - 2015-11-30 10:39 -0800
Re: Never using booleans as parameters Brixomatic <brixomatic@yahoo.com> - 2015-11-29 16:40 +0100
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-29 16:00 -0500
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-29 19:26 -0500
Re: Never using booleans as parameters Eric Douglas <e.d.programmer@gmail.com> - 2015-11-30 06:16 -0800
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-22 14:01 -0500
Re: Never using booleans as parameters Fredrik Jonson <fredrik@jonson.org> - 2015-11-24 18:50 +0000
Re: Never using booleans as parameters Sir Hugh Jarce <hugh@example.com> - 2015-11-27 10:07 +0000
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-29 15:49 -0500
Re: Never using booleans as parameters Brixomatic <brixomatic@yahoo.com> - 2015-11-29 16:33 +0100
Re: Never using booleans as parameters Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-29 15:57 -0500
Re: Never using booleans as parameters Arne Vajhøj <arne@vajhoej.dk> - 2015-11-29 16:01 -0500
Re: Never using booleans as parameters Brixomatic <brixomatic@yahoo.com> - 2015-12-20 15:00 +0100
Re: Never using booleans as parameters Gene Wirchenko <genew@telus.net> - 2015-12-21 10:50 -0800
Re: Never using booleans as parameters Gene Wirchenko <genew@telus.net> - 2015-11-30 10:48 -0800
Re: Never using booleans as parameters Brixomatic <brixomatic@yahoo.com> - 2015-12-20 15:44 +0100
Re: Never using booleans as parameters Gene Wirchenko <genew@telus.net> - 2015-12-21 10:56 -0800
Re: Never using booleans as parameters Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-21 15:30 -0500
Re: Never using booleans as parameters Eric Douglas <e.d.programmer@gmail.com> - 2015-12-21 12:47 -0800
Re: Never using booleans as parameters Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-21 16:29 -0500
csiph-web