DEV Community

Peter + AI
Peter + AI

Posted on

Uniface 101: Understanding the `boolean` Data Type πŸ’‘

Hello everyone! πŸ‘‹ If you're working with Rocket Uniface, you know it's a powerful platform for building robust enterprise applications. Sometimes, however, the most fundamental concepts are the ones we need to understand best. Today, we're diving into a core building block of Uniface's ProcScript: the boolean data type.

(Disclaimer: This post was drafted with the assistance of an AI to help explain the official Uniface documentation in a simple way.)

What is a boolean in Uniface?

At its heart, a boolean is a data type used to represent two states: TRUE or FALSE. It's the foundation of all logical operations in your application. Think of it as a simple switch that can be either on or off. In Uniface, this is crucial for controlling application flow with if/else statements, managing flags, or checking the status of an operation.

How to Declare a boolean πŸ“

In ProcScript, you can declare a boolean in two main ways: as a variable within your script or as a parameter for a component's operation.

1. As a Variable:

You declare it directly within a variables block. This is perfect for temporary flags or state management inside your script.

variables
  boolean isCustomerActive
endvariables

; Later in your script...
isCustomerActive = "1" ; Set it to TRUE
Enter fullscreen mode Exit fullscreen mode

2. As a Parameter:

You can define a boolean as an input (IN), output (OUT), or input/output (INOUT) parameter in a params block. This is how you pass true/false values between different parts of your application.

params
  boolean pCreditApproved : INOUT
endparams

; Check the parameter
if (pCreditApproved)
  ; Do something if credit is approved
else
  ; Do something else
endif
Enter fullscreen mode Exit fullscreen mode

The "Truthy" and "Falsy" World of Uniface Booleans πŸ€”

This is where Uniface has a specific and important behavior! Unlike some other languages that are very strict, Uniface is quite flexible in how it interprets values as TRUE or FALSE.

What is considered FALSE? ❌

Uniface will interpret the following values as FALSE:

  • An empty value ("")
  • The number 0
  • The characters F or f (for False)
  • The characters N or n (for No)

What is considered TRUE? βœ…

This is the simple part: everything else is TRUE!

Yes, you read that right. Any value that is not in the "FALSE" list above will be evaluated as TRUE. This includes:

  • The number 1 (the most common way to represent TRUE)
  • Any other number, like -1 or 42
  • The characters T or Y
  • Any non-empty string, like "Hello"

A Practical Example

Let's see this in action. Imagine a script that checks if a user's subscription is active.

variables
  boolean vSubscriptionActive
endvariables

; Let's test different values
vSubscriptionActive = "1"
if (vSubscriptionActive)
  message "Subscription is Active! (Value was 1)"
endif

vSubscriptionActive = "n"
if (!vSubscriptionActive) ; The '!' means 'not'
  message "Subscription is NOT Active. (Value was n)"
endif

vSubscriptionActive = "some_random_text"
if (vSubscriptionActive)
  message "Subscription is surprisingly Active! (Value was 'some_random_text')"
endif

vSubscriptionActive = ""
if (!vSubscriptionActive)
  message "Subscription is NOT Active. (Value was empty)"
endif
Enter fullscreen mode Exit fullscreen mode

Key Takeaway πŸš€

The Uniface boolean type is flexible, but you need to be careful. The rule "everything that is not explicitly false is true" can be a powerful feature but also a source of bugs if you are not aware of it.

My advice? Stick to using 1 for TRUE and 0 for FALSE in your code. This makes your intentions clear and your application more predictable.

Happy coding! πŸ’»

Top comments (0)