Seemingly pointless extra characters

I am recently learning ‘real’ programming langauges, such as Javascript. Previously I learned a ‘fake’ simplified programming language called DM which is for making 2d ‘byond’ games, and got really good at it. The syntax for ‘DM’ is very simple, much like Python.

So I have a question about Javascript and other ‘curly bracket’ languages.

Here is some code in the standard style that people are ‘supposed’ to write their code in.

function OnGUI(){
	if(GUI.Button(Rect(25,50,100,50),"Lol Button")){
		print("Button pressed");
		}
	}

And likewise, this condensed version works:

function OnGUI(){
	if(GUI.Button(Rect(25,50,100,50),"Lol Button")){
		print("Button pressed");}}

But why isn’t this acceptable?

function OnGUI()
	if(GUI.Button(Rect(25,50,100,50),"Lol Button"))
		print("Button pressed")

I don’t understand the logic, it seems the curly braces and semicolons serve no purpose. Sure, semicolons can be used to place lots of code on 1 line, but otherwise shouldn’t the editor (or whatever) know that the line has ended?

Almost everyone tabs their code anyway, which is supposed to be for the same purpose, so isn’t having both redundant?

I’m just trying to understand this since I’m new to programming. It just seems like the curly brackets and semicolons serve no purpose most of the time. Maybe there is a good reason that I don’t yet understand. But right now they annoy me.

Not requiring brackets and semicolons would reduce that code from 91 to 86 characters, not even counting what you save from shift key presses to type curly brackets. That decreases typing time by like 6%.

Thanks.

The ‘DM’ programming language is ‘tab-based’ and doesn’t require curly brackets or semicolons (although they still work if a person chooses to use them) so I don’t understand why, if a ‘fake’ programming language could accomplish that, a ‘real’ one hasn’t?

It is acceptable, if you put a semicolon at the end and braces for the function. You can leave out braces if there’s just one statement with things like “if”.

No. You may prefer to use spacing to clarify code:

if (somethingThatIsReallyLong 
    somethingElseThatIsReallyLong 
    yetAnotherLongThing) {

It would be kind of bad if having line endings there actually made the editor think the line was ended, because then you’d have syntax errors.

But if you want to do away with the braces and semicolons, use Boo.

–Eric

There is a language called Python built on exactly this concept. It’s called “Boo” in Unity, you might want to check it out.

Personally, I like the curly braces, because they give a clear definition of where something ends, and, depending on your IDE, where it begins. It also lets you write handy one-line statements if you don’t feel like newlines.

The ‘curly bracket’ enclose block of sentences, except when you only have one line of code, in which case are nonsense.

The compiler undestands that a line has ended when it finds a semicolon… and, of course, understands that a block (at last, a couple of lines) has ended when finds the ‘curly bracket’ (this one: }).

greets

the curly brackets are used so you can ignore white spacing and create blocks easyer, its more flexible this way.

also with your second “condensed” version where you have the two }} next to each other, you should avoid doing this as it may cause confusion and also if done as a habbit of closing the block with as many } untill they turn red then it will start merged blocks and causing your script to fail, its always good to set them out right for example (picture below)

hope this makes it more clear, and doesn’t confuse you more :stuck_out_tongue:

as for ; they are used to end the line of code, for instance you wouldnt have one at the end of an if state ment as you dont want to terminate it before it excutes the block within.