Re: An Introduction and a Philosophical Question Regarding Functions

236 views
Skip to first unread message

Pedro Giménez

unread,
May 9, 2013, 9:46:21 AM5/9/13
to clean-code...@googlegroups.com
Uncle Bob says:

Boolean arguments loudly declare that the function does more than one thing. They are confusing and should be eliminated.

A boolean in an argument list can be a significant source of confusion and error, What does it mean if is true? What does it mean if is false

And besides, it violates SRP.

On Thursday, May 9, 2013 2:36:21 PM UTC+2, Terence McGhee wrote:
Hello all.

Introduction

I've recently finished reading Clean Code (excellent btw) within the last 3 months.

I have been maniacally evangelizing to my peers at work every since. The atmosphere that our company's owner has created for us is phenomenal. I can't speak highly enough about it. Our owner is a highly skilled developer in his own right, and as such, is firmly focused on maintaining an environment where the devs can be most successful. We follow the Agile principles and we want to be the best developers in the world.

I've started applying the lessons learned from the book in my current project. However, none of the other devs are on board with this process (because they haven't yet had a chance to read the material. Most of them are still reading Code Complete and plan to read Clean Code next).  Everyone readily agreed that functions should only do one thing, so I figured the first thing I should start cleaning is our functions. 

During the code reviews, I have to defend my choices. I thoroughly enjoy this process, because as you said in Clean Coder, it gives me an opportunity to teach. 

The Problem

Recently, the team has expressed that it doesn't agree that passing a Boolean to a function should be avoided. I explained that it automatically implies that the method is doing more than one thing: one thing when the value is true and a different thing when the value is false. Yet they insist that the code to accommodate that guideline is bloated and much harder to follow. And they say things like "so you would really resort to Template Method just so you don't have to pass in a bool? That's making the code more complex, not less". To which I don't have a good reply.

My Question

So can you please explain this concept in more detail, or point me to a source where this is explained in more depth? I would really appreciate this.

Thanks!

Uncle Bob

unread,
May 9, 2013, 1:07:39 PM5/9/13
to clean-code...@googlegroups.com
The fact that they are mentioning template method gives this away.  Apparently the function that accepts the boolean is using the boolean in some inner loop.  The author wants two different behaviors for the body of that loop. 

Template method is not a bad idea, actually.  It's pretty simple with inner classes.  

OR

We can extract the body of the loop into a new function which takes a boolean.  This new function clearly does two things and an be turned into two different functions.  So the problem changes.  Now we need to tell the loop which of the two functions to call.  So instead of passing the boolean in, we could pass the function in.  

Of course you can't pass functions in Java -- yet.  But you _can_ pass objects that act like functions.  These objects could be created by the caller and passed into the loop.  Again, those objects could be instances of public inner classes that are in scope.  


Jon Reid

unread,
May 9, 2013, 11:44:06 AM5/9/13
to Terence McGhee, clean-code...@googlegroups.com
Hi Terence,

The main thing that's helping my coworkers begin to rethink things is the Clean Code video series. The business license is really reasonable: $10 per viewer, per episode. And the very first episode is $1 per viewer, just to get you started.

Share this 10-minute freebie with your manager http://www.cleancoders.com/promotions/the_business_case then ask him/her to expense episode one.

The argument that breaking methods into smaller pieces "makes the code more complex" will be put to rest by Uncle Bob. :)

- Jon

Terence McGhee

unread,
May 9, 2013, 4:48:45 PM5/9/13
to clean-code...@googlegroups.com
@Uncle Bob... awesome reply! 

Thank you!

Thanks to everyone else who also responded.

I'll take this logic back to my team and I'll report back with the results.

Thanks again!

Tomasz Cichecki

unread,
Jun 14, 2013, 4:25:45 PM6/14/13
to clean-code...@googlegroups.com
I'll tell my story about boolean parameters and how I hate them:
I am a php programmer and the php space is poluted by functions that take boolean parameters for example:
function var_dump(mixed $variable, $return = false) {}
function print_r(mixed $variable, $return = false) {}

You can have a look at them here

The purpose of those boolean parameter is to either print the variables on STDOUT (default $return=false) or return them.
That kind of signatures is so common that it's already become sort of a pattern or convention.


These function are usually used extensively in logging modules and I must admit it's not only unreadable but causes a lot of simple bugs because I keep forgetting to pass the TRUE and nothing get logged!
Following the guidelines of the clean code I've got a work around. Look:

function sprint_r($variable) {
   
return print_r($vaiable, true);
}

function svar_dump($variable) {
   
return var_dump($variable, true);
}

The only problem one of  my collegues found with it - it's great for a seasoned programmer that knows C.

Another thought I'd like to share about the boolean parameters is - if you introduce them you're just try to do better than your compiler/interpreter is the best at and you try to reinvent the function call dispatch mechanism.

W dniu czwartek, 9 maja 2013 14:36:21 UTC+2 użytkownik Terence McGhee napisał:
Hello all.

Introduction

I've recently finished reading Clean Code (excellent btw) within the last 3 months.

I have been maniacally evangelizing to my peers at work every since. The atmosphere that our company's owner has created for us is phenomenal. I can't speak highly enough about it. Our owner is a highly skilled developer in his own right, and as such, is firmly focused on maintaining an environment where the devs can be most successful. We follow the Agile principles and we want to be the best developers in the world.

I've started applying the lessons learned from the book in my current project. However, none of the other devs are on board with this process (because they haven't yet had a chance to read the material. Most of them are still reading Code Complete and plan to read Clean Code next).  Everyone readily agreed that functions should only do one thing, so I figured the first thing I should start cleaning is our functions. 

During the code reviews, I have to defend my choices. I thoroughly enjoy this process, because as you said in Clean Coder, it gives me an opportunity to teach. 

The Problem

Recently, the team has expressed that it doesn't agree that passing a Boolean to a function should be avoided. I explained that it automatically implies that the method is doing more than one thing: one thing when the value is true and a different thing when the value is false. Yet they insist that the code to accommodate that guideline is bloated and much harder to follow. And they say things like "so you would really resort to Template Method just so you don't have to pass in a bool? That's making the code more complex, not less". To which I don't have a good reply.

My Question

So can you please explain this concept in more detail, or point me to a source where this is explained in more depth? I would really appreciate this.

Thanks!

W dniu czwartek, 9 maja 2013 14:36:21 UTC+2 użytkownik Terence McGhee napisał:
Hello all.

Introduction

I've recently finished reading Clean Code (excellent btw) within the last 3 months.

I have been maniacally evangelizing to my peers at work every since. The atmosphere that our company's owner has created for us is phenomenal. I can't speak highly enough about it. Our owner is a highly skilled developer in his own right, and as such, is firmly focused on maintaining an environment where the devs can be most successful. We follow the Agile principles and we want to be the best developers in the world.

I've started applying the lessons learned from the book in my current project. However, none of the other devs are on board with this process (because they haven't yet had a chance to read the material. Most of them are still reading Code Complete and plan to read Clean Code next).  Everyone readily agreed that functions should only do one thing, so I figured the first thing I should start cleaning is our functions. 

During the code reviews, I have to defend my choices. I thoroughly enjoy this process, because as you said in Clean Coder, it gives me an opportunity to teach. 

The Problem

Recently, the team has expressed that it doesn't agree that passing a Boolean to a function should be avoided. I explained that it automatically implies that the method is doing more than one thing: one thing when the value is true and a different thing when the value is false. Yet they insist that the code to accommodate that guideline is bloated and much harder to follow. And they say things like "so you would really resort to Template Method just so you don't have to pass in a bool? That's making the code more complex, not less". To which I don't have a good reply.

My Question

So can you please explain this concept in more detail, or point me to a source where this is explained in more depth? I would really appreciate this.

Thanks!

Reply all
Reply to author
Forward
0 new messages