Blog

Introducing numeric placeholders in BoxLang source!

Maria Jose Herrera September 18, 2024

Spread the word

Maria Jose Herrera

September 18, 2024

Spread the word


Share your thoughts

Introducing Numeric Placeholders in BoxLang

We're excited to announce a new addition to our BoxLang parser—a feature that draws inspiration from several other languages but fits naturally within BoxLang. The conversation around which features from other languages make sense to include in BoxLang is ongoing. Just because a feature works in language X doesn't mean it will serve a purpose or feel idiomatic in BoxLang. However, in this case, the utility and readability benefits are clear.

What Are Numeric Placeholders?

Numeric placeholders allow you to insert underscore characters (_) inside numeric literals to make large numbers more readable. For example, consider this number:

n = 1000000000

Is that 1 billion? Or 100 million? It takes a moment to decipher. With numeric placeholders, your code becomes more readable:

n = 1_000_000_000

Ah, it's 1 billion! Much clearer, right? There's no strict rule on where you can place the underscores, as long as they are inside the number and not leading or trailing. Even this, though a bit excessive, is valid:

n = 1_0_0_0_0_0_0_0_0_0

Where Can You Use Numeric Placeholders?

Numeric placeholders aren't just limited to integers. You can also use them in:

  • Decimals:

    n = 3.141_592_653_59
    
  • Scientific notation:

    n = 1e2_345
    

These underscores are purely for readability in your source code. At compile time, they are simply removed, meaning they don't affect the bytecode and won't appear anywhere in your running application.

Borrowed From the Best

This feature isn't entirely new to the programming world. It’s borrowed from languages like Ruby, Java, Swift, Kotlin, and JavaScript, where it has proven to be a valuable tool for writing more readable code.

Ruby Example:

puts 1_000_000 * 9
# Outputs: 9000000

This is functionally identical to:

puts 1000000 * 9
# Outputs: 9000000

But the first example is much more readable and developer-friendly.

JavaScript Example:

1_000_000 * 9;
// Outputs: 9000000

How It Works in BoxLang

Here are some examples of valid uses of numeric placeholders in BoxLang:

result1 = 5_000         // 5000
result2 = 5_000.000_4   // 5000.0004
result3 = .1_2          // .12
result4 = 1.2_3         // 1.23
result5 = 1_2.3_4e5_6   // 12.34e56
result6 = 1_2_3_4_5_6_8 // 1234568

You can even use numbers as struct keys or in dot notation:

str = {
    1_0 : "brad"
}
result7 = str[ 10 ]     // "brad"
result8 = str.1_0       // "brad"

str2 = {
    '1_0' : "brad"
}
result9 = str2[ '1_0' ] // "brad"

What’s Not Allowed

There are a few invalid cases where an exception will be thrown:

result1 = _5  // Invalid: variable _5 not found
result2 = 5_  // Invalid: Identifier name cannot start with a number

Since underscores can be valid identifiers in BoxLang, the parser might interpret _5 as a variable rather than a numeric literal.

Conclusion

We hope this new feature will make your code easier to read and maintain. Numeric placeholders are a small but meaningful addition that aligns with BoxLang's goals of improving developer productivity and code clarity. As always, we're open to your feedback as we continue to enhance BoxLang with features that truly make a difference.

Add Your Comment

Recent Entries

VS Code BoxLang Extension: Debugging BoxLang in CommandBox

VS Code BoxLang Extension: Debugging BoxLang in CommandBox

So you’ve installed CommandBox and are running the latest BoxLang server like a boss. You open up your browser and are met with an error message. This looks like a job for, you guessed it, the BoxLang VS Code Debugger!

Maria Jose Herrera
Maria Jose Herrera
September 26, 2024
Top ColdFusion security vulnerabilities in 2024 and how to prevent them

Top ColdFusion security vulnerabilities in 2024 and how to prevent them

ColdFusion continues to be a powerful platform for web application development. However, with its widespread use, it's crucial to remain vigilant about potential security vulnerabilities. This article provides a technical overview of the most relevant security threats affecting ColdFusion (CFML or Adobe ColdFusion) in 2024 and offers practical steps to mitigate them.

1. SQL Injection Vulnerabilities SQL Injection remains one of the most common and dangerous vulnerabilities in C...

Cristobal Escobar
Cristobal Escobar
September 24, 2024
BoxLang 1.0.0 Beta 15 Launched

BoxLang 1.0.0 Beta 15 Launched

Welcome to Beta 15! This release brings several significant enhancements aimed at improving the efficiency and functionality of the CLI environment and continued bug fixing to bring our CFML compatibility to several client applications and Ortus Libraries. Key among these is the introduction of the bx-web-support module, which allows the CLI to interface with web server capabilities, making it an excellent tool for testing and feature auditing. Additionally, the merge capabilities within module settings have been refined to support deep merges from the configuration file, ensuring greater flexibility and control.

Luis Majano
Luis Majano
September 20, 2024