简体   繁体   中英

Concise way to write decision tree with height 2 in C

I have a decision tree of height 2:

const BOOL isVerticalAnimationRequired = YES;
const BOOL isArgumentEditorExpanded = YES;
const BOOL isSelectionLeftToRight = YES;

UITableViewRowAnimation argumentEditorAnimation;
if (isVerticalAnimationRequired)
{
    argumentEditorAnimation = (isArgumentEditorExpanded) ? UITableViewRowAnimationTop :     UITableViewRowAnimationBottom;
}
else
{
    argumentEditorAnimation = (isSelectionLeftToRight) ? UITableViewRowAnimationLeft : UITableViewRowAnimationRight;
}

My problem is that the code is verbose. Ideally I would like to declare and set argumentEditorAnimation in one statement.

Are there any clever C style tips for handling situation like this?

I think I would not try to fold this into one expression for reasons of clarity, but if you must:

argumentEditorAnimation =
  isVerticalAnimationRequired ?
      (isArgumentEditorExpanded ?
           UITableViewRowAnimationTop
        :  UITableViewRowAnimationBottom)
    : (isSelectionLeftToRight ?
           UITableViewRowAnimationLeft
         : UITableViewRowAnimationRight);

Alternatively, you can make your code more concise by dropping the {} when they're not needed.

(Consider using shorter identifiers if you want to write these kinds of expressions. The long identifiers make the code verbose as well. Eg, drop the is in your booleans.)

Sometimes logic like this can best be expressed with a small table that you index with your decision criteria.

In this case, I would just use good formatting as larsmans showed in his answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM