Talk

Dyndesign: Merging Classes Dynamically

Saturday, May 27

11:15 - 11:45
RoomPizza
LanguageEnglish
Audience levelIntermediate
Elevator pitch

Frequent development scenarios involve working on scripts/applications with variety of options for customizing behavior. Dyndesign organizes & structures code for each option in specific classes, then dynamically merges with main class based on selected options for a clear and maintainable codebase.

Abstract

Developers often find themselves working on scripts or applications that require a high degree of customization, with various behaviors controlled by script options or application parameters. As the number of behaviors increases in proportion to the combinations of options, the complexity of the code can become overwhelming, leading to issues such as duplicated code, poor code structure, and difficulty in maintaining the codebase. Design patterns in Python can be used to enhance code quality in such development scenarios, and examples of these design patterns can be easily found online. Package Dyndesign (https://github.com/amarula/dyndesign) offers a solution with a set of tools to load and merge optional classes with base classes dynamically, also enabling safe invocation of methods or use as decorators without causing fatal errors if the optional classes are not loaded. It also provides the ability to choose between method overloading or calling all methods of the merged classes with the same name.

Dyndesign’s main objective is to adhere to SOLID principles, particularly the Single Responsibility principle and the Open-closed principle, and eliminate code duplication by

  1. confining code related to each option to specific classes,
  2. building option-customized class by loading classes to merge based on selected options,
  3. minimizing the impact on the code when adding new options, by only requiring the addition of new modules containing the optional classes to be merged.

Dyndesign stands out from common design patterns, such as adapter, class builder, facade, etc. by simplifying ease of use, readability, and maintainability of code. Developers don’t need to manage dependency injections, separation of abstract/concrete classes, or MRO for multiple inheritance to create customizable apps. The only concern for developers using Dyndesign is building basic classes and option-specific extension classes, and merging them dynamically using the mergeclasses function based on selected options.

TagsOpen-Source, Tooling, Clean Code, Abstractions
participant photo

Patrizio Gelosi

I’m Patrizio, Software Engineer by profession and by passion. Since I received my Master’s degree in Software Engineering at the University of Pisa in 1999, I have worked either for corporate (Telecom Italia, Vueling Airlines) or self-founded companies/projects (Dispage, 3nce, HandyContract). Here are some cool projects I’ve worked at: detection of facial movements from videos with AI, Natural Language Processing engines, platforms for processing Images and Documents and HTML graphic editors. Since 2022, I have been an active member of the Amarula Solutions crew as a software engineer, contributing my skills to several fascinating projects.