Blog post

Naming query variables

Dattatray Kale

-
August 10, 2019
Clean Code
Programming
Software Design
Software Craftsmanship

After working with the different framework and libraries like Laravel, PHP, ASP.NET MVC, ExpressJS and other technologies, I realised that I should write about naming query variables. Perhaps, variable that holds the query and executes it later (Deferred execution).

Sometimes, when developers of any language or framework choose to write the query that gets data using deferred execution, they don’t include the word “query” in the variable name.

Due to exclusion of word `query` in such a variable name, the maintenance developer presumes that the query will be executed immediately. This assumption leads them to introduce bugs in their code.

Please ensure to include the word `query` in the variable name so that it conveys the reader that it is NOT to be executed immediately.

As always, we have to provide meaningful and intuitive names to query variables. See some of the examples below:

Naming query variable examples

  • puneCustomersQuery: When I want to fetch the customers in Pune locations.
  • allPackagesQuery: When I want to fetch all the packages.
  • evenNumbersQuery: When I want to fetch even numbers from a given data source.
  • damagedPackagesQuery: When I want to fetch packages that are marked as damaged.
  • customersByCityQuery: When I want to fetch customers that are grouped by their city.
  • customerNameQuery: When I want to fetch customer names from a given data source.
  • soldOutProductsQuery: When you are fetching the sold-out products.

Originally published at https://beingcraftsman.com on February 2, 2019.

Related Blog Posts