curly-braces

What's the purpose of using braces (i.e. {}) for a single-line if or loop?

南楼画角 提交于 2019-11-26 21:15:03
I'm reading some lecture notes of my C++ lecturer and he wrote the following: Use Indentation // OK Never rely on operator precedence - Always use parentheses // OK Always use a { } block - even for a single line // not OK , why ??? Const object on left side of comparison // OK Use unsigned for variables that are >= 0 // nice trick Set Pointer to NULL after deletion - Double delete protection // not bad The 3rd technique is not clear to me: what would I gain by placing one line in a { ... } ? For example, take this weird code: int j = 0; for (int i = 0 ; i < 100 ; ++i) { if (i % 2 == 0) { j++;

What do curly braces in Java mean by themselves?

て烟熏妆下的殇ゞ 提交于 2019-11-26 18:33:16
I have some Java code that uses curly braces in two ways // Curly braces attached to an 'if' statement: if(node.getId() != null) { node.getId().apply(this); } // Curly braces by themselves: { List<PExp> copy = new ArrayList<PExp>(node.getArgs()); for(PExp e : copy) { e.apply(this); } } outAMethodExp(node); What do those stand-alone curly braces after the first if statement mean? The only purpose of the extra braces is to provide scope-limit. The List<PExp> copy will only exist within those braces, and will have no scope outside of them. If this is generated code, I assume the code-generator

Go to Matching Brace in Visual Studio?

人走茶凉 提交于 2019-11-26 16:51:41
Is there a way in Visual Studio 2008 to go from a closing brace to its opening brace? I've found a fair amount of stuff about highlighting the brace, but nothing about moving the cursor to it. (VB.NET version of this Question: Keyboard shortcut for Jumping between "If/End If" ) Tim Santeford I found this for you: Jump between braces in Visual Studio Put your cursor before or after the brace (your choice) and then press CTRL + ] . It works with parentheses ( ), brackets [ ] and braces { }. From now on you don’t need to play Where’s Waldo? to find that brace. Use CTRL + ] to switch between them.

Should I use curly brackets or concatenate variables within strings?

陌路散爱 提交于 2019-11-26 16:10:56
Straight forward question: Is there an advantage or disadvantage to concatenating variables within strings or using curly braces instead? Concatenated: $greeting = "Welcome, ".$name."!"; Curly braces: $greeting = "Welcome, {$name}!"; Personally, I've always concatenated my strings because I use UEStudio and it highlights PHP variables a different color when concatenated. However, when the variable is not broken out, it does not. It just makes it easier for my eyes to find PHP variables in long strings, etc. EDIT: People are confusing this about being about SQL. This is not what this question

Is there a difference in removing the curly braces from If statements in java

本小妞迷上赌 提交于 2019-11-26 14:42:48
While watching thenewbostons tutorial on basic java, he teaches us to do if statements like this.(Notice the curly Braces) if("pie"== "pie"){ System.out.println("Hurrah!"); } So I tried removing the curly braces if("pie"== "pie") System.out.println("Hurrah!"); And it still works! Since I'm new to java, I don't know why that works. And I want to know if removing(or adding) the curly braces give any benefits/disadvantages. Habib For a single statement it will remain same, but if you want to group more than one statement in the if block then you have to use curly braces. if("pie"== "pie"){ System

What Is a Curly-Brace Enclosed List If Not an intializer_list?

寵の児 提交于 2019-11-26 14:27:09
问题 I asked a question here: Lifetime Extension of a initializer_list return involving the non-functional code: const auto foo = [](const auto& a, const auto& b, const auto& c) { return {a, b, c}; }; I believed the lambda was trying to return an intializer_list (that's bad, don't do that.) But I got a comment: It's not an initializer_list , it's an initializer list. Two different things. I just thought that any time you did a curly-braced list you were creating an intializer_list . If that's not

PHP curly brace syntax for member variable

这一生的挚爱 提交于 2019-11-26 14:20:23
First question on SO and it's a real RTM candidate. But I promise you I've looked and can't seem to find it. I'll happily do a #headpalm when it turns out to be a simple thing that I missed. Trying to figure out Zend Framework and came across the following syntax: $this->_session->{'user_id'} I have never seen the curly braces syntax used to access what appears to be a member variable. How is it different than $this->_session->user_id I'm assuming that the _session is irrelevant, but including it in the question since it may not be. Are the curly braces just a cleanliness convention that

Curly braces inside JavaScript parameters for functions

别说谁变了你拦得住时间么 提交于 2019-11-26 12:11:27
问题 What do the curly braces surrounding JavaScript parameters for functions do? var port = chrome.extension.connect({name: \"testing\"}); port.postMessage({found: (count != undefined)}); 回答1: The curly braces denote an object literal. It is a way of sending key/value pairs of data. So this: var obj = {name: "testing"}; Is used like this to access the data. obj.name; // gives you "testing" You can give the object several comma separated key/value pairs, as long as the keys are unique. var obj =

What is the meaning of the ${0##…} syntax with variable, braces and hash character in bash?

风流意气都作罢 提交于 2019-11-26 12:06:41
问题 I just saw some code in bash that I didn\'t quite understand. Being the newbie bash scripter, I\'m not sure what\'s going on. echo ${0##/*} echo ${0} I don\'t really see a difference in output in these two commands (prints the script name). Is that # just a comment? And what\'s with the /* . If it is a comment, how come it doesn\'t interfere with the closing } brace? Can anyone give me some insight into this syntax? 回答1: See the section on Substring removal in the Advanced Bash-Scripting

Curly Brackets in Arrow Functions

别来无恙 提交于 2019-11-26 11:55:42
can someone, please explain the following: I'm following Dan Abramov's lectures & doing the exercises. The code works fine, however, the tests fail when the following particular function is written with curly brackets **{ }** . case 'toggleTodo' : return ( state.map( (one) => { oneTodo( one, action ) }) ); The same code works fine without curly brackets. case 'toggleTodo' : return ( state.map( (one) => oneTodo( one, action ) ) ); Here is the JsBin . Please refer to line 31 onwards. case 'toggleTodo' : return ( state.map( (one) => oneTodo( one, action ) ) ); is equal to: case 'toggleTodo' :