Skip to main content

If you write down one call to getXY()getXY() in your code, and I read your code, then I know what happens: There is one call to getXY()getXY(), and if the result is either xx or yy then DoSomething()DoSomething() gets called.

If you write down two calls to getXY()getXY() in your code, and I read your code, then things are more complicated: There is one call to getXY()getXY() first. If the result is xx, then DoSomething()DoSomething() gets called. Otherwise, there is another call to getXY()getXY(), and if the result is yy, then DoSomething()DoSomething() gets called.

Now without reading the source code for getXY()getXY(), I don't know if calling getXY()getXY() twice has any side effects. For example, getXY()getXY() might increment a static variable and return it. In that case, I can't swap the order of the comparisons with xx and yy. Or xx or yy might be a static variable, and calling getXY()getXY() increases it. It has nothing to do with efficiency, but with code complexity. If one call is enough, use one call.

If you write down one call to getXY() in your code, and I read your code, then I know what happens: There is one call to getXY(), and if the result is either x or y then DoSomething() gets called.

If you write down two calls to getXY() in your code, and I read your code, then things are more complicated: There is one call to getXY() first. If the result is x, then DoSomething() gets called. Otherwise, there is another call to getXY(), and if the result is y, then DoSomething() gets called.

Now without reading the source code for getXY(), I don't know if calling getXY() twice has any side effects. For example, getXY() might increment a static variable and return it. In that case, I can't swap the order of the comparisons with x and y. Or x or y might be a static variable, and calling getXY() increases it. It has nothing to do with efficiency, but with code complexity. If one call is enough, use one call.

If you write down one call to getXY() in your code, and I read your code, then I know what happens: There is one call to getXY(), and if the result is either x or y then DoSomething() gets called.

If you write down two calls to getXY() in your code, and I read your code, then things are more complicated: There is one call to getXY() first. If the result is x, then DoSomething() gets called. Otherwise, there is another call to getXY(), and if the result is y, then DoSomething() gets called.

Now without reading the source code for getXY(), I don't know if calling getXY() twice has any side effects. For example, getXY() might increment a static variable and return it. In that case, I can't swap the order of the comparisons with x and y. Or x or y might be a static variable, and calling getXY() increases it. It has nothing to do with efficiency, but with code complexity. If one call is enough, use one call.

deleted 2 characters in body
Source Link
gnasher729
  • 49.4k
  • 4
  • 71
  • 137

If you write down one call to getXY() in your code, and I read your code, then I know what happens: There is one call to getXY(), and if the result is either x or y then DoSomething() gets called.

If you write down two calls to getXY() in your code, and I read your code, then things are more complicated: There is one call to getXY() first. If the result is x, then DoSomething() gets called. Otherwise, there is another call to getXY(), and if the result is y, then DoSomething() gets called.

Now without reading the source code for getXY(), I don't know if calling getXY() twice has any side effects. For example, getXY() might increment a static variable and increasereturn it. In that case, I can't swap the order of the comparisons with x and y. Or x or y might be a static variable, and calling getXY() increases it. It has nothing to do with efficiency, but with code complexity. If one call is enough, use one call.

If you write down one call to getXY() in your code, and I read your code, then I know what happens: There is one call to getXY(), and if the result is either x or y then DoSomething() gets called.

If you write down two calls to getXY() in your code, and I read your code, then things are more complicated: There is one call to getXY() first. If the result is x, then DoSomething() gets called. Otherwise, there is another call to getXY(), and if the result is y, then DoSomething() gets called.

Now without reading the source code for getXY(), I don't know if calling getXY() twice has any side effects. For example, getXY() might increment a static variable and increase it. In that case, I can't swap the order of the comparisons with x and y. Or x or y might be a static variable, and calling getXY() increases it. It has nothing to do with efficiency, but with code complexity. If one call is enough, use one call.

If you write down one call to getXY() in your code, and I read your code, then I know what happens: There is one call to getXY(), and if the result is either x or y then DoSomething() gets called.

If you write down two calls to getXY() in your code, and I read your code, then things are more complicated: There is one call to getXY() first. If the result is x, then DoSomething() gets called. Otherwise, there is another call to getXY(), and if the result is y, then DoSomething() gets called.

Now without reading the source code for getXY(), I don't know if calling getXY() twice has any side effects. For example, getXY() might increment a static variable and return it. In that case, I can't swap the order of the comparisons with x and y. Or x or y might be a static variable, and calling getXY() increases it. It has nothing to do with efficiency, but with code complexity. If one call is enough, use one call.

Source Link
gnasher729
  • 49.4k
  • 4
  • 71
  • 137

If you write down one call to getXY() in your code, and I read your code, then I know what happens: There is one call to getXY(), and if the result is either x or y then DoSomething() gets called.

If you write down two calls to getXY() in your code, and I read your code, then things are more complicated: There is one call to getXY() first. If the result is x, then DoSomething() gets called. Otherwise, there is another call to getXY(), and if the result is y, then DoSomething() gets called.

Now without reading the source code for getXY(), I don't know if calling getXY() twice has any side effects. For example, getXY() might increment a static variable and increase it. In that case, I can't swap the order of the comparisons with x and y. Or x or y might be a static variable, and calling getXY() increases it. It has nothing to do with efficiency, but with code complexity. If one call is enough, use one call.