Introduction

The concept of scripting has been around since as early as the late 1980s. Its primary goal has been to provide a lightweight, flexible, and efficient way to automate tasks, customize software behaviour, and streamline processes in various domains, including system and database administration, automation, testing, prototyping, and more.

In this article, we'll discuss:

  • Types of scripting languages
  • Why and when to use scripting
  • Which language is the right fit for your project

Types of Scripting Languages

Categorizing scripting languages is not an easy feat. They can differ in their implementation, use cases, and provided functionality.

However, it's a common practice to classify them into the following generalized categories:

  • Interpreted
  • Compiled
  • Hybrid

Interpreted

Interpreted scripting languages execute scripts directly without prior compilation. The interpreter reads the source code line by line and executes exposed APIs on-the-fly. This is advantageous for the development and debugging process, but it often results in a significant loss of performance.

E.g.: Python, Ruby, Perl, JavaScript, PHP, PowerShell, Bash..

Compiled

Compiled scripting languages require an explicit compilation before execution. The source code is translated into machine-readable instructions, either directly to machine code or to an intermediate form like bytecode, which is then run in the execution context.

This approach offers a significant advantage in terms of performance, as the generated code is pre-optimized and the compiled binary can be executed more efficiently.

Additionally, it provides better static type checking and can generate standalone executables.

Note: The aforementioned execution context can be either an exposed software API, an operating system, or even a piece of hardware.

E.g.: Lua, Rust, Swift, Kotlin..

Hybrid

Hybrid scripting languages combine aspects of both interpreted and compiled languages. Languages of this category may utilize a combination of ahead-of-time (AOT) compilation and just-in-time (JIT) compilation techniques.

Hybrid languages typically have a compilation step that converts source code into an intermediate representation, such as bytecode, which can be executed by an interpreter or a just-in-time compiler.

This approach offers balance between performance and development flexibility.

E.g.: Julia, Groovy, R, PHP, Lua, Ruby, Python, JavaScript

Why and When to Use Scripting

Like with every technology, scripting is not a one-size-fits-all solution. Despite all the possible benefits, it's easy to invest a significant workload in implementing it and end up gaining little to nothing as a result.

In this section, we'll delve into the potential benefits of using scripting and discuss important considerations before proceeding with its implementation.

The Benefits

Separate Implementation & Consumption

Implementing scripting into your project can create a dedicated abstraction layer separating implementation from consumption.

Instead of hardcoding the consumption logic right into the software, one could simply create the required functionality and expose it to a scripting environment for execution. Then, write the consumption logic separately in a scripting language of their choosing.

This approach allows us to have the same piece of software, but potentially unlimited paths to consume it, e.g., one game engine supporting unlimited games.

A sample game engine before and after adding Lua scripting would look something like this:

Note: Separation of implementation and consumption is a widely used and praised approach in Software Architecture.

Additional Security Layer

Implementing a scripting engine in your project can enhance security through:

  1. Reduced Exposure: By offering scripting capabilities, you can reduce the need for users to modify or access the core codebase of your software. This reduces the risk of unintended modifications or errors that could introduce security vulnerabilities.

  2. Flexibility and Updates: With scripting, you can introduce new features or modify existing functionality without requiring a full software update. This allows you to address security issues promptly and deploy patches or fixes more efficiently.

  3. Controlled Extensibility: Scripting allows users to extend the functionality of your software without directly modifying the core code.

  4. Enhanced User Permissions: With scripting, you can assign specific permissions to scripts based on user roles or privileges. This allows you to control the actions and resources accessible to scripts, reducing the risk of unauthorized access or misuse of sensitive data.

  5. Separation of Concerns: By separating core functionality from scripted code, you isolate potential vulnerabilities within the scripting layer. This mitigates the impact of any security issues that may arise within scripts, keeping the core software more secure.

Faster Prototyping & Testing

Using scripting in software development can significantly speed up the prototyping and testing phases of the development process.

Scripting languages offer several perks for these stages, including:

  • Simpler and More Concise Syntax: Scripting languages often have a simpler, type-unsafe, and more concise syntax compared to compiled languages. This simplicity allows for rapid iteration, quick implementation of changes, and testing different ideas without the overhead of explicit type declarations, compiling, building, and deployment.

  • Interactive Tools and Environments: Scripting languages are often supported by tools and environments that facilitate interactivity. This enables developers to execute code snippets and receive immediate feedback, enhancing the speed and efficiency of prototyping and testing.

  • Integration Capabilities: Scripting languages can be easily integrated into new and existing projects through various third-party solutions. Due to their community-driven nature, scripting languages have a wide range of third-party plugins and frameworks available, simplifying the implementation of a specific scripting language into your project.

Open to Third-Party Solutions

Adding scripting to your software enables integration of third-party solutions and promotes community-driven innovation.

Think of examples like Browser Extensions or Mods & Addons for any game that supports them. These real-world examples demonstrate the power of scripting, allowing users to extend and enhance software functionality according to their specific needs.

This approach encourages an ecosystem of community-developed solutions, expanding the possibilities and usefulness of the software while tapping into the expertise and creativity of the community.

Use Cases

As mentioned before, scripting is not a one-size-fits-all solution and can potentially do more harm than good if not used appropriately.

Certain scripting languages, such as JavaScript, SQL, PHP, Ruby, and Shell, are widely used and often come bundled with databases, frameworks, and other tools. In these cases, their usage is unavoidable.

However, when considering whether to implement scripting in your project, the following diagram can help guide your decision-making process:

Which Scripting Language to Pick

Selecting the right scripting language for your project requires careful consideration. More often than not, the best tool to accomplish the task at hand is the tool you're most comfortable with - so if you're experienced in JavaScript, Python, or Lua, go with either of them.

In any other case, consider the following when picking the right scripting language for your project:

  • Integration Capabilities: Is there a plugin offering out-of-box integration with your project?
    I.e.: can you easily create Lua bindings for a .NET project?

  • Long-Term Viability: Is the scripting language still actively developed and updated?

  • Ecosystem & Tooling: Does the scripting language have an ecosystem rich enough to provide you with the tools that you need?

  • Community Support: Is the scripting language popular enough, and is the community active?

If you've answered "Yes" to most of these, then the language is most likely a good fit.

Final Thoughts

In conclusion, scripting offers significant benefits in software development, such as flexibility, extensibility, and faster prototyping. It allows for the separation of implementation from consumption, enabling easy customization and diverse ways to consume software.

When choosing a scripting language, consider project requirements, team expertise, integration capabilities, ecosystem support, and long-term viability. Selecting the right language ensures compatibility, productivity, and access to a thriving community.

Scripting promotes an ecosystem of community-developed solutions, tapping into the expertise and creativity of the community. By taking advantage of scripting, you can extend and enhance software functionality according to specific needs, leading to a more versatile software ecosystem.

Remember to carefully evaluate the applicability of scripting to your project and be mindful of potential risks. With wise implementation and consideration, scripting can unlock new possibilities and streamline development processes.