1.12. Naming Conventions for Widget Types

Naming conventions? How boring! Well, sometimes our programs get so large and unwieldy that we can't remember what that stupid $button variable was pointing to. If there are over 10 buttons in our program, we would be hard-pressed to figure out which button was $button3 without digging through a bunch of code.

I'm merely going to suggest a naming convention, and if you like it, please use it! If not, either come up with your own, or hope you have a really good memory.

For buttons, I like to use _b, _bttn, or Button as a type of qualifier to the variable name. For instance, I would name my button in the Hello World example $done_b, $done_bttn, or$doneButton.

A specialized widget type is the very first window we create with the MainWindow method. I always use $mw as the variable name for this. You will see other programs use $main or $mainwindow as well.

Table 1.1 contains a list of widget types and my suggested naming conventions for them. Replace "blah" with a sensible description of the widget's purpose (e.g., exit). If you use this convention, you'll always know what type of widget you're working with.

Table 1.1. Naming conventions by widget type
Widget Type Suggested Name Examples
Button $blah_b (or $blah_bttn, $blahButton) $exit_b, $apply_b, $newButton
Canvas $blah_canvas or $blahCanvas $main_canvas, $tinyCanvas
Checkbutton $blah_cb or $blahCheckbutton $uppercase_cb, $lowercaseCheckbutton
Entry $blah_e or $blahEntry $name_e, $addressEntry ...

Get Learning Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.