Skip to main content
Pascal, despite being designed to encourage good programming practices, has several common anti-patterns that can lead to maintainability problems and bugs. Here are the most important anti-patterns to avoid when writing Pascal code.
Avoid using GOTO statements, which make code difficult to understand and maintain. Use structured control flow constructs like for, while, repeat-until, and if-then-else instead.
Leverage Pascal’s strong typing system rather than using variant records or other techniques that circumvent type safety. In modern Object Pascal, use interfaces, classes, or generics for type-safe polymorphism.
Avoid using global variables, which create hidden dependencies and make code harder to understand, test, and maintain. Pass data explicitly through parameters and return values.
Use proper error handling with try-except-finally blocks (in modern Pascal) to handle exceptions and ensure resources are properly cleaned up, even when errors occur.
Avoid using magic numbers (hardcoded numeric literals) in your code. Use named constants to make your code more readable and maintainable.
Always ensure that resources (files, memory, etc.) are properly released, even when exceptions occur. Use try-finally blocks to guarantee cleanup code is executed.
Use appropriate data structures for your needs. In modern Pascal, use dynamic arrays, lists, dictionaries, and other collection classes instead of fixed-size arrays.
Organize your code into units (modules) with clear responsibilities. This improves maintainability, reusability, and compilation times.
Use strong type definitions and structured data types to make your code more type-safe and self-documenting.
Use function return values instead of var parameters for outputs. This makes the code more readable and allows for function composition.
Use modern string types and operations instead of fixed-length character arrays and manual string manipulation. This makes your code more readable and less error-prone.
Document your code with comments that explain the purpose, parameters, return values, and any important details or assumptions. This makes your code more maintainable and easier for others to understand.
In modern Object Pascal, use object-oriented design principles like encapsulation, inheritance, and polymorphism to create more maintainable and extensible code.