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!)

有帮助吗?

解决方案

Anyone who can edit the source code can remove anything that you might put in there to "protect" them. You can do two things:

  1. Choose a good name for the function that conveys your intention

  2. Provide Unit Tests that will break if someone ever changes what the function does

Both do not prevent future programmers from changing anything if they need to, but they will have some guidance in how the function was intended to behave.

Another option would be to turn these temporal dependencies into structural ones by having the first one generate/return something that the second one requires as input. But that is likely overkill for something as simple as your example. Robert C. Martin mentions this technique in his book "Clean Code".

其他提示

In probably all relevant languages, the statements are processed in order.

And that's valid for the whole program code. Even the simplest programs wouldn't work otherwise.

If someone is allowed to change the code but doesn't understand anything about it, there's not much you can do about it, except removing his rights to do so.

Nothing wrong with a comment in the code if it is hard to understand, or refactoring the code to make its intents more clear, of course.

Edit (as in comment):

The easier understandable your code, the lesser the chances of messing around with it. For example, (index < 2) would be much easier to read and understand for me. And in this case, it's the same as (index < 0). So this function always returns true for positive numbers, so the whole function is as simple as return index > 0.

Make your logic as easy to follow as possible.

And - as another answer has pointed out - this should be captured by a unit test anyway.

So, you're asking how to protect your code from clumsy future devs? That's a tough task which is usually handled by applying code organisation techniques and not language constructs.

In this specific case, I would simply make sure that reorderring the if blocks didn't functionally change my method. Like this:

subroutine foo(int index)
{
    bool checkA = (index == 1);
    bool checkB = (index - 2 < 0);

    if (checkA)
    {
        return true;
    }

    if (!checkA && checkB)
    {
        return false;
    }

    return true;
}

Of course, you could accomplish the same thing by putting CheckB in CheckA's else block, but then we're nesting ifs, which I don't like.

许可以下: CC-BY-SA归因
scroll top