2

In many cases you have code like this (C-style pseudo code used):

bool checkCondition();

bool doSomething(){
   if (checkCondition() == false)
      return false;

   // do something
   return true;
}

I keep reusing this patter and every time wonder, if there is better way to express it?

Sometimes, the condition check can be left for the caller or asserted, but often condition check must be done inside the function.

You can get fancy and use exceptions, but result is almost the same code.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Nick
  • 9,962
  • 4
  • 42
  • 80

4 Answers4

2

First I would express the negation like that:

 if (!checkCondition())
     return false;

Also I would prefer to have the positive condition in the if statement, whenever applicable (depending on the length of the blocks):

bool doSomething(){
      if (checkCondition()) {
         // do something
         return true;
      } else {
        return false;
     }
}

You could also drop the else here because of a return in the `if``statement.

bool doSomething(){
      if (checkCondition()) {
         // do something
         return true;
      }
      return false;
}
Dorian Gray
  • 2,913
  • 1
  • 9
  • 25
  • 3
    I think just simple `if (!condition) return false;` is better because you don't need any `else` and other brackets. – sanitizedUser May 09 '20 at 09:50
  • It depends on the length of the "do something" and on your personal taste. – Dorian Gray May 09 '20 at 09:53
  • 2
    @sanitizedUser it depends on how verbose you want to be. Personally I tend to agree with you but I'll usually just go with the flow of whatever codebase im working in. Anyway we're definitely in opinion territory. – George May 09 '20 at 09:54
  • I have added another variant here. I think, with the "Also I would prefer to..." introduction, I made it clear that we are in opinion territory here. A question like `if there is better way to express it?` can only have opinion based answers. It is too vague to get any hard facts, it merely asks for alternatives. – Dorian Gray May 24 '20 at 18:12
0

Your code itself is simple and clean. But below is one more clean variation (avoiding explicit true/false)

bool doSomething(){
   var isConditionTrue = checkCondition();

   if (isConditionTrue) 
   {
       // if executes only in case of 'isConditionTrue = true'
       // do something
   }
   return isConditionTrue; 
}
as-if-i-code
  • 2,103
  • 1
  • 10
  • 19
0

In most cases I prefer the following approach

bool doSomething()
{
    bool success = checkCondition();

    if ( success )
    {
        // do something
    }

    return success;
}

For example consider a function that appends a node to a singly-linked list in C.

struct Node
{
    int data;
    struct Node *next;
};

int append( struct Node **head, int data )
{
    struct Node *new_node = malloc( sizeof( struct Node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = data;
        new_node->next = NULL;

        while ( *head != NULL ) head = &( *head )->next;

        *head = new_node;        
    }

    return success;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You can remove the if condition as below:

return checkCondition();

It is enough. This code is simple enough but if the checkCondition() function is not too large, you can define it as an "inline" function to enhance performance:

inline bool checkCondition() {//your code;}