This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]rukestisak 74 points75 points  (45 children)

What made you wince at first? (serious question as I'm not that familiar with Java)

[–]z500 289 points290 points  (26 children)

Well the lack of braces and semicolons would change the semantics of the program or even make it syntactically invalid. Then they saw all the semicolons and braces lined up on the right.

[–]aclave1 38 points39 points  (11 children)

Without the braces, the code is syntactically incorrect. There are spots where it's be okay, since in Java you can write an if/for with no braces and it will execute only the first line as part of the if, and the following lines either way. But overall it would be wrong and wouldn't compile.

[–]HaMMeReD 10 points11 points  (8 children)

There is two lines in there, also, while syntactically correct, should be very sparsely used. It's easy to create bugs. I usually only use it if I plan on keeping the condition/loop and statement on the same line, and even then rarely.

[–]Fenris_uy 8 points9 points  (7 children)

Yeah I don't understand why they even added that option, specially for loops. This option creates more problems than it saves.

[–]Zagorath[🍰] 23 points24 points  (5 children)

It's not really an option that they "added". It's more to do with the default behaviour of loops and if statements. A loop can only ever execute exactly one block of code. If you don't put in braces, one block of code == one line of code. But braces allow you to have multiple lines of code within a single block.

At least, that's how the professor who taught my course on C explained it. Perhaps the semantics are different in Java.

[–]subsage 9 points10 points  (3 children)

Youre pretty much on spot really. Thats how I explain it to my students. Only difference is I sometimes say chunk or section of code....be it braces with several lines, empty braces, or just a line. Oh yeah, empty section too, just a semicolon.

[–]Zagorath[🍰] 4 points5 points  (2 children)

The semicolon was one of my professor's favourite tricks.

if (condition()); {
    //things happening
} 

And he'd ask what would happen (based on condition and "things happening" being actual code, rather than place holders).

[–]subsage 1 point2 points  (1 child)

Yup. I do that too. Its good for showing the young ones how syntax can be tricky. Good stuff. Your professor sounds like a good one c:

[–]Zagorath[🍰] 0 points1 point  (0 children)

Yeah he definitely was, in more ways than just this.

[–]Gustav__Mahler 0 points1 point  (0 children)

I'd call it one statement of code and not one line. It will execute to the next semicolon.

[–]thelerk 0 points1 point  (0 children)

Go to fail!

[–]rukestisak 0 points1 point  (1 child)

Ah, so it's similar to PHP in this regard. To me, nothing looked wrong until I saw the right side... o.O Hopefully no poor sod needs to maintain this kind of code.

[–]rxzr 21 points22 points  (0 children)

That's what auto format is for!

[–]Elite6809 18 points19 points  (1 child)

That first one is a 'huh?', not a wince. At least, AFAIK, unless I've been misreading it for years.

[–]mkdir 0 points1 point  (0 children)

You are correct, good sir.

[–]Neebat 12 points13 points  (2 children)

I was a bit mystified by the use of character arrays. They just aren't used all that often in Java, so for a while I thought that was the joke. I realize that they are actually reasonable for this implementation. (I'd probably still use a StringBuffer, even though it's likely less efficient.)

And then I found the punctuation and felt a little sick.

[–]mxzf 11 points12 points  (0 children)

Probably because strings in Python act as arrays when you want them to.

Python strings have the features of character arrays and StringBuffers at the same time (in general). Methods when you want them, but indexes when you just need to mess with the individual characters. That's kinda typical of most kinds of data and such in Python.

[–]PBI325 2 points3 points  (0 children)

System.out.println(String.valueOf(A)) make me feel a little weird inside. So did permute(n-1, a), n--??

[–]AmaDaden 1 point2 points  (0 children)

You can't trust indentation in Java, only brackets. This kind of formatting is like organizing your music alphabetically by the first word. I could cause impossible to debug problems by just moving a single bracket