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

The Hidden Costs of In-House Database Management

The Hidden Costs of In-House Database Management

The Hidden Costs of In-House Database Management


Opting for in-house database management involves more than just a salary. Here are some often-overlooked costs associated with maintaining your own DBA team.



1. High Salaries and Benefits


Hiring skilled DBAs is expensive. According to industry reports, the average salary of a DBA in the U.S. can range from $85,000 to over $130,000 per year, depending on experience and expertise. When you add ...

Cristobal Escobar
Cristobal Escobar
November 20, 2024
5 Signs It’s Time to Modernize Your ColdFusion / CFML Application

5 Signs It’s Time to Modernize Your ColdFusion / CFML Application

ColdFusion has long been a reliable platform for building web applications, but like any technology, it requires maintenance and modernization over time. Whether you're using Lucee or Adobe ColdFusion, it’s critical to recognize the signs that your application is no longer meeting today’s standards in performance, security, and scalability. Let’s explore five clear indicators that it’s time to modernize your ColdFusion application and how ColdFusion consulting can help breathe new life into y...

Cristobal Escobar
Cristobal Escobar
November 19, 2024
ColdBox Free Tip 5 - Building Named Routes with a Struct

ColdBox Free Tip 5 - Building Named Routes with a Struct

**Did you know ColdBox provides flexible ways to build routes using structs?** In this tip, we’ll cover how to use the `event.buildLink()` and `event.route()` methods for named routes, a feature that’s especially handy when working with dynamic URLs.

Maria Jose Herrera
Maria Jose Herrera
November 19, 2024