Skip to main content

Modules

A module in EasyBite is a logical collection of code that is packaged and stored in a separate file to make it reusable, organized, and easier to maintain. These modules contain functions, variables, constants, classes, and other reusable code blocks that you can import into other programs or files. The primary purpose of modules is to promote code reusability and modularity, which helps to keep the codebase clean, manageable, and maintainable.

In EasyBite, modules allow developers to divide their code into smaller, focused files, each responsible for a particular task or functionality. This division enables developers to organize their code logically, making it easier to test, update, and expand.

Why Use Modules?

Modules in programming, including EasyBite, offer a host of benefits:

  1. Reusability: Once a module is created, it can be reused in multiple programs or files, reducing the need to rewrite the same code multiple times. If you need to change the logic, you only need to modify the module, and all programs that use it will automatically reflect those changes.

  2. Organization and Structure: By breaking your code into multiple modules, you can organize it into logical components, making it easier to maintain, understand, and debug. For example, one module might handle string operations, another might handle file I/O, and another could provide mathematical functions.

  3. Separation of Concerns: Modules allow you to separate different concerns of the program into distinct sections. For instance, if your program handles both networking and user interface tasks, you could have separate modules for each, making your code cleaner and easier to manage.

  4. Maintainability: With a well-structured modular codebase, updating or fixing bugs becomes easier. If an error is found in one module, you can address it without affecting the rest of the code. This reduces the risk of breaking other parts of the program.

  5. Scalability: As your program grows, the modular approach allows you to scale the project effectively by adding more modules without overwhelming the entire system. This also makes collaborative work more efficient since different developers can work on different modules concurrently.

  6. Namespace Management: Modules help avoid name clashes. Since each module creates its own namespace, variables, and functions within the module do not conflict with those in other parts of the program.

Structure of a Module in EasyBite

A module in EasyBite is simply a file that contains code (functions, variables, classes, etc.) that can be imported into another program or module. Modules do not need to follow any particular internal structure, but typically, a module will contain functions or classes that are logically grouped based on their functionality.

For example, a module might contain functions related to math operations, file I/O functions, or user interface helpers. Here is a basic example of what a module might look like:

math_operations.eb:

// math_operations.eb - A module for basic math functions

// Function to add two numbers
function add(a, b)
return a + b
end function

// Function to subtract two numbers
function subtract(a, b)
return a - b
end function

// Function to multiply two numbers
function multiply(a, b)
return a * b
end function

The math_operations.eb module can now be imported into other programs to use these functions, making it easier to perform math operations without having to rewrite the logic each time.

Types of Modules

  1. Built-in Modules: These are modules that come pre-packaged with the EasyBite language. Built-in modules provide commonly used functionalities, such as math operations, file handling, and more. These modules are always available and do not need to be created or imported by the user.

  2. Custom Modules: These are modules that you, as a developer, create in your own projects. Custom modules allow you to organize your program into smaller, focused sections. You can create these modules and store them in a designated folder (typically called modules/).

How Does a Module Work?

When you import a module into your EasyBite program, the system searches for the module in a predefined set of locations and loads it into the program’s namespace. This process makes the functions, variables, and classes inside the module available for use.

The EasyBite runtime performs the following steps to handle module imports:

  1. Check for Built-in Modules: If the module is a built-in module, the system will immediately load it. Built-in modules are part of the EasyBite standard library, such as mathematical operations or data structures.

  2. Check the Current Working Directory: If the module is not built-in, the system will search the current directory where the program is being executed for a file with the same name as the module.

  3. Check for a Directory with the Module Name: If the module is not found in the current working directory, the system will check if there is a directory with the module's name. This is useful for organizing your code by separating modules into different directories.

  4. Check for the BITE_MODULES Environment Variable: If the module is still not found, and the environment variable BITE_MODULES is set, the system will check the path specified in the environment variable to locate the module.

  5. Check the /modules Folder: Finally, if the module is not found in any of the above locations, the system will check the /modules folder in the project directory.

  6. Look for a Directory Named After the Module: If all else fails, the system will look for a directory with the name of the module. This directory should contain a main.eb file that acts as the entry point for the module.

This modular approach ensures that modules can be easily organized and reused across different projects and that the system is flexible in locating the necessary files.

Importing Modules in EasyBite

Once a module is located, it can be imported using the import keyword. Here’s the general syntax for importing a module in EasyBite:

  • Import a Custom Module:

    import modulename
  • Import Built-in Modules:

    import math
  • Import Specific Functions from a Module:

    from modulename import functionname

Example of Importing a Custom Module

Let’s say you have a custom module named utils.eb that contains helper functions for string manipulation:

// utils.eb - A custom module with string manipulation functions

// Function to concatenate two strings
function concatenate(str1, str2)
return str1 + str2
end function

To import and use this module in your main program:

import utils

set greeting to utils.concatenate("Hello", "World")
show greeting // Outputs: "HelloWorld"

Example of Importing Built-in Modules

In EasyBite, built-in modules such as math are available for mathematical operations. To use the math module:

import math

set sum to math.add(10, 5)
show sum // Outputs: 15

Importing Multiple Functions or Classes

You can import multiple functions or classes from a module in one line:

from math import add, subtract

Then, you can use them directly in your program:

set result_add to add(10, 5)
set result_subtract to subtract(10, 5)

show result_add // Outputs: 15
show result_subtract // Outputs: 5

Example of Importing a Class from a Module

Suppose you have a module called shapes.eb that contains a Circle class to calculate the area of a circle:

// shapes.eb - A module with a Circle class

class Circle
declare radius

init(radius)
this.radius to radius
end init

method calculate_area()
return 3.14 * self.radius * self.radius
end method
end class

To import and use this class:

import shapes

set my_circle to shapes.Circle(5) // Create a Circle with radius 5
set area to my_circle.calculate_area()

show "Area of the circle: " + area // Outputs the area of the circle

Example of Importing a Class and Function Together

You can also import both a class and a function from the same module:

from shapes import Circle, square_area

set square_result to square_area(4)
set circle to Circle(5)
set circle_area to circle.calculate_area()

show square_result // Outputs: 16
show circle_area // Outputs the area of the circle

Conclusion

Modules in EasyBite are powerful tools that allow you to organize, reuse, and maintain your code more efficiently. By breaking your program into smaller, manageable pieces, you can create more scalable and maintainable applications. The ability to import both built-in and custom modules enhances flexibility and reduces redundancy.

The search order for modules ensures that EasyBite can locate the necessary files, whether they are in the current directory, a specific directory, the /modules folder, or even specified through environment variables. By using the import statement effectively, you can build complex programs with minimal code repetition and maximum modularity.