Jump to content
Mike Torrettinni

Is it really that bad to Use boolean parameters to make your functions do two things?

Recommended Posts

I use

FindFiles('c:\', RECURSIVE_YES);

with a bunch of predefined boolean constants.

Edited by yonojoy
  • Like 1

Share this post


Link to post
22 minutes ago, yonojoy said:

I use


FindFiles('c:\', RECURSIVE_YES);

with a bunch of predefined boolean constants.

Interesting. I've been thinking about creating a global enum type, like TBooleanValues = (bEnableCotrol, bDisableControl, bRecursive, bNonRescursive,...) and use them as boolean True/False.

But this is still just an idea 😉 good to know somebody actually has implemented similar.

Share this post


Link to post

Why do you say so? You don't think it's a good idea?

 

It would solve many many questions about using booleans; also it would be one common place where I can connect such methods. Will give it another thought, but it you have a suggestion please let me know.

Edited by Mike Torrettinni

Share this post


Link to post
53 minutes ago, Mike Torrettinni said:

Why do you say so? You don't think it's a good idea?

 

It would solve many many questions about using booleans; also it would be one common place where I can connect such methods. Will give it another thought, but it you have a suggestion please let me know.

You've had my suggestion. Use a boolean and give your function a good name. Where you can't do that, use an enumerated type.

 

You seem to be looking for problems where none exist. These kind of problems don't need to be fixed! 

  • Like 2

Share this post


Link to post
2 minutes ago, David Heffernan said:

You've had my suggestion. Use a boolean and give your function a good name. Where you can't do that, use an enumerated type.

 

Thanks, I already improved some naming and split some methods so the name and what they do are more aligned and exact.

It's nice we have so many developers here and so many interesting implementations.

Share this post


Link to post
4 hours ago, Lars Fosdal said:

After doing a lot of SQL, I find myself wanting to name parameters in Delphi too...


TFileSearcher.FindFiles('c:\', aRecursive := True);

 

This is of course the holy grail, available in many other languages.

Share this post


Link to post

or use a const 🙂

 

const
  FILESEARCH_RECURSIVE = True;
  FILESEARCH_NONE_RECURSIVE = False;

...
...
TFileSearcher.FindFiles('c:\', FILESEARCH_RECURSIVE);

...
...

 

[edit]

already mentioned i see now, sorry

 

 

For myself i prefer the enumating way by the way.

 

Edited by mvanrijnen
  • Like 1

Share this post


Link to post
2 minutes ago, mvanrijnen said:

or use a const 🙂

 


const
  FILESEARCH_RECURSIVE = True;
  FILESEARCH_NONE_RECURSIVE = True;

...
...
TFileSearcher.FindFiles('c:\', FILESEARCH_RECURSIVE);

...
...

 

[edit]

already mentioned i see now, sorry

 

It's OK, always good to know more developers using certain implementation. Just don't turn const -> enum or you might get a truly shocking jolt of electricity 😉

Share this post


Link to post

Always understand the reason behind guidelines, suggestions, principles: why is it a good thing to do x - not just because famous person y said so.

Because when you understand why something is a good or bad thing to do you are able to evaluate the situations where you want or dont want to follow them and what implications arise.

  • Like 2
  • Thanks 1

Share this post


Link to post
1 hour ago, mvanrijnen said:

or use a const 🙂

 


const
  FILESEARCH_RECURSIVE = True;
  FILESEARCH_NONE_RECURSIVE = False;

...
...
TFileSearcher.FindFiles('c:\', FILESEARCH_RECURSIVE);

...
...

 

[edit]

already mentioned i see now, sorry

 

 

For myself i prefer the enumating way by the way.

 

Probably another bad idea in this topic which seems to be collecting a huge number of bad ideas. Do this for do different unrelated functions, and now you can pass the consts intended for function1 to function2, and vice versa.

 

 

  • Like 1

Share this post


Link to post

I have these basic rules
procedure name - verb. It expresses what the procedure does. He always has to do only one thing!
function name - noun. It expresses what it will actually return
names of routine argument - must express their purpose

It's nothing special if I rename something 5 times:classic_smile:

  • Like 1

Share this post


Link to post
Guest

tumb-rules:

  1. if you works in a "team", follow the rules!
    1. the manager's project is your guide!
      1. if you make a mistake, it's his fault!  :classic_biggrin:
      2. if he misses, it's his boss's fault!  :classic_cheerleader:
  2. if not, follow your rules and FKY!!! :classic_rolleyes:
    1. you should have already learned the basics in the environment in which you work!
Edited by Guest

Share this post


Link to post

My viewpoint to the topic's initial question:

Whole WinAPI is written this way. I mean, almost every function provides flags parameter, which alters the function behavior. WinAPI also have examples of Enable/Disable like cases:

ShowWindow(wnd, SW_HIDE);
EnableWindow(wnd, FALSE);

I thingk, it's ok, and it's a kind of well known pattern.

 

 

  • Like 1

Share this post


Link to post

EnableDisableWindow(wnd, FALSE) does not convey the meaning of FALSE, while EnableWindow(wnd, FALSE) does.

Share this post


Link to post
Just now, Lars Fosdal said:

EnableDisableWindow(wnd, FALSE) does not convey the meaning of FALSE, while EnableWindow(wnd, FALSE) does.

Agree.

Share this post


Link to post
8 minutes ago, Lars Fosdal said:

EnableDisableWindow(wnd, FALSE) does not convey the meaning of FALSE, while EnableWindow(wnd, FALSE) does.

Not really, or yes only when you read the documentation to know what false does. Same would be for DisableWindow(wnd, TRUE);

EnableDisableWindow name can give you more sense of what the function does, compared to the fact that EnableWindow can disable window.

 

Well in any case you need to read the documentation and then you know. I just wanted to point out that my original EnableDisableControls wasn't not such a bad name, after all 🙂

 

 

Share this post


Link to post
4 minutes ago, Mike Torrettinni said:

DisableWindow(wnd, TRUE);

DisableWindow with boolean parameter is a bad name, by the same reason why dontUseWidget is a bad name. As already described in this topic.

Share this post


Link to post
1 minute ago, balabuev said:

DisableWindow with boolean parameter is a bad name, by the same reason why dontUseWidget is a bad name. As already described in this topic.

I agree, although the point wasn't about the actual naming, but that boolean parameter does 2 opposite actions. The naming can be pretty much anything we want, I can use like EngedelyezoAblak() or AbilitaFinestra(), it would still be EnableWindow used to disable window.

 

 

Share this post


Link to post
5 minutes ago, Mike Torrettinni said:

although the point wasn't about the actual naming, but that boolean parameter does 2 opposite actions

But, in this particualr case these two things are related. If the function name expresses a positive action and the boolean parameter also expresses a positive action - then it's a kind of acceptable style. Otherwise, it will be a mess:

procedure EnableWindow(AHandle: HWND; AEnable: Boolean);   // Good and consistent.
procedure EnableWindow(AHandle: HWND; ADisable: Boolean);  // Bad.
procedure DisableWindow(AHandle: HWND; AEnable: Boolean);  // Bad.
procedure DisableWindow(AHandle: HWND; ADisable: Boolean); // Bad.

 

Share this post


Link to post
1 minute ago, balabuev said:

But, in this particualr case these two things are related. If the function name expresses a positive action and the boolean parameter also expresses a positive action - then it's a kind of acceptable style. Otherwise, it will be a mess:


procedure EnableWindow(AHandle: HWND; AEnable: Boolean);   // Good and consistent.
procedure EnableWindow(AHandle: HWND; ADisable: Boolean);  // Bad.
procedure DisableWindow(AHandle: HWND; AEnable: Boolean);  // Bad.
procedure DisableWindow(AHandle: HWND; ADisable: Boolean); // Bad.

 

True,

 

Perhaps it would be better something like ControlWindow.Enable() or ControlWindow(wnd, cwEnable) or ControlWindow(wnd, [cwEnable, cwActive]) with type TControlWindow = (cwEnable, cwDisable, cwActive, csFocused...);

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×