Way to Conditionally Exclude Code

As a Roblox developer, it is currently too hard to maintain debug and release code separation without manual comment toggling. Consider this function:

local function divide(x: number, y: number): number 
  return x / y
end

This function has a couple of preconditions.

  • Both arguments must be a number.
  • Y cannot be 0.

To make sure any team member (or your future self) does not abuse this function, we can add safety guards during debugging:

local function divide(x: number, y: number): number
  assert(typeof(x) == "number", "X must be a number")
  assert(typeof(y) == "number", "Y must be a number")
  assert(y ~= 0, "Cannot divide by zero")

  return x / y
end

Now, when we pass invalid arguments, they are caught by the assertions, and a detailed error is thrown instead of a (possibly) confusing lua error. Great, however, the function is much slower now due to all of the assertions. These assertions are only really useful during debugging, in release we can assume everything will be correct. There’s no reason to do this in release. However, commenting and uncommenting is tedious, it becomes worse when you have multiple of these. There should be a (zero-cost) way to exclude code based on the current mode (debug, release).

Proposed Solution 1

A possible approach is preprocessor-like behaviour. Now, I’ve already seen people suggest a preprocessor, and while I agree, I don’t think Roblox needs a full preprocessor, it would make scripting unnecessarily complex. To solve this problem, we only need something like #ifdef and #endif preprocessor directives, an entire preprocessor is overkill.

I decided to go with this syntax:

--!region SYMBOL
--!endregion

These are inspired by Visual Studio’s’ #pragma region directives that show or hide a region in the editor window. Instead of introducing a new symbol like % or #, we can extend the already existing set of comment directives. I chose for !region and !endregion, because I thought !ifdef and !endif would imply the existance of a full preprocessor. You can, of course, always give them a better name if you choose to solve it like this. !region would exclude code if SYMBOL is not defined, and !endregion would mark where to stop excluding code.

You could predefine DEBUG in studio, and RELEASE in live games. You don’t even have to allow us to define our own symbols, that’s not necessary for this problem (You could if heavily requested though, if you do, make it a separate window in studio, and preferrably don’t add a !define comment directive).

Proposed Solution 2

For most of my assertions, adding Runservice:IsStudio checks actually makes performance worse, the overhead from checking the environment is apparently much greater than the overhead of my assertions.

Now, I don’t know how you’re checking the environment, but if IsStudio isn’t too complex, I think you should make the optimizer remove or inline if statements using IsStudio if it will always be true or false, so that it has the same effect as Solution 1.

Proposed Solution 3 (Preferred)

Checking a simple global constant is much faster than calling RunService:IsStudio, even though it is theoretically not zero-cost, the overhead is literally not noticeable. The only way I noticed it was not zero-cost was by disabling optimizations entirely, so it is zero-cost in live games.

Because of this, a possible solution could be to simply introduce a new roblox-exclusive global boolean constant, something like debug or studio that is true in studio and false in live games. This is my preferred solution because it, in my opinion, fits the platform the most.

If this issue is addressed, it would improve my development experience because I could maintain debug code without runtime cost or manual comment toggling, which saves me some time.

Thanks in advance for considering these ideas!

Sounds like a good idea. And if you need to check for specific instances, then you’re wasting even more time having to do index or name calls on instances with IsA or ClassName, and it starts to add up when you need to call functions multiple times.

I would love a feature like this. I tried to make a plugin that did something like this but the APIs just aren’t there to do some of this stuff.