In the last articles from the High-Quality Automated Tests we talked about coding styles and various tools that can help you to force them automatically. In this publication, I am going to talk about different methods naming guidelines your tests should follow so that they are more readable and maintainable by other people.
While ago when we were working on the first version of the BELLATRIX test automation framework, I did this research while I was working on a similar feature for our solution.
Naming Methods
Method names should answer the question- What does this method do? If you cannot find a good name for a method, think about whether it has a clear intent. Because methods are the means of taking action, the design guidelines require that method names be verbs or verb phrases.
Correct
FindStudent, LoadReport, PlaceOrder, CreatePurchase
Incorrect
Method1, DoSomething, HandleStuff, SampleMethod, DirtyHack
Use PascalCase for C#. For example LoadSettings. Prefer the following formats [Verb], [Verb] + [Noun],[Verb] + [Adjective] + [Noun].
Correct
Generate, LoadSettingsFile, FindNodeByPattern, ToString, PrintClientsList
Incorrect
Student, Generator, Counter, White, Approximation, MathUtils
Do Not Use Underscores in Method Names
Life cycle events are exception.
Correct
public SendActionKeys(IKeyboard keyboard, IMouse mouse, ILocatable actionTarget, string keysToSend)
{
//...
}
Incorrect
public Send_Action_Keys(IKeyboard keyboard, IMouse mouse, ILocatable actionTarget, string keysToSend)
{
//...
}
Meaningful Method Names
Correct
int count = GetCountByStampId(Tenant tennat, long stampId);
Incorrect
// get objects count filtered by stampId
int count = GetAllByStampCount(Tenant tennat, long stampId);
Correct
var meanDemand = CalculateMeanDemand();
Incorrect
// calculate mean demand
double meanDemand = Calculate();
Do Not Use Many Method Parameters
Correct
protected virtual DependentDemandValue InitDe-pendentDemand(
DateTime startDate,
DateTime endDate,
int counter,
int futureCounter,
bool isForDailyBucket)
{
//..
}
Incorrect
protected virtual void InitDependentDemand(
DateTime startDate,
DateTime endDate,
int counter,
int futureCounter,
bool isForDailyBucket,
out double dependentDemand,
out double dependentDemandFirm,
out double futureDependentDemand,
out double upperLevelForecast,
out double dependantDemandWithoutDestinationSO)
{
// ...
}
The Length of Method Names
How long could be the name of a method? The name should be as long as required. Do not abbreviate. Use your IDE’s autocomplete.
Correct
LoadCustomerSupportNotificationService, CreateMonthlyAndAnnualIncomesReport
Incorrect
LoadCustSuppSrvc, CreateMonthIncReport
Naming Method Parameters
Method parameters names preferred form: [Noun] or [Adjective] + [Noun]. Should be in camelCase. Should be meaningful. Unit of measure should be obvious.
Correct
firstName, report, supportTicket, recurringBillingOrder, sku
Incorrect
p, p1, p2, populate, LastName, last_name, convertImage
Single Purpose of All Methods
Methods should have a single purpose! Otherwise they cannot be named well. Methods that have multiple purposes (weak cohesion) are hard to be named. They need to be refactored instead of named.
Consistency in Methods Naming
Use consistent naming in the entire project. LoadFile, LoadImageFromFile, LoadSettings, LoadFont, LoadLibrary, but not ReadTextFile.
Use consistently the opposites at the same level of abstraction.
- LoadLibrary vs UnloadLibrary, but NOT FreeHandle
- OpenFile vs CloseFile, but NOT DeallocateResource
- GetName vs SetName, but NOT AssignName
Summary
In the second part of the naming guidelines series of articles we looked at various rules about naming methods which can make your tests much more readable and maintainable. In the next publications, we will go through a list of practices for naming properties and variables.
