Chained Method Calls Sample Class
This repository demonstrates Chained Method Calls (or fluent interfaces) in Classic ASP. Although Classic ASP is an older technology introduced by Microsoft in 1996, it can still benefit from modern programming techniques. By returning the current instance (Me) from each method, developers can achieve a chained call style similar to those in more recent languages.
- Language: Classic ASP (VBScript)
- Concept: Chained Method Calls / Fluent Interface
- Goal: Improve code readability and maintainability by executing multiple method calls concisely on a single object.
Chained methods (often referred to as a fluent interface) allow multiple method calls on the same object in a single statement. Each method returns the current object (e.g., this in many languages), enabling a sequential chain of operations without needing separate variables or lines for each call. This approach makes code more readable and concise, as it clearly shows the flow of actions applied to the object.
-
Class Structure
- Encapsulate functionality within a
Classblock. - Use a private variable (e.g.,
m_number) to maintain state.
- Encapsulate functionality within a
-
Returning the Class Instance
- Each method (except the final result method) should return the current instance:
Set <MethodName> = Me
- Each method (except the final result method) should return the current instance:
-
MeKeyword- Similar to
thisin other languages,Merefers to the current class instance in VBScript.
- Similar to
The Mathematics class offers the following methods:
Add(number): AdditionSubtract(number): SubtractionMultiply(number): MultiplicationDivide(number): Division (with zero check)
Power(number): ExponentiationSquareRoot(): Square root (with negative number check)Modulus(number): Modulo operation (with zero check)
RoundUp(): Standard roundingCeiling(): Rounds up to the nearest integerFloor(): Rounds down to the nearest integer
GetResult(): Returns the final calculated value
<%
' Basic arithmetic operations
Set Math = New Mathematics
Result = Math.Add(10).Subtract(5).Multiply(2).Divide(2).GetResult()
Set Math = Nothing
' More complex operations
Set Math = New Mathematics
Result = Math.Add(16).Power(2).SquareRoot().GetResult()
Set Math = Nothing
%>With Line Continuation Sample
<%
Set Math0 = New Mathematics
Response.Write Math0.Add(10)_
.Subtract(5)_
.Multiply(2)_
.Divide(2)_
.GetResult()
Set Math0 = Nothing
%>When using chained method calls in Classic ASP, consider the following best practices:
-
Memory Management
- Always use
Set object = Nothingwhen you’re done. - Keep chain lengths reasonable to avoid memory issues.
- Always use
-
Error Handling
- Implement proper error handling in each method.
- Use
On Error Resume Nextcautiously.
-
Performance
- Long chains can affect performance.
- Break down complex operations into smaller steps if needed.
Below are some more involved chained method call examples:
' Complex mathematical operations
Result = Math.Add(10).Multiply(2).Divide(4).Subtract(3).GetResult()
' Nested operations
Result = Math.Add(5).Multiply(Math.Add(3).Multiply(2).GetResult()).GetResult()
' Using modulus and rounding
Result = Math.Add(17).Modulus(5).Ceiling().GetResult()The repository includes a test.asp file with comprehensive test scenarios:
- Basic arithmetic operations
- Exponentiation and square root
- Modulus and rounding
- Complex chained operations
- Error handling scenarios
- Negative number operations
Contributions are welcome! Please submit a Pull Request. For major changes, open an issue first to discuss what you would like to modify.
This project is licensed under the MIT License as described in the LICENSE file.
Document for Educational Purposes
- Project: Classic-ASP-Method-Chaining-Class
- Date: 19.01.2025 - V1.0
- Author: Anthony Burak DURSUN
- E-Mail: badursun🐌gmail.com