Is there a recommended naming convention for controls?

I’m looking to standardize naming conventions for controls.
Using text box as and example: here are a couple styles I can think of:
txtFirstName (from back in the Visual Basic days, but not very pythonic)
text_box_32 (the default anvil name, which I want to avoid)
text_box_first_name
first_name_text_box
first_name_box
etc.

I searched for a recommendation, but could not find one.

Have you seen a published list somewhere? What has worked for you and your team?

Thanks!

I tend to use first_name_text_box

One of my clients uses text_box_first_name

I only rename components with event handlers. I just let anvil name anything with no reference in the code.

1 Like

I go with this, just replacing the number with something more descriptive, then I can use the search later in the code to find all referenced instances of that component, since they are all named the same at the beginning and I might not remember what it was called on that form.
This way I can look at all of them in a single search.
Since you are using python, scope is almost never an issue (or you are probably doing something wrong) so you can easily re-use the same names for new forms without issue.

I agree. If feels slightly sloppy, but there is no need to waste time naming stuff that will never be accessed by name. And it gives me a little bit of information, it says “this thing is never accessed by the code”.


Rearding the names of objects used instead, I like to keep it simple: no hungarian notation for variable names, no control type for control names.

I don’t like int_age or str_name as much as I don’t like text_box_age or text_box_name. I use age or name and add the prefix only in the very few cases where the same function keeps track of the same piece of data in multiple formats, for example as a text box and as an integer, or as a dictionary and as a json string.

Here is an example of the controls on a form with a pager.

If it has any non-default behavior – via code, via event-handlers, or via data bindings – then I tend to name it. I abbreviate the type, and follow it with the name of the datum or data group. (If it’s a data binding, then I try to match the binding’s key.)

For example, I might make an entire set of questions disappear, when they don’t apply, by setting its container’s visible attribute to False. That may be via data binding, if feasible, or by code, if it requires computation or lookup, but either way, I’ll be sure to name it after the role it plays. Otherwise, later, I’ll be asking, “I’m hiding what, now?”

1 Like