Separate conditional expressions - why is the template not used for other blocks of code?

I just thought I'd see if anyone could explain why Anders decided it really ...

if(...) //single statement else ///single statement 

But it is not...

 try //single statement catch //single statement 
+7
source share
2 answers

A quote from the Wireframe Design Guide in the General Style Conventions section talks about braces:

AVOID dropping curly braces, even if the language allows it. Brackets should not be considered optional. Even for single blocks, you should use curly braces. This code increases readability and maintainability.

There are very limited cases where lowering braces may be acceptable, for example, when adding a new operator after an existing singing line, the expression is either impossible or incredibly rare. For example, it makes no sense to add an expression after throw :

if(someExpression) throw new ArgumentOutOfRangeExcetion(...);

Another exception to the rule is braces in the case of statements. These braces can be omitted as case and break statements indicate the beginning and the beginning of a block.

What Anders considers subjective and reasoned is a recommendation.

You can also see the section on committing in a conditional encoder in msdn.

+7
source

Probably because single-word conditional expressions are historically valid in curly binding languages, but other patterns are not.

Since any of the examples makes the code less readable at all, there is no good reason for introducing support for one operator further than historically necessary.

If you create extended single-operator support for a large number of blocks of code, you can easily see that someone is writing completely unreadable code. Personally, I would also have avoided the first thing.

+1
source

All Articles