HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Function "between" for "Everything in One Line" library

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
linefunctiononebetweenforeverythinglibrary

Problem

This is a function to determine if a number is between x and y. It is part of a library of mine called EOL or "Everything in One Line".

#include 

#ifndef _WINDOWS_H
    #include 
#endif

bool between (WORD n, WORD x, WORD y) { return (n >= x && n <= y) ? (true) : (false); }

Solution

There's no need to use the conditional operator here. This would be enough:

bool between (WORD n, WORD x, WORD y) { 
    return n >= x && n <= y;
}


Other than this, the variable names are not descriptive. It is not clear that x specifies the lower bound and y the higher bound.

bool between (WORD value, WORD low, WORD high) { 
    return value >= low && value <= high;
}


Additionally, I have to question a little bit the usage for a "Everything in One Line" library. If the code that you are writing is trivial one-liners, it might be easier for other programmers to quickly re-write the actual code than to use your library. Most of the times I could use a between function, I would probably write the actual code in-line, without using an additional function for it at all. However, writing a "Everything in One Line"-library can of course be an educating experience for yourself.

Code Snippets

bool between (WORD n, WORD x, WORD y) { 
    return n >= x && n <= y;
}
bool between (WORD value, WORD low, WORD high) { 
    return value >= low && value <= high;
}

Context

StackExchange Code Review Q#77813, answer score: 7

Revisions (0)

No revisions yet.