if(condition)
{
//stuff
}
if(condition){
//stuff
}
if (condition) {
// ... some code...
}
else {
// ... some other code
}
if (condition) {
// ... some code...
} else {
// ... some other code...
}
if(condition) {
}
else {
}
Code:if(condition) { } else { }
That's how I do it.
Honestly though - I think indentation is more important than the dang brackets. People put way too much emphasis on bracket styles.
if( true ){
//do something amazing
}
void doSomething()
{
//do something amazing
}
if (condition)
{
// ... some code...
}
else
{
// ... some other code...
}
if (a is 1) then
--do stuff
else
--do something else
end if
Here's a style that always pisses me off:
This is all preference. The code formatter at work puts the open brace on the line with the construct instead of a new line, and that's what I've done for a long time.
-Lee
if (blah == 0)
{
;
}
else if (blah == 1)
{
;
}
else
{
;
}
Code:if (condition) { ; } else { ; }
Crikey! It looks like I'm the only one who does:
Code:if (condition) { // ... some code... } else { // ... some other code... }
I guess it maybe comes from when I started out many, many years ago with on AppleScript where the syntax is enforced as:
Code:if (a is 1) then --do stuff else --do something else end if
...so I just chose a syntax that was as close to what I was used to as possible.
I don't see that it really matters, although I think for the sake of tidiness it's best to choose a style that works for you (or your team) and stick to it.
Not always.. there are a few (marginal) cases where the brace location can have a functional impact (see here).
It's mostly preference alright though. There are some small benefits to either approach. The 'brace on same line' format uses fewer vertical lines meaning you can view more code on a screen at the same time, which is useful. The 'brace on new line' approach is more spaced out, and IMO more readable - particularly when reading code you're reading someone else's code - just as a big monolithic paragraph is less readable than several short, discrete paragraphs.
Personally, the 'brace on same line' approach bugs me (far more than it should!!), as in my head it's putting things together which shouldn't be. Just as it would annoy me (too much) if someone typed:
Hi whooleytoo. Good
day.
How are you? Good
night.
instead of:
Hi whooleytoo.
Good day.
How are you?
Good night.
Always...
Code:if (blah == 0) { ; } else if (blah == 1) { ; } else { ; }
Looks cleaner to me. And it allows braces to match up. I think putting the braces on the same lines 'compresses' the code too much and makes it harder to read. I like the code being a little more 'spread-out'.
if (!goodCondition) return;
if (condition == YES) {
// do stuff
} else {
// do other stuff
}
Crikey! It looks like I'm the only one who does:
Code:if (condition) { // ... some code... } else { // ... some other code... }
I guess it maybe comes from when I started out many, many years ago with on AppleScript where the syntax is enforced as:
Code:if (a is 1) then --do stuff else --do something else end if
...so I just chose a syntax that was as close to what I was used to as possible.
...
{ ... }
if
...
end if
tell
...
end tell
No, you're not the only one. I know a lot of people, including myself, who prefer to put matching braces in the same column.
The AppleScript syntax (which has a lot in common with Ada and similar languages) seems much more readable to me. C-like languages only have one grouping mechanism:
which is used for many different things (bad) and is optional (bad).Code:{ ... }
In contrast, the AppleScript way,
removes much of the inconsistency. "if" ALWAYS needs a matching "end if", and "end if" needs "if". "end tell" needs a matching "tell". In C, "}" needs a matching "{", but you have no idea what the "{" will belong to until you find it.Code:if ... end if tell ... end tell
Very True. But I remember when I started this was also one of my questions. You unsure of yourself as you are starting out.
function (roof,
floor,
cow) {
printit();
}
function (roof) { print("zooo"); }
Or there's the far supperior Python syntax. A block of code will be indented. When the indent ends, you're out of that block of code.
It's ok until you or someone else accidentally change intentation during an edit and change the meaning of the code, if you are lucky it creates an error and you notice it. Since there's no compilation, it will also always be a runtime error.
I try to follow RobotsAndPencil's Style Guide:
Code:if (!goodCondition) return; if (condition == YES) { // do stuff } else { // do other stuff }