curly-braces

Is it a bad practice to use an if-statement without curly braces? [closed]

依然范特西╮ 提交于 2019-11-26 00:58:55
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . I\'ve seen code like this: if(statement) do this; else do this; I don\'t like that, I think this is cleaner and more readable if(statement){ do this; }else{ do this; } Is this simply a matter of preference or would one way be recommended over the other? 回答1: The problem with

When do we need curly braces around shell variables?

安稳与你 提交于 2019-11-25 22:32:09
问题 In shell scripts, when do we use {} when expanding variables? For example, I have seen the following: var=10 # Declare variable echo \"${var}\" # One use of the variable echo \"$var\" # Another use of the variable Is there a significant difference, or is it just style? Is one preferred over the other? 回答1: In this particular example, it makes no difference. However, the {} in ${} are useful if you want to expand the variable foo in the string "${foo}bar" since "$foobar" would instead expand

How can I print literal curly-brace characters in python string and also use .format on it?

无人久伴 提交于 2019-11-25 21:47:07
问题 x = \" \\{ Hello \\} {0} \" print x.format(42) gives me : Key Error: Hello\\\\ I want to print the output: {Hello} 42 回答1: You need to double the {{ and }} : >>> x = " {{ Hello }} {0} " >>> print x.format(42) ' { Hello } 42 ' Here's the relevant part of the Python documentation for format string syntax: Format strings contain “replacement fields” surrounded by curly braces {} . Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you

Is it a bad practice to use an if-statement without curly braces? [closed]

别来无恙 提交于 2019-11-25 21:44:14
I've seen code like this: if(statement) do this; else do this; I don't like that, I think this is cleaner and more readable if(statement){ do this; }else{ do this; } Is this simply a matter of preference or would one way be recommended over the other? The problem with the first version is that if you go back and add a second statement to the if or else clauses without remembering to add the curly braces, your code will break in unexpected and amusing ways. Maintainability-wise, it's always smarter to use the second form. EDIT: Ned points this out in the comments, but it's worth linking to here