| Start |
|---|
Abstraction Abstraction levels help determine when to split things up.Abstraction is achieved by implementing functionality with increasingly detailed code. The entry point of a macro should be a small procedure with a high abstraction level that makes it easy to grasp at a glance what's going on: The The Here the procedure is reading the available values from a named range on a |
Encapsulation Encapsulation hides implementation details from client code.The Handling QueryClose (opens new window) example demonstrates encapsulation: the form has a checkbox control, but its client code doesn't work with it directly - the checkbox is an implementation detail, what the client code needs to know is whether the setting is enabled or not. When the checkbox value changes, the handler assigns a private field member: And when the client code wants to read that value, it doesn't need to worry about a checkbox - instead it simply uses the The Using interfaces to enforce immutabilityLet's push that a step further by encapsulating the form's model in a dedicated class module. But if we made a The Notice The interface members are all Using a Factory Method to simulate a constructorUsing a VB_PredeclaredId (opens new window) attribute, we can make the This factory method assigns the property values that are read-only when accessed from the And now we can code against the |
Polymorphism Polymorphism is the ability to present the same interface for different underlying implementations.The ability to implement interfaces allows completely decoupling the application logic from the UI, or from the database, or from this or that worksheet. Say you have an The form's code-behind could look like this: But then, nothing forbids creating another class module that implements the And now we can change the code that works with a Because the Testable code depends on abstractionsWriting unit tests in VBA can be done, there are add-ins out there that even integrate it into the IDE. But when code is tightly coupled with a worksheet, a database, a form, or the file system, then the unit test starts requiring an actual worksheet, database, form, or file system - and these dependencies are new out-of-control failure points that testable code should isolate, so that unit tests don't require an actual worksheet, database, form, or file system. By writing code against interfaces, in a way that allows test code to inject stub/mock implementations (like the above |
Read Article
https://develop.horje.com/learn/1434/reference