Article
· 4 hr ago 2m read

From "Oops" to "Aha!" - Avoiding Beginner Mistakes in ObjectScript

Starting out with ObjectScript, it is really exciting, but it can also feel a little unusual if you're used to other languages. Many beginners trip over the same hurdles, so here are a few "gotchas" you'll want to watch out for. (Also few friendly tips to avoid them)


NAMING THINGS RANDOMLY

We have all been guilty of naming something Test1 or MyClass just to move on quickly. But once your project grows, these names become a nightmare. 

➡ Pick clear, consistent names from the start. Think of it as leaving breadcrumbs for your future self and your teammates.


MIXING UP GLOBALS AND VARIABLES

Globals (^GlobalName) can be confusing at first. They're not just normal variables. They live in the database and stick around even after your code stops running.

➡ Use them only when you really need persistent data. For anything else, stick with local variable. (This also saves storage.)


FORGETTING TRANSACTIONS

Imagine updating a patient record, and your session crashes halfway. Without a transaction, you are left with half-baked data.

➡ Wrap up important updates in TSTART/TCOMMIT. It is like hitting "save" and "undo" at the same time.


BUILDING SQL IN STRINGS

It is tempting to just throw SQL into strings and execute it. But that quickly gets messy and hard to debug.

➡ Use embedded SQL. It's cleaner, safer and easier to maintain.

EXAMPLE:

❌ Building SQL in Strings

Set id=123
Set sql="SELECT Name, Age FROM Patient WHERE ID="_id
Set rs=##class(%SQL.Statement).%ExecDirect(,sql)

✅ Using Embedded SQL

&SQL(SELECT Name, Age INTO :name, :age FROM Patient WHERE ID=:id)
Write name_" "_age,!

SKIPPING ERROR HANDLING

Nobody likes seeing their app crash with a cryptic message. That usually happens when error handling is ignored.

➡Wrap risky operations in TRY/CATCH and give yourself meaningful error messages.


IGNORING BETTER TOOLS

Yes, the terminal works. But if you only code there, you are missing out.

➡ Use VS Code with the ObjectScript extension. Debugging, autocomplete, and syntax highlighting make life so much easier.


REINVENTING THE WHEEL

New developers often try writing their own logging or JSON handling utilities, not realizing ObjectScript already has built-in solutions.

➡ Explore%Library and dynamic objects before rolling your own.


WRITING "MYSTERY CODE"

We have all thought "I'll remember this later."

⚠️SPOILERYOU WON'T! 

➡ Add short, clear comments. Even a single line explaining why you did something goes a loooong way.


 

FINAL THOUGHTS : )

Learning Objectscript is like learning any other new language. It takes a little patience, and you will make mistakes along the way. The key is to recognize these common traps early and build good habits from the start. That way, instead of fighting the language, you will actually enjoy what it can do. :)

Discussion (4)2
Log in or sign up to continue

FYI, in your "Building SQL In Strings" section, you can also still use %SQL.Statement like this:

set stmt = ##class(%SQL.Statement).%New()
// Note the question mark in the query.
set query = "SELECT Name, Age FROM Patient WHERE ID=?"
set sc = stmt.%Prepare(query)
// You can add error handing here if the above status results in an error
// Providing variables to the %Execute method will insert them where the question marks are in the query, in order
set rs = stmt.%Execute(id)