# Function “between” for “Everything in One Line” library

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 <stdbool.h>

#ifndef _WINDOWS_H
#include <windows.h>
#endif

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

• For what it's worth, EOL is a common abbreviation of end of line, such as \n in Unix and \r\n in windows... – janos Jan 17 '15 at 16:24
• Exactly. That's the irony plus the relationship between "Every function ends in one line" and "End of Line" – Edenia Jan 17 '15 at 16:26

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.

• It would be slightly better to arrange the comparisons as low <= value && value <= high – janos Jan 17 '15 at 16:21
• Just imagine the phrase "20 lines of 20 useful functions". How isn't that good personal experience? Programmers must not only watch for readability but also stare to eliminate pointless lines and gain the ability to shrink the code. Oh wait.. that's still the "readability" – Edenia Jan 17 '15 at 16:23
• It might be interesting to read but certainly not profitable to use. – itsbruce Jan 17 '15 at 21:13
• @janos That's something I'd consider opinion based, I think value >= low is better, personally. – Simon Forsberg Jan 17 '15 at 22:58

Strictly speaking, you only need to #include <windef.h> here, though including all of <windows.h> is harmless. The #ifndef shouldn't be necessary in either case, as each included header should be responsible for guarding against its own double-inclusion.

The parentheses around the true and false are superfluous. It makes no difference to the compiler and doesn't really improve the readability here. It may be warranted if the expressions were somewhat more complex.

There is no reason to include windows.h at all, as there is nothing Windows-specific in this code (aside from the WORD type, which is not really a good way to do things in this day and age).

Doing such things as macros is dangerous and unnecessary; parameters aren't necessarily single values without side effects. And if it's in a header or if your compiler supports link-time optimization, the compiler can optimize it into an inline anyway. The only thing a macro buys you in this case is type-independence but it's not doing it in a particularly safe way here, and you're specifying the type in the macro so you lose that as well.

You might want to improve the condition to allow for the extents to be in any order, e.g.:

static bool between(int n, int x, int y) {
return (x <= n && n <= y) || (y <= n && n <= x);
}


(static bool is if this implementation is kept in a header, so that non-optimizing compilers won't have multiple instances of the same symbol at link time.)

And, finally, for something so simple and obvious as this, it's not really necessary to put into a library.

between (WORD n, WORD x, WORD y)


In languages that have such a comparison built in, the order of operands would be x <= n <= y. Is there a reason you chose NXY instead of XNY for your parameter order?

• vegasoft.site90.com/graph.png – Edenia Jan 17 '15 at 16:52
• "Is n between x and y?" – Morwenn Jan 17 '15 at 17:39