在C中编写高度为2的决策树的简明方法

时间:2012-01-20 16:38:33

标签: c coding-style

我有一个高度为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;
}

我的问题是代码很冗长。理想情况下,我想在一个声明中声明并设置argumentEditorAnimation

是否有任何聪明的C风格提示处理这样的情况?

2 个答案:

答案 0 :(得分:4)

我认为为了清楚起见,我会尝试将其折叠成一个表达式,但如果你必须:

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

或者,您可以通过在不需要时删除{}来使代码更简洁。

(如果你想编写这些类型的表达式,请考虑使用较短的标识符。长标识符也会使代码变得冗长。例如,将is放在你的布尔值中。)

答案 1 :(得分:2)

有时像这样的逻辑最好用一个小表来表达,你用你的决策标准来索引。

在这种情况下,我会使用良好的格式,因为larsmans在他的回答中表示。