The priority when writing code is maintainability / readability. There are a large number of factors that can affect this.
Breaking it down by example:
Example 1:
- Positives:
- named variables: by naming each variable with a readable name, it is easier to understand the intent behind the usage of the variable
- braced structures: It is clear what operations are linked to which control statements
- Negatives:
- "bloat": There are a lot of variables in use. The more there are, the more difficult it is for the reader to keep track. This link (yes, it is wikipedia...) provides either
7
or4
as "cognitive limits", meaning that to try and remember many values as the code is read becomes more difficult.- this means that a reader who needs to check what value a variable represents, or refresh their memory on what has been done to that variable will have to break off to check back
- end results are separated: return value is set in one location, (possibly) modified in another (you have to read and understand each
if
separately to confirm) and returned at the end. While not terrible, it's more to keep track of.
- "bloat": There are a lot of variables in use. The more there are, the more difficult it is for the reader to keep track. This link (yes, it is wikipedia...) provides either
Example 2:
Positives:
- temporary variables are temporary: There is no need to store these values for later use, as they are not going to be used later. Where the return value of a function is used as an argument, the function name is sufficient to identify the use of the value without explicitly naming it
- concise: minimal bloat / higher informational density, there is little boiler-plate around (and amongst) the code, so what remains is more meaningful (yes, this can be taken too far, obfuscating the code. Avoid that)
- clear meaning: The snippet's only job (at a high level) is to return a value, instead of creating more.
Negatives
- reliance on understanding the boolean operators and their precedence
- bracket matching can be a visual issue
I'm sure there are more arguments for and against. These are just those that spring to mind.