Vyper Booleans (Usage + Code Examples)

Whiteboard Crypto
on

A Boolean is a value-type variable in the Vyper programming language which stores a binary value of 1 or 0. Similarly, we think of the variable being either true or false. Here’s an example:

```@public
def toggle_state(state: bool) -> bool:
"""
Return the opposite state.
:param state: The original boolean state
:return: The opposite state
"""
return not state

@public
def logical_operations(x: bool, y: bool) -> (bool, bool, bool, bool):
"""
Perform logical AND, OR, and NOT operations on two booleans.
:param x: The first boolean value
:param y: The second boolean value
:return: A tuple containing the results of AND, OR, NOT (x), and NOT (y) operations
"""
and_result: bool = x and y
or_result: bool = x or y
not_x: bool = not x
not_y: bool = not y
return (and_result, or_result, not_x, not_y)
```
Contents

Boolean Function

Let’s start with the basics. The Boolean type, denoted by the `bool` keyword in Vyper, is the simplest data type and is used to represent truth values.

In computing, and Vyper is no exception, a Boolean variable can only hold one of two values: `True` or `False`. These two values are the cornerstone of logical statements and control structures, dictating the flow of your smart contracts.

Consider this simple Vyper smart contract that emulates a light switch:

```@public
light_switch: bool

@external
def get_switch() -> bool:
"""
Return the current state of the light switch.
"""
return self.light_switch

@external
def change_switch():
"""
Toggle the state of the light switch.
"""
self.light_switch = not self.light_switch
```

In this contract, `light_switch` exemplifies a Boolean in action, being toggled on and off by the change_switch function.

Booleans with Operators

Booleans in Vyper come with operator tools for crafting logical expressions:

• `not` flips the truth value
• `and` requires both values to be True for the result to be True
• `or` requires at least one value to be True for the result to be True
• `==` checks if two values are equal
• `!=` checks if two values are different

These aren’t just theoretical concepts — they are the decision-makers in your Vyper smart contracts.

Truth with Short-Circuiting

Vyper’s Boolean or and and operators exhibit short-circuiting behavior. This means once an “if” statement returns true, the rest of the code in the conditional is skipped. This performance feature and safety mechanism ensures your logical expressions are as efficient as possible. To highlight this behavior, consider an eligibility check based on age:

```@public
is_eligible: bool

@external
def check_eligibility(age: uint256) -> bool:
"""
Determine eligibility based on age.
"""
if age >= 18:
return True
else:
return False

@external
def update_eligibility(age: uint256):
"""
Update the stored eligibility status based on the given age.
"""
self.is_eligible = self.check_eligibility(age)
```

In this example, we use a condition within check_eligibility and update our state variable is_eligible based on the result, illustrating the use of Booleans for logical decision-making.

Best Practices

When using Booleans in Vyper:

• Choose descriptive variable names, like `is_eligible` or `has_balance`.
• Be explicit; `if is_verified:` is clearer than `if is_verified == True:`.
• Utilize short-circuiting for efficient code; consider the order of your logical operations.

Remember, Booleans are passed by value and immutable, so reassignments won’t affect the original variable.

Booleans in Vyper are simple yet powerful foundational tools. By understanding and utilizing Booleans and their operators, you can create smart contracts that are precise, efficient, and reliable.

WhiteboardCrypto is the #1 online resource for crypto education that explains topics of the cryptocurrency world using analogies, stories, and examples so that anyone can easily understand them. Growing to over 870,000 Youtube subscribers, the content has been shared around the world, played in public conferences and universities, and even in Congress.