Should we create a new constant or modify an existing one

Posted on


I was fixing a bug in the code and it was something like this:

int const ROW_LOC = 1;
Foo ( x , y , ROW_LOC ) ;

so it was passing that constant to a method … and this ROW_LOC constant is being used in multiple other places in the code too.

For ONLY ONE place in the code because this constant was getting passed to some method that was working with a zero-based index I did like this:

SomeOtherFooMethd ( x , str , ROW_LOC -1 );

So my question is: Do I need to create a separate constant with value Zero for this one method call or you would continue using the same ROW_LOC and like I did just decremneting it by 1 in this case?


It depends on the meaning of this constant. If the value ROW_LOC - 1 has logical relation to ROW_LOC (e.g. it means that it should go right before ROW_LOC in some list) then just use ROW_LOC - 1 there, but if it’s just 0 – then either define a new constant or just use 0 as value, depending on situation.

What I would suggest is to rename ROW_LOC to more meaningful (descriptive) name unless it’s a well-known acronym in your company.

Leave a Reply

Your email address will not be published. Required fields are marked *