Poor Names

bakatsiasgeorge

George Bakatsias

Posted on March 5, 2023

Poor Names

In software development, code smells such as poor names can greatly hinder code comprehension and maintainability. Inadequate naming of variables, methods, and classes can lead to confusion and errors. It's important to be aware of various categories of poor names:

Mysterious Names: These names fail to convey their intention or purpose. Examples include using vague variable names like "dr1" instead of "dataReader" or employing ambiguous method names like "Button1_Click" instead of "CheckAvailability_Click".

Meaningless Names: These names are overly generic and lack descriptive meaning. Instead of using convoluted method names like "BeginCheckFunctionality_StoreClientSideCheckboxIDsArray", it's better to opt for more descriptive names that clearly indicate the method's purpose.

Names with Encodings: This refers to the use of prefixes, suffixes, or Hungarian notation to indicate data types or scope. With modern IDEs providing advanced features, encoding is no longer necessary. Let the IDE handle the determination of data types, avoiding encodings like Hungarian notation.

Ambiguous Names: These names can be interpreted in multiple ways, leading to confusion. For instance, a method named "MultiSelect" could be unclear about whether it selects multiple items or handles multiple selections. Similarly, a variable named "incidentNameId" raises questions about its nullability or the presence of "Name" in the name itself.

Noisy Names: These names are excessively verbose or redundant. Instead of using long variable names like "theCustomer" or "listOfApprovedCustomers", it is better to use concise names such as "customer" and "approvedCustomers".

To avoid poor names, ensure that your chosen identifiers are meaningful, of appropriate length, convey intention, and are relevant to the problem domain you are working on. Additionally, you can employ refactoring tools like Rider to easily and efficiently rename identifiers without introducing any issues into your codebase. By adhering to these guidelines, your code will be more readable, understandable, and maintainable for both yourself and other developers.

Let's consider a C# example to illustrate how naming conventions can significantly impact code clarity:

// Poorly named variables and methods
int o; // overdue days
string c; // customer
double[] p; // prices
void fc() // function to calculate discounts

// Well-named variables and methods
int overdueDays;
string customerName;
double[] itemPrices;
void CalculateDiscounts()
Enter fullscreen mode Exit fullscreen mode

In the above example, the poorly named variables and methods employ single-letter names or abbreviations that provide little insight into their purpose. Conversely, the well-named variables and methods use descriptive names that clearly indicate their intent.

By following proper naming conventions, you can greatly enhance code readability and comprehension, saving time and effort for both you and fellow developers. Remember to choose meaningful and relevant names that align with the problem domain. And don't forget to utilize refactoring tools like Rider, which facilitate quick and hassle-free identifier renaming.


Here is a way of how Rider's refactoring tools can help:

  • Select with your mouse the variable or method
  • Do a mouse right click and go to "Refactor > Rename" (or go from the "Refactor" menu in the ribbon).

Keyboard Shortcuts:

  • F2
  • Or Ctrl + Shift + R > Rename

Rider will then prompt you to enter a new name for the identifier, and will automatically update all references to that identifier throughout your codebase.

💖 💪 🙅 🚩
bakatsiasgeorge
George Bakatsias

Posted on March 5, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related