Q:

How to replace duplicated index-calculating formula

In my code, I often use a formula that returns me an index into a 2D array:
cells[cellToMarkX + cellToMarkY * xSize] 
// ...
if (cells[j + i * xSize] == 0)
//...
else if (cells[j + i * xSize] >= 5)
//..
cells[cellToMarkX + cellToMarkY * xSize] += 4;

I'm not sure if I should instead create some function like
getCell(int x, int y)

or should is this unnecessary and I should prefer to use a macro?

A:

Particularly in the example you give, you might want to consider precomputing as well, as in:
int myindex = j + i * xSize;
if (cells[myindex] == 0)
....
else if (cells[myindex] >= 5)

but to answer your question more directly, a function call is not out of place here, particularly if you declare it with the inline attribute. In many cases, a good optimizing compiler will generate the same code regardless of whether you use a macro or a function, but that is of course implementation-dependent.
Personally, I'd prefer a function.

