U
U
Urukhayy2015-03-16 16:55:05
Programming
Urukhayy, 2015-03-16 16:55:05

Which option is best?

if(a == 3)    CallMe();
if(b == 4) 
{
    CallMe();
    Giveme();
}

versus
if(a == 3 || b == 4)    CallMe();
if(b == 4) Giveme();

________________________________________________________________________
for(var i = 0, j = GetPool(); i <= j; i++)
    {
        CallFuncTo(i);
    }

versus
for(var i = GetPool(); i >= 0; i--)
    {
        CallFuncTo(i);
    }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2015-03-16
@Urukhayy

What do you think reads better?
for the first example - the second option as more readable. Let the condition be better duplicated than the action on this condition.
On the second - as it is more convenient to you and if the logic allows a reverse of the order.

E
Evgeny Shatunov, 2015-03-16
@MarkusD

For the first block:

switch( b )
{
case 4:
  GiveMe();
case 3:
  CallMe();
  break;
};

In general, this is not a question of optimality. It's a matter of implementation approach. It's easy to come up with a situation in which the developer rakes with all of these approaches, except for one or a couple.
Regarding the second block: I propose to independently assemble the following code in debug and release, and make sure of your doubts yourself.
class A
{
public:
  inline size_t begin()
  {
    printf( "begin();\n" );
    return 0;
  };
  
  inline size_t end()
  {
    printf( "end();\n" );
    return 10;
  };
};

A cont;
for( size_t iter = cont.begin(); cont.end() > iter; ++iter )
{
  printf( "%d\n", iter );
};

But here you always need to understand what expenses it is permissible to go for for the sake of convenience. You should not get carried away with the second variables in the for declaration if the same "end ()" is a lightweight and compiler-optimized construct. In this regard, I advise you to pay much more attention to the increment area in "for".
The postfix form always results in the creation of a temporary variable, the prefix form does not. Prefix forms for loop counters in Google Code Style are marked as good practice.

M
Mrrl, 2015-03-16
@Mrl

In the first case, the options are not equivalent. With a=3, b=4, in the first variant CallMe() will be called twice, and in the second - only once.
The first option is written optimally. To make it behave like the second option (with one call to CallMe()), just insert else:

if(a == 3)    CallMe();
else if(b == 4) {
    CallMe();
    Giveme();
}

This will be optimal in time. If instead of CallMe() there is a more complex call or a long block, and you are concerned about the length of the code (for example, on a microcontroller), then take the second option. If, at the same time, the general condition is more complicated than b==4, then you have to remember it:
bool cond1=(sin(x)>=sin(y));
if(cond1 || cos(x)<cos(y)) Draw(tan(x),tan(y));
if(cond1) CloseLine();

In the second case, the second option is shorter (in code) and slightly faster - the program needs to store one less variable, and there are more opportunities for register optimization. But this is provided that the order of the calls is not important, and that processing the indices in the reverse order will not give a loss compared to the direct one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question