libcellml
libcellml copied to clipboard
Create properties in python language bindings
A problem I noticed with the python bindings is that there are many functions which are named like attributes. This can create many problems in a dynamic typed language like python.
E.g. to access the name on a component one uses component.name()
. A typical mistake will be to try to access the name via the name attribute, i.e. component.name
which returns the function reference.
This will create many downstream problems and and subtle bugs of the form
if component.name
do A
else
do B
Because name is a function reference it will always evaluate to True and not create an Attribute error.
Similar things for other functions which are named like attributes such as id.
This is especially problematic because all the other combine libraries basically have the id
, name
and similar attributes.
I.e. users know
sbase.id
sbase.name
sedbase.id
sedbase.name
The solution is either to correctly name the getter functions as get_name
or getName
,or to create properties in the swig bindings.
I.e. use property decorators
@property
def name():
return ...
Functions should always start with a verb which describes what the function is doing. See for instance guidelines in the famous Clean Code concepts: https://medium.com/coding-skills/clean-code-101-meaningful-names-and-functions-bf450456d90c
Method Names
Methods should have verb or verb phrase names like postPayment, deletePage, or save.
In the special case of python the dynamical typed language and first class function objects and existance tests in combination will create a lot of problems, e.g.
if not component.id:
component.setId(myid)
will leave components without ids. I have no idea about the setters, but probably similar issues.