public static IEnumerable<StatementSyntax> LopsidedTerminalBranchesToGuardedBranches(IfStatementSyntax syntax) { Contract.Requires(syntax != null); Contract.Requires(syntax.Parent is BlockSyntax); if (syntax.Statement.IsGuaranteedToJumpOut()) return new[] { syntax }; if (syntax.Else != null && syntax.Else.Statement.IsGuaranteedToJumpOut()) return new[] { syntax }; var allowedJump = syntax.TryGetEquivalentJumpAfterStatement(); if (allowedJump == null) return new[] { syntax }; var trueBloat = syntax.Statement.Bloat(); var falseBloat = syntax.Else == null ? 0 : syntax.Else.Statement.Bloat(); if (trueBloat < falseBloat * 2 - 10) { // inline the false branch, guard with the true branch return syntax.Else.Statement.Statements().Prepend( syntax.WithStatement(syntax.Statement.BracedTo(syntax.Statement.Statements().Concat(new[] {allowedJump}))) .WithElse(null)); } if (falseBloat < trueBloat * 2 - 10) { // inline the true branch, guard with the false branch return syntax.Statement.Statements().Prepend( syntax.WithCondition(syntax.Condition.Inverted()) .WithStatement(syntax.Else == null ? allowedJump : syntax.Else.Statement.BracedTo(syntax.Else.Statement.Statements().Concat(new[] {allowedJump}))) .WithElse(null)); } return new[] { syntax }; }