I have the following code:
subroutine foo(int index)
{
// Check A.
// Critical: Check A must precede Check B below.
if (index == 1)
{
return true;
}
// Check B.
if (index - 2 < 0)
{
return false;
}
return true;
}
The code is a simplified representation of a real-life scenario where I am checking for validity of punctuation marks in a string.
My question: Is there a construct in any language which would guarantee that the order of the two if statements are maintained as is? (Without having to place a comment in the code as I have done and hope that it is heeded.)
For the case where index is 1, if Check B is moved before Check A, Check A will never be caught and foo() will always return false, which is bad.
Again, my concern is for the maintenance of the code by future programmers. There are about 10 if statements in the code, one after another, and their order is important.
EDIT 1:
I am an experienced developer, and I am really asking whether there are any new developments in languages that would allow for what I am asking above. I am sorry that I did not make this clear.
EDIT 2:
In response to comments suggesting index < 2
instead of index - 2
< 0: I don't agree. index - 2
indicates that I am interested if there is an item two locations before the current index, while index < 2
does not convey the same information. (Of course, this is my opinion!)
return variable
, that you then have to track just as much as you would have had to track thereturn
statements. Meanwhile, you introduce potential bugs by not immediately ending execution of the function when that's logically what you need to happen.