Quantcast
Channel: User Baldrickk - Software Engineering Stack Exchange
Viewing all articles
Browse latest Browse all 21

Coding style: Binary logic or multiple if()s?

$
0
0

Something that often comes up in code reviews is the structure of the code, particularly related to the use of control structures, where individuals can be fairly opinionated on what is "right" and what is "wrong".

I have my own opinions on the subject, but at the moment, that's just what it is.

The code snippets below are as an example, and are from a recent discussion. I prefer one of these, and my colleague prefers the other.

Example 1:

  enum {    failed_conn = 0,    made_connection = 1  };  enum {    is_not_in_set = 0,    is_in_set = 1  };  int status = Status_Failed;  in_set = FD_ISSET(socket_desc, &fdset);  if (in_set) {    sock_opt_read_result = getsockopt(socket_desc, SOL_SOCKET, SO_ERROR, &result, &result_len);    if (sock_opt_read_result >= 0) {      connection_to_target = (result == ECONNSUCCESS) || (result == ECONNREFUSED);      if (connection_to_target == 1) {        status = Status_OK;      }    }    return status;  }

Example 2:

  return ((FD_ISSET(socket_desc, &fdset))                                            /* Select returned the file descriptor (socket connected or failed) */   && (getsockopt(socket_desc, SOL_SOCKET, SO_ERROR, &result, &result_len) >= 0) /* We can get the error code (if any) from the connect call */&& ((result == ECONNSUCCESS) || (result == ECONNREFUSED))) ? Status_OK        /* The connect() call succeded or was refused by target */                                                                  : Status_Failed;   /* If any of the above are not true, we failed to connect */

These are just as an example of structure. For those of you who are worried about side effects (e.g. the result variable) don't be, none of the variables in use here are used elsewhere.

What are the (if any) hard (not opinion based - I have enough of those already) arguments for coding in either of these styles, that might help cement a coding style that can be generally agreed upon, either one style, the other, or some other form?


Viewing all articles
Browse latest Browse all 21

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>