Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / C# 6.0

Boolean parameters and code readability

Rate me:
Please Sign up or sign in to vote.
4.82/5 (25 votes)
25 Apr 2017CPOL1 min read 24.6K   6   23
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

C#
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:

C#
// 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:

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

Or if this syntax is not available, like this:

C#
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:

C#
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.

C#
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:

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

Our other methods then become:

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

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

Now instead of:

C#
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:

C#
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)


Written By
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.

Comments and Discussions

 
Questionyet another plug for using 'Enum Pin
BillWoodruff1-May-17 10:37
professionalBillWoodruff1-May-17 10:37 
QuestionGood to point this out Pin
irneb27-Apr-17 23:01
irneb27-Apr-17 23:01 
GeneralMy vote of 5 Pin
viler8426-Apr-17 15:18
viler8426-Apr-17 15:18 
SuggestionAnother simple option Pin
Michael S. Post26-Apr-17 6:40
Michael S. Post26-Apr-17 6:40 
GeneralRe: Another simple option Pin
pt140127-Apr-17 0:44
pt140127-Apr-17 0:44 
GeneralRe: Another simple option Pin
chrisbray27-Apr-17 6:20
chrisbray27-Apr-17 6:20 
GeneralRe: Another simple option Pin
irneb27-Apr-17 22:55
irneb27-Apr-17 22:55 
GeneralAs long you're writing your own wrapper function... Pin
PIEBALDconsult25-Apr-17 12:13
mvePIEBALDconsult25-Apr-17 12:13 
GeneralRe: As long you're writing your own wrapper function... Pin
JamesHurburgh25-Apr-17 14:44
JamesHurburgh25-Apr-17 14:44 
GeneralRe: As long you're writing your own wrapper function... Pin
PIEBALDconsult25-Apr-17 14:54
mvePIEBALDconsult25-Apr-17 14:54 
QuestionRe: As long you're writing your own wrapper function... Pin
pt140127-Apr-17 6:49
pt140127-Apr-17 6:49 
Question2 problems Pin
Paulo Zemek25-Apr-17 10:49
mvaPaulo Zemek25-Apr-17 10:49 
AnswerRe: 2 problems Pin
JamesHurburgh25-Apr-17 14:42
JamesHurburgh25-Apr-17 14:42 
GeneralRe: 2 problems Pin
Paulo Zemek25-Apr-17 19:47
mvaPaulo Zemek25-Apr-17 19:47 
GeneralRe: 2 problems Pin
JamesHurburgh25-Apr-17 23:09
JamesHurburgh25-Apr-17 23:09 
GeneralRe: 2 problems Pin
pt140127-Apr-17 7:20
pt140127-Apr-17 7:20 
GeneralRe: 2 problems Pin
Paulo Zemek27-Apr-17 12:56
mvaPaulo 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
JamesHurburgh25-Apr-17 4:57 
QuestionWhy not use ’is’? Pin
George Swan20-Apr-17 4:17
mveGeorge Swan20-Apr-17 4:17 
AnswerRe: Why not use ’is’? Pin
JamesHurburgh20-Apr-17 5:07
JamesHurburgh20-Apr-17 5:07 
GeneralRe: Why not use ’is’? Pin
the Kris26-Apr-17 12:43
the 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.