Code on demand is a paradigm in distributed computing in which executable software code is transmitted from a server computer to a client computer upon the client's request. It is a specific form of mobile code and falls within the broader field of code mobility.
Overview
In a code-on-demand system, program code resides inactive on a web server until a client requests a resource (such as a web page) that contains a reference to the code. The code is then transported to the client's machine using a protocol such as HTTP. Once received, the code executes locally on the client until it is terminated (for example, when the user navigates away from the page). Well-known examples include Java applets, Adobe ActionScript for Flash Player, and JavaScript.
Role in REST Architecture
Code on demand is one of the six architectural constraints that define the Representational State Transfer (REST) architectural style, as originally described by Roy Fielding in his 2000 doctoral dissertation. It is the only optional constraint among the six. The other five constraints are: client-server architecture, statelessness, cacheability, a layered system, and a uniform interface.
Fielding's dissertation defines the constraint as follows:
"REST allows client functionality to be extended by downloading and executing code in the form of applets or scripts. This simplifies clients by reducing the number of features required to be pre-implemented. Allowing features to be downloaded after deployment improves system extensibility. However, it also reduces visibility, and thus is only an optional constraint within REST."
Benefits
- Client simplification: Reduces the number of features that must be pre-implemented on the client.
- Extensibility: Allows server-side developers to extend client functionality after deployment without requiring client software updates.
- Reduced server load: Offloading processing to the client can reduce computational demands on the server.
Drawbacks
- Reduced visibility: Because the server sends executable logic rather than just data, it becomes harder to monitor and inspect the interactions between client and server.
- Security risks: Executing server-provided code on the client introduces potential vulnerabilities, including the risk of malicious code execution.
- Client compatibility: Not all clients support or permit execution of server-provided code (e.g., due to strict security policies in mobile apps or enterprise environments).
Common Implementations
The most widespread implementation of code on demand on the web is JavaScript. When a web browser requests an HTML page that includes embedded or linked JavaScript, the server delivers executable code that the browser runs locally. This enables dynamic user interfaces, client-side validation, asynchronous data fetching, and other interactive features. Other historical implementations include Java applets and Adobe Flash.
Relation to Other Concepts
Code on demand is related to the concepts of remote evaluation and code mobility. In remote evaluation, code is sent to a remote server for execution; in code on demand, code is sent to a client for local execution. Both are forms of mobile code.