Using { } brackets correctly
Posted by Parasite-FT-
Saturday, April 17, 2004
Description: a short guide on how to use { } brackets

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.

Anyway, I hope that covers it, though I have a feeling I didn't make any sense; if you have any questions contact me in #script and I'll be glad to help.

  Cheers,
- Para



good job
Posted by madewokherd
Sunday, April 18, 2004 07:12am PDT
I thought you summed it up pretty well. Just one thing I have to add. The reason people like to put { } brackets after if statements and such is because something like this
if (foo) bar | baz
will not count baz as inside the if block. That is, mIRC will do /baz whether foo is true or not.
But if you use { brackets } in this situation, you will never have to worry about such a thing.
if (foo) { bar | baz }
This clearly indicates that both bar and baz are inside the if block. It's much easier to always use brackets than to figure out these sorts of rules and decide how they will apply in any situation.
Submit a comment
Oops! You need to login or register before you can post a comment!

ebaum's world