Soplang Variables

Variables in Soplang are containers for storing data values. Soplang offers the flexibility of both dynamic and static typing, allowing you to choose the approach that best fits your needs while maintaining type safety.

Variable Declaration

Dynamic Typing

In Soplang, you can declare variables with dynamic typing using the door keyword. The type of the variable is determined by the value assigned to it.

The door keyword is short for doorsoome, which means "variable" in Somali. This abbreviation was chosen to make the code more concise while preserving its Somali roots.

dynamic_typing.sop
// Dynamic typing with door
door name = "John"     // String
door age = 25         // Number
door is_student = true  // Boolean
door grades = [85, 90, 78, 92]  // List/Array

Static Typing

For more type safety, Soplang also supports static typing where you explicitly declare the type of the variable.

static_typing.sop
// Static typing with specific type keywords
tiro count = 10           // Integer 
qoraal message = "Hello"  // String
labadaran active = true   // Boolean
toban price = 29.99       // Float/Decimal

// Type mismatch will cause an error
// tiro wrong = "text"  // Error: Cannot assign string to tiro

Declaration Without Initialization

You can also declare variables without initializing them, but you must assign a value before using them.

declaration_without_init.sop
// Declaration without initialization
door username
tiro user_age

// Assign values later
username = "Ahmed"
user_age = 30

// Using the variables
qor(username + " is " + user_age + " years old.")

Naming Conventions

When naming variables in Soplang, follow these guidelines for clarity and consistency:

Valid Variable Names

naming_conventions.sop
// Good variable names
door user_name = "John"   // Using underscores
door userName = "John"    // Using camelCase
door age = 25            // Simple and clear
door isActive = true     // Boolean with descriptive name

// Bad variable names
door a = "John"          // Too short, not descriptive
door x1 = 25             // Not meaningful
door thisisaveryverylongvariablenamethatisnoteasytoread = true  // Too long

Naming Style

  • Use camelCase for variable names (e.g., userName, firstNumber)
  • Use PascalCase for class names (e.g., Person, UserProfile)
  • Use snake_case as an alternative for variable names (e.g., user_name, total_amount)

Note on Somali Language Identifiers

While Soplang keywords are in Somali, variable names are typically in English for international readability. However, you can use Somali words as variable names if it makes your code more readable to Somali speakers.

Variable Scope

The scope of a variable defines where the variable can be accessed within your code.

Global Scope

Variables declared outside any function or block have global scope and can be accessed from anywhere in the program.

global_scope.sop
// Global variable
door app_name = "Soplang Demo"

howl display_app_info() {
    // Global variable accessible inside function
    qor("App: " + app_name)
}

display_app_info()  // Prints "App: Soplang Demo"

Local Scope

Variables declared inside a function or block have local scope and can only be accessed within that function or block.

local_scope.sop
howl calculate_area() {
    // Local variables
    door width = 10
    door height = 5
    door area = width * height
    
    soo_celi area
}

door result = calculate_area()
qor(result)  // Prints 50

// This would cause an error:
// qor(width)  // Error: width is not defined in this scope

Variable Shadowing

When a local variable has the same name as a global variable, the local variable "shadows" the global one within its scope.

shadowing.sop
door count = 5  // Global variable

howl display_count() {
    door count = 10  // Local variable that shadows the global one
    qor("Local count: " + count)  // Prints "Local count: 10"
}

display_count()
qor("Global count: " + count)  // Prints "Global count: 5"

Lexical Scope

Soplang uses lexical scoping, which means that inner functions have access to variables declared in their outer function.

lexical_scope.sop
howl create_counter() {
    door count = 0  // Variable in outer function
    
    // Inner function has access to count
    howl increment() {
        count = count + 1
        soo_celi count
    }
    
    soo_celi increment  // Return the inner function
}

door counter = create_counter()
qor(counter())  // Prints 1
qor(counter())  // Prints 2
qor(counter())  // Prints 3

Constants

Constants are variables whose values cannot be changed after initialization. In Soplang, you declare constants using the joogto keyword.

constants.sop
// Defining constants
joogto PI = 3.14159
joogto MAX_USERS = 100
joogto APP_VERSION = "1.0.0"

// Constants cannot be changed
// PI = 3.14  // This would cause an error

// Using constants
door radius = 5
door area = PI * radius * radius
qor("Area: " + area)  // Prints "Area: 78.53975"

Naming Convention for Constants

By convention, constants are usually named using UPPER_SNAKE_CASE to distinguish them from regular variables.

Best Practices

  • Use Static Typing for Better Type Safety

    While dynamic typing offers flexibility, static typing helps catch type-related errors earlier.

    static_typing_best_practice.sop
    // Prefer static typing for important variables
    tiro user_count = 42
    qoraal status = "active"
  • Initialize Variables When Declaring

    Initialize variables at the time of declaration to avoid undefined value errors.

    initialization_best_practice.sop
    // Good practice
    door user_name = "Default"
    
    // Instead of
    // door user_name
    // ... code ...
    // user_name = "Default"
  • Use Descriptive Names

    Choose descriptive variable names that clearly indicate their purpose.

    descriptive_names.sop
    // Good variable names
    door user_age = 25
    door is_logged_in = true
    door product_price = 29.99
    
    // Instead of
    door a = 25
    door b = true
    door c = 29.99
  • Limit Variable Scope

    Keep variables in the smallest scope necessary to avoid naming conflicts and improve code clarity.

    limit_scope.sop
    // Prefer local variables when possible
    howl calculate_total(prices) {
        door total = 0  // Local variable only used in this function
        
        ku_celi price ku dhex jira prices {
            total = total + price
        }
        
        soo_celi total
    }
  • Use Constants for Fixed Values

    Use constants for values that should not change during program execution.

    use_constants.sop
    // Use constants for configuration and fixed values
    joogto API_URL = "https://api.example.com"
    joogto MAX_ATTEMPTS = 3
    joogto TAX_RATE = 0.07
    
    // Use in code
    door remaining_attempts = MAX_ATTEMPTS
    door price_with_tax = price_of_item * (1 + TAX_RATE)

Next Steps

Now that you understand variables in Soplang, explore these related topics: