Skip to content

Commit 8753eb7

Browse files
docs: [FC-0074] add suggestion about the design of filters (#240)
1 parent b33c647 commit 8753eb7

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
7. Filters Design Practices
2+
###########################
3+
4+
Status
5+
------
6+
7+
Draft
8+
9+
Context
10+
-------
11+
12+
It is important to follow standards to ensure that the filters are designed in a way that is easy to understand and maintain. This ADR aims to provide guidelines for designing Open edX Filters with long term maintainability in mind.
13+
14+
Decision
15+
--------
16+
17+
These are the practices that we recommend reviewing and following when designing Open edX Filters and contributing them to the library. Consider this example of a filter that is triggered when a user requests a certificate, we will use this example to illustrate the practices:
18+
19+
.. code-block:: python
20+
21+
class CertificateCreationRequested(OpenEdxPublicFilter):
22+
23+
class PreventCertificateCreation(OpenEdxFilterException):
24+
"""
25+
Custom class used to stop the certificate creation process.
26+
"""
27+
28+
@classmethod
29+
def run_filter(cls, user, course_key, mode, status, grade, generation_mode):
30+
31+
Design Clarity and Understanding
32+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33+
34+
- A filter should describe as accurately as possible what behavior intends to modify.
35+
- A filter should have a clear and concise name that describes its purpose.
36+
- Ensure that the triggering logic of the filter is consistent and narrow. It should only trigger when the conditions are met and cover all the cases that the filter should be applied with the minimum number of modifications.
37+
38+
Consider the example above, the filter name is ``CertificateCreationRequested`` which indicates that it is a filter that is triggered when the certificate creation is requested, providing a clear understanding of the filter behavior. Avoid using generic names such as ``Filter`` or ``Process`` that do not provide any context about the filter behavior.
39+
40+
The ``CertificateCreationRequested`` is triggered by `_generate_certificate_task`_ which handles all the cases that the filter should be applied avoiding unnecessary modifications.
41+
42+
Contextual Information
43+
~~~~~~~~~~~~~~~~~~~~~~
44+
45+
- A filter should provide enough context in its arguments about the process that is modifying to be able to modify the intended behavior.
46+
- A filter should provide enough context in its arguments to avoid runtime dependencies with the application.
47+
- The arguments of the filter should be directly related to the responsibility of the filter.
48+
49+
Consider the filter example above, the filter provides the necessary context to understand the behavior that is being modified such as the user, course key, mode, status, grade, and generation mode. The arguments are directly related to the responsibility of the filter.
50+
51+
Flexibility and Customization
52+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53+
54+
- A filter should be flexible enough to allow the developer to customize the behavior of the application.
55+
56+
Error Handling
57+
~~~~~~~~~~~~~~
58+
59+
- Ensure that the exceptions when using the filter are handled properly and that the filter does not break the application.
60+
- The filter exceptions should correspond with the filter behavior. If the filter is not supposed to halt the application behavior in any case, then do not specify exceptions. If the filter is supposed to halt the application behavior in some cases, then specify the exceptions that the filter can raise with enough reason to understand why the filter is halting the application behavior.
61+
62+
Consider the example above, the filter specifies an exception that is raised when the certificate creation process is stopped, providing a clear understanding of the filter behavior when the exception is raised. This exception should be handled properly in the application to avoid runtime errors.
63+
64+
.. note:: The application should catch the exception and handle it properly by returning an error message to the user through API responses, UI messages, or logs depending on the application context and where the filter is being used. For reference, see how the exception is handled in the `Pull Request edx-platform#35407`_.
65+
66+
Type Safety
67+
~~~~~~~~~~~
68+
69+
- A filter should annotate the type of the arguments that it receives, only when it doesn't create unnecessary dependencies with the application.
70+
71+
Consequences
72+
------------
73+
74+
Following these practices will ensure that the filters are designed in a way that is easy to understand and maintain. Having these standards in place will also make the decision process easier when designing new filters.
75+
76+
.. _`_generate_certificate_task`: https://github.com/openedx/edx-platform/blob/master/lms/djangoapps/certificates/generation_handler.py#L116-L128
77+
.. _`Pull Request edx-platform#35407`: https://github.com/openedx/edx-platform/pull/35407

docs/decisions/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Decisions
1111
0004-filters-naming-and-versioning
1212
0005-filters-payload
1313
0006-filter-debug-tooling
14+
0007-filter-design-practices

0 commit comments

Comments
 (0)