I would like to know, if it is a good idea to inline small, private functions?
These functions, in my case, are only there for readability purposes and I know, that they are only called a few times, so the bigger bytecode size is insignificant.
I know, that the performance boost may also be insignificant, because I am not passing functional types (and the compiler actually warns me about that), but lets assume that it is a hot spot in our application.
Actual example:
I hava a theoretically infinite tape of symbols (like the one of a turing machine) which is modeled by tow arrays (left and right for positions < 0 and positions >= 0, respectively). Now I have read and write operations which are assumed to be called a lot.
In java I had:
/**
* @param cell the cell
* @return the symbol at the specified cell
*/
public char read(int cell) {
char[] tape;
if (cell < 0) {
tape = left;
cell = -cell - 1;
} else {
tape = right;
}
return cell < tape.length ? tape[cell] : blank;
}
/**
* Writes a symbol to the specified cell.
* @param c the symbol
* @param cell the cell
*/
public void write(char c, int cell) {
char[] tape;
if (cell < 0) {
cell = -cell - 1;
if (cell >= left.length) left = expandArray(left, cell, blank);
tape = left;
} else {
if (cell >= right.length) right = expandArray(right, cell, blank);
tape = right;
}
tape[cell] = c;
}
Now I wanted to translate the snippet to kotlin, read about inline functions and come up with this:
fun read(cell: Int = headPosition) = when {
cell < 0 -> read(left, -cell - 1)
else -> read(right, cell)
}
private inline fun read(tape: CharArray, cell: Int): Char {
return if (cell < tape.size) tape[cell] else blank
}
fun write(c: Char, cell: Int = headPosition) = when {
cell < 0 -> left = write(c, left, -cell - 1)
else -> right = write(c, right, cell)
}
private inline fun write(c: Char, tape: CharArray, cell: Int): CharArray = when {
cell >= tape.size -> expandArray(tape, cell, blank)
else -> tape
}.also { it[cell] = c }
I personally think, especially the read functions, are easy to read.
So is this a good idea and can I ignore the warning of the IDE? Or am I missing something? Maybe there is a best practice or other pattern to write these functions without repeading (almost) the same lines twice (for position < 0 and position >= 0).