Timeline for Is it wrong to use a boolean parameter to determine behavior?
Current License: CC BY-SA 3.0
19 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 27, 2023 at 12:59 | comment | added | gnasher729 | Any of the commentators actually working with Objective-C? saveWithEncryption is the way how you name a parameter as part of the method name. Any Objective-C developer knows that this will be followed by a YES or NO argument. | |
| May 12, 2018 at 10:23 | history | wiki removed | Thomas Owens♦ | ||
| Jul 28, 2017 at 13:09 | history | edited | Deduplicator | CC BY-SA 3.0 |
added syntax-highlighting
|
| Mar 5, 2013 at 23:30 | history | made wiki | Post Made Community Wiki by Eva | ||
| May 10, 2012 at 18:01 | comment | added | Caleb | @BlueRaja-DannyPflughoeft That seems like a fine solution, and probably more in keeping with typical Java practice. I voted your answer up. | |
| May 10, 2012 at 17:35 | comment | added | Christoffer Hammarström |
And your tea examples are awful. Provide a realistic example. If i were to model mixing of ingredients i might do cup.add(tea(2, DECILITER), milk(5, CENTILITER), sugarLumps(1), lemonSlices(1)). Or possibly tea.withSugar().withLemon().withMilk(), or tea.with(sugar, lemon, milk).
|
|
| May 10, 2012 at 17:34 | comment | added | BlueRaja - Danny Pflughoeft |
I would prefer file.save(useEncryption: false)
|
|
| May 10, 2012 at 17:27 | comment | added | Christoffer Hammarström | The method is bad, because there is obviously "doubt about the meaning of 'false' here". Anyway, i would never write a method like that in the first place. Saving and encrypting are separate actions, and a method should do one thing and do it well. See my earlier comments for better examples how to do it. | |
| May 10, 2012 at 16:02 | comment | added | Caleb |
@ChristofferHammarström Clearly, we're each reading different things into the name; what it "says" to me is "save, with encryption(on or off)," i.e. the parameter is a switch for enabling encryption. See the UINavigationController example in my answer for a similar case. You might prefer a name like saveConsideringEncryption(boolean) or saveWithEncryptionOption(boolean). To me, teaWithLemonAndSugarAndMilk(true, true, false) is a lot easier to understand than tea(true, true, false); it means "tea with lemon and sugar but no milk."
|
|
| May 10, 2012 at 15:10 | comment | added | Christoffer Hammarström |
Actually, code that does something else than what it says doesn't work, and is thus a bug. It does not fly to make a method named saveWithEncryption(boolean) that sometimes saves without encryption, just like it doesn't fly to make a saveWithoutEncryption(boolean) that sometimes saves with encryption.
|
|
| May 10, 2012 at 14:39 | comment | added | Caleb | @ChristofferHammarström The style may not be your cup of tea, but it's only a bug if it doesn't work. IMO, 1) using parameters to control optional behavior in a method is legitimate; 2) the number and types of parameters (including boolean) doesn't change (1); 3) naming methods in a way that helps to document the parameters can be a good thing. I'll agree that (3) doesn't have any real bearing on the other two points or the OP's question, and my POV on the matter is heavily influenced by Obj-C. Java culture may be different, and that's fine, but it doesn't make it a bad idea. | |
| May 10, 2012 at 14:16 | comment | added | Christoffer Hammarström |
Or you should have two methods, saveWithEncryption() and saveWithoutEncryption().
|
|
| May 10, 2012 at 14:09 | comment | added | Christoffer Hammarström |
A method named saveWithEncryption that sometimes doesn't save with encryption is a bug. It should possiby be file.encrypt().save(), or like Javas new EncryptingOutputStream(new FileOutputStream(...)).write(file).
|
|
| May 10, 2012 at 0:11 | comment | added | Caleb |
@JamesYoungman I certainly don't mean that the method name is entirely self-documenting, but once you've looked it up once, the withEncryption should be a strong reminder of what the method does and what the parameter means. Works well with multiple parameters, too. If a method is called colorFromRedGreenBlue(float, float, float) you don't have to remember which parameter does what because the method name tells you.
|
|
| May 9, 2012 at 23:34 | comment | added | James Youngman |
An alternative hypothesis is that false means, don't overwirte any existing file of the same name. Contrived example I know, but to be sure you'd need to check the documentation (or code) for the function.
|
|
| May 9, 2012 at 23:27 | comment | added | Caleb |
@Ray The idea (seems obvious to me) is that the saveWithEncryption() method can save the file either encrypted or not encrypted. Putting withEncryption in the method name reminds the reader of what the parameter means. You're right: file.save(false); is clear as mud. Including the meaning of the parameters in the method name helps with that.
|
|
| May 9, 2012 at 23:20 | comment | added | Ray |
Actually I have no clue what false means in the file.saveWithEncryption example. Does it mean that it will save without encryption? If so, why in the world would the method have "with encryption" right in the name? I could understand have a method like save(boolean withEncryption), but when I see file.save(false), it is not at all obvious at a glance that the parameter indicates that it would be with or without encryption. I think, in fact, this makes James Youngman's first point, about using an enum.
|
|
| May 9, 2012 at 23:14 | history | edited | Caleb | CC BY-SA 3.0 |
added 396 characters in body
|
| May 9, 2012 at 23:05 | history | answered | Caleb | CC BY-SA 3.0 |