Language features that you really should know from day 1

Here's a fun test to see how well you know ObjectScript.

What will each of the following statements output?

write 5 * 10 + 1

write 1 + 5 * 10

write 1 + 0 / 10

write 0 && 0 = 0

write 0 = 0 && 0

write 1 && 1 = 1

write 1 = 1 && 1

write 2 && 2 = 2

write 2 = 2 && 2

write "1 APPLE" + "2 BANANAS"

write "-1" + "1-2"

write +"+-+-+-5"

write "1D7P"-1

write "1E6F"-1

write 0 = "FOO"

write 0 = +"FOO"

write 0 = ''"FOO"

write "10X" - " 5" - "5"

write "10-5"

write +"10-5"

write @"10-5"

write 5_"1"-1

write "5"-1_"1"

write 10 + 10 > 21 + 1

Scroll down to see how many you got right...

   \/

   \/

   \/

   \/

   \/

   \/

   \/

   \/

   \/

   \/

   \/

   \/

>write 5 * 10 + 1
51
 
>write 1 + 5 * 10
60

>write 1 + 0 / 10
.1
 
>write 0 && 0 = 0
1
 
>write 0 = 0 && 0
0
 
>write 1 && 1 = 1
1
 
>write 1 = 1 && 1
1
 
>write 2 && 2 = 2
0
 
>write 2 = 2 && 2
1
 
>write "1 APPLE" + "2 BANANAS"
3
 
>write "-1" + "1-2"
0
 
>write +"+-+-+-5"
-5
 
>write "1D7P"-1
0
 
>write "1E6F"-1
999999
 
>write 0 = "FOO"
0
 
>write 0 = +"FOO"
1
 
>write 0 = ''"FOO"
1
 
>write "10X" - " 5" - "5"
5
 
>write "10-5"
10-5

>write +"10-5"
10
 
>write @"10-5"
5

>write 5_"1"-1
50

>write "5"-1_"1"
41

>write 10 + 10 > 21 + 1
1

There are a couple of things going on here that are important to understand, otherwise your logic is going to look like its doing unexpected things.

The first is that ObjectScript implements strict left to right precedence, that means ObjectScript is going to behave completely differently to other languages that you might use.

The second is the automated coercion of strings to numbers, this can be both useful and a trip hazard that you need to be aware of. I've also seen some cool and crazy ideas that leverage on this, but best stay away from doing anything too clever as it can lead to unreadable code.

Precedence and Coercion are the main points here, and if you are new to the language make sure you have a good handle on them. For more detailed information read the documentation here...

https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GCOS_operators

  • + 6
  • 2
  • 547
  • 13

Comments

while this test is indeed fun, the most important phrase in your post is of course:

best stay away from doing anything too clever as it leads to unreadable code

best stay away from doing anything too clever as it can lead to unreadable code

No, as staying away you are getting untrained, so you can loose an ability to read unreadable code written by others very soon wink

Right! And write more and more clever code to train colleagues :-)

Pretty embarassing result for me. I misinterpretted how ObjectScript treats equals(=) operator when used with a write and thus lost a lot of marks.  Its never something I've come across but now I am well and truely informed. Good article. From a support perspective, I often have to evaluate expressions in the terminal to check my understanding is correct. It can be mighty fustrating if you spend all day assuming an expression is interpretted one way, only to find your assumption to be incorrect! Agree with Avoid ambiguous or easily misinterrepted code. Also use brackets, they are your friends.

Yes, good advice, brackets everywhere!

Also, sanitise input data, we wouldn't want "1E6" to accidentally transfer $1,000,000 to the Cayman Islands.

One can only hope that no one write like this.

Or something like this:

I S S S=I

what's the problem ?   <ctrl>e in studio expands it for you 

Thanks, had no idea about <ctrl>e. Now if someone would just change its behaviour so that IT DOESN'T SHOUT AT US that would be really neat.

In Studio if you go to "Options"-> "Editor" -> "Keyword Expansion", you can change whether it uses uppercase, lower case, mixed case or retains the original case when you CTRL+E

Thanks Jolyon for that embarrassingly simple solution. Got my curiosity going though and I can now understand why I didn't know of this feature in the first place - the "Advanced" sub menu doesn't appear on my Edit menu in Studio 2012.1.2 (where I did a lot of my development) though the option for customising Keyword Expansion does appear in the Options. All good in subsequent versions though.