Click here to Skip to main content
13,145,068 members (56,282 online)
Click here to Skip to main content
Add your own
alternative version

Stats

9.3K views
5 bookmarked
Posted 19 Apr 2017

Boolean parameters and code readability

, 25 Apr 2017
Rate this:
Please Sign up or sign in to vote.
Annotating parameters vs Rewriting methods for code readability

In response to Annotating constant parameters (Mar 31, 2017).

The example given in the article above tries to address the ambiguity of parameters in methods calls. If a call such as

GetRecord(true, false);

Is used, the reader has no idea what true or false mean in this case. Usually they may have some indicator if these values are variables or parameters such as:

// Example 1: Using variables
var autoCreate = true;
var useCache = false;
GetRecord(autoCreate, useCache);

// Example 2: Using parameters
public void ProcessRecord(bool autoCreate, bool useCache){
    GetRecord(autoCreate, useCache);
    ...
}

However the author suggests that if this is not the case then you should annotate your parameters like this:

GetRecord(autoCreate: true, useCache: false);

Or if this syntax is not available, like this:

GetRecord(true /* autoCreate */, false /* useCache */);

On the face of it, I don't disgree that this makes the code a little more readable, however if you can, you should go further. Imagine the GetRecord function itself:

public Record GetRecord(bool autoCreate, bool useCache)
{
    Record record = null;
    if(useCache){
        record = cache.GetRecord();
    }else{
        record = database.GetRecord();
    }
    if(autoCreate && record == null){
        record = new Record;
    }
    return record;
}

Instead of using arbitary parameters, I would instead break this down into multiple methods to make it more readable and useable.

public Record GetRecord(bool useCache)
{ 
    if(useCache){
        return cache.GetRecord();
    }else{
        return database.GetRecord();
    }
}
    
public GetOrCreateRecord(bool useCache)
{
    var record = GetRecord(useCache);
    if(record == null){
        record = new Record;
    }
    return record;
}

We now have two simpler methods that do what their name implies. However the parameter for useCache is an arbitary boolean that could be eliminated. As a consumer of the GetRecord function I shouldn't have to specify where it's getting it's record from.
There are a few solutions to this depending on what is required. The simplest is to create another method. I would usually think that using a cache would be implied unless otherwise specified, so I would create another method as follows:

public Record GetRecordNoCache()
{       
    return database.GetRecord();
}

Our other methods then become:

public Record GetRecord()
{       
    return cache.GetRecord() ?? GetRecordNoCache();
}

public GetOrCreateRecord()
{
    return GetRecord() ?? new Record();
}

Now instead of:

public Record GetRecord(bool autoCreate, bool useCache)
{
    Record record;
    if(useCache){
        record = cache.GetRecord();
    }else{
        record = database.GetRecord();
    }
    if(autoCreate && record == null){
        record = new Record;
    }
    return record;
}

GetRecord(autoCreate: false, useCache: true);
GetRecord(autoCreate: false, useCache: false);
GetRecord(autoCreate: true, useCache: true);

We have:

public Record GetRecordNoCache()
{       
    return database.GetRecord();
}   
public Record GetRecord()
{       
    return cache.GetRecord() ?? GetRecordNoCache();
}   
public GetOrCreateRecord()
{
    return GetRecord() ?? new Record();
}

GetRecord();
GetRecordNoCache();
GetOrCreateRecord();

Which is another step forward in making our code more readable.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

JamesHurburgh
Software Developer (Senior)
Australia Australia
I have been developing software for 22 years, 10 of those professionally. I'm interested in a wide range of topics but tend to focus on things such as efficiency and code readability.

You may also be interested in...

Pro

Comments and Discussions

 
Questionyet another plug for using 'Enum Pin
BillWoodruff1-May-17 10:37
mvpBillWoodruff1-May-17 10:37 
QuestionGood to point this out Pin
irneb27-Apr-17 23:01
memberirneb27-Apr-17 23:01 
GeneralMy vote of 5 Pin
viler8426-Apr-17 15:18
memberviler8426-Apr-17 15:18 
SuggestionAnother simple option Pin
Michael S. Post26-Apr-17 6:40
memberMichael S. Post26-Apr-17 6:40 
GeneralRe: Another simple option Pin
pt140127-Apr-17 0:44
memberpt140127-Apr-17 0:44 
GeneralRe: Another simple option Pin
chrisbray27-Apr-17 6:20
memberchrisbray27-Apr-17 6:20 
GeneralRe: Another simple option Pin
irneb27-Apr-17 22:55
memberirneb27-Apr-17 22:55 
GeneralAs long you're writing your own wrapper function... Pin
PIEBALDconsult25-Apr-17 12:13
protectorPIEBALDconsult25-Apr-17 12:13 
GeneralRe: As long you're writing your own wrapper function... Pin
JamesHurburgh25-Apr-17 14:44
memberJamesHurburgh25-Apr-17 14:44 
GeneralRe: As long you're writing your own wrapper function... Pin
PIEBALDconsult25-Apr-17 14:54
protectorPIEBALDconsult25-Apr-17 14:54 
QuestionRe: As long you're writing your own wrapper function... Pin
pt140127-Apr-17 6:49
memberpt140127-Apr-17 6:49 
Question2 problems Pin
Paulo Zemek25-Apr-17 10:49
professionalPaulo Zemek25-Apr-17 10:49 
AnswerRe: 2 problems Pin
JamesHurburgh25-Apr-17 14:42
memberJamesHurburgh25-Apr-17 14:42 
GeneralRe: 2 problems Pin
Paulo Zemek25-Apr-17 19:47
professionalPaulo Zemek25-Apr-17 19:47 
GeneralRe: 2 problems Pin
JamesHurburgh25-Apr-17 23:09
memberJamesHurburgh25-Apr-17 23:09 
GeneralRe: 2 problems Pin
pt140127-Apr-17 7:20
memberpt140127-Apr-17 7:20 
GeneralRe: 2 problems Pin
Paulo Zemek27-Apr-17 12:56
professionalPaulo Zemek27-Apr-17 12:56 
Questionautocreate Pin
LanLa23-Apr-17 21:01
professionalLanLa23-Apr-17 21:01 
AnswerRe: autocreate Pin
JamesHurburgh25-Apr-17 4:57
memberJamesHurburgh25-Apr-17 4:57 
QuestionWhy not use ’is’? Pin
George Swan20-Apr-17 4:17
memberGeorge Swan20-Apr-17 4:17 
AnswerRe: Why not use ’is’? Pin
JamesHurburgh20-Apr-17 5:07
memberJamesHurburgh20-Apr-17 5:07 
GeneralRe: Why not use ’is’? Pin
the.Kris26-Apr-17 12:43
memberthe.Kris26-Apr-17 12:43 
QuestionSimple and elegant Pin
Nelek20-Apr-17 1:37
protectorNelek20-Apr-17 1:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 25 Apr 2017
Article Copyright 2017 by JamesHurburgh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid