Jump to content

Using { } Brackets Correctly


Guest X-Fusion

Recommended Posts

Guest X-Fusion

I've noticed lately that many people have been unsure of how to use brackets, so I thought I'd whip up a short little explanation on their purpose and their use.

 

Simply put, {} brackets are designed to turn a 1 line script into multiple lines. {} brackets aren't neccessary unless you are performing multiple commands, however many scripters feel the need to use them regardless, which is probably a good habit forming procedure. Like many things, an explanation is best made through example.

 

Note 1: Every opening bracket { requires a corresponding } bracket. If you do not have an equal number of opening { and closing } brackets then you will have a bracket mismatch and all subsequent scripts will probably be thrown into error.

 

It is usually easy to detect when you have a bracket mismatch - the indenting in your remote editor (alt+r) is thrown out of alignment when you have a bracket mismatch; all "alias name {"'s and corresponding closing } brackets should be neatly tucked against the left side with no indentation, everything inside it should be indented AT LEAST one tab. You'll pick it up as you go.

 

 

Note 2: Aliases should always be enclosed with {} brackets. Anything within the opening and closing {} brackets belong to that alias, and will run only when the alias is run.

 

alias helloworld {
  say Hello 
  say World
}

 

Note 3: If, elseif, and else statements should be enclosed in {} brackets. Anything within the opening and closing {} brackets belong to that IF block and will only be run if that if condition proves true.

 

alias helloworld {
 if (1 == 1) {
     say Hello
     say World
 }
 else {
     say 1 != 1? wth?
 }
}

 

Note 4: You MAY put commands on one line instead of spanning several lines. You may also put pipes | between each command if you have multiple commands.

 

alias helloworld {
 if (1 == 1) { say Hello | say World }
 else { say 1 != 1? wth? }
}

 

^ the use of pipes | to separate commands is messy, and should be avoided - it is best to let multiple commands span multiple lines.

 

Note 5: You don't HAVE to use {} brackets on single command lines. This is where a lot of people get confused, and most likely because I'm a confusing person (why am I writing a tutorial?). Often people will include brackets even if it is just one line because they feel it's good scripting habit. I am of the other category.

 

alias helloworld {
 if (1 == 1) { 
    say Hello 
    say World 
 }
 elseif (2 == 2) say Two does equal Two, however this line will never be reached because 1 does equal 1
 else say 1 != 1? wth?
}

 

^notice the lack of {} brackets surrounding the elseif and else conditions - I could have just as easily put them in, though I chose to leave them out. I only mention this possibility because you are likely to encounter it somewhere, but it is up to you to decide which style you prefer. I will show the opposite method for inspection:

 

alias helloworld {
 if (1 == 1) { 
    say Hello 
    say World 
 }
 elseif (2 == 2) { say Two does equal Two, however this line will never be reached because 1 does equal 1 }
 else { say 1 != 1? wth? }
}

 

Aliases, Events, and /IF commands are the only three things which will need {} brackets to enclose them - other commands will definitely not need them (there are some exceptions, but if I mention them it will only cloud the water.)

 

To let it sink in I think I'll show some examples of what NOT to do:

 

DO NOT have a { bracket by itself on a line, it will only cause a mismatch

 

alias helloworld {
  if (1 == 1)
  {
      say hello
      say world
  }
}

 

DO NOT proceed every command with a }, ONLY the three things previously mentioned need to be enclosed with {} brackets

 

 

alias helloworld {
  { say hello }
  { say world }
}

 

^ some of us laugh and wonder who would ever do such a thing, but I promise you that this is a rather common error.

 

DO NOT solely rely on the remote editor to tell you if you have a bracket mismatch - it only counts the number of {}'s, it doesn't take into account it's location or use.

 

 

alias helloworld {
 if (1 == 1) { say hello world 
   else say 1 != 1? wth? 
 }
 {
   say wow, this is shitty code
 }
}

 

^ the remote editor won't throw an error on this, nor will the indenting appear off, as far as the remote is concerned that code is peachy-keen.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...