Naming query variables

After working with the different framework and libraries like Laravel, PHP, ASP.NET MVC, ExpressJS and other technologies, I realized 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.
  • evenNumberQuery 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.

Be declarative wherever possible

Imperative programming focuses on describing how a program operates. Take an example of the below function “ isPresent “.

First example: Imperative way, Look implementation of isPresent function.

Of course, it searches a number (search term) in the numbers array and replies with true or false.

If you observe carefully, it focuses on how the number is searched.

It iterates each number one by one in an array of numbers and checks if criteria are fulfilled (HOW part: for/if). Eventually, it tells you if the number is present/not in the array.

Declarative programming, on the other hand, expresses the logic of a computation without describing its control flow.

Let’s look at the declarative way of searching a number in an array.

Declarative way: Any function from LINQ.

In the above code, the method Any (present on array data type), is an extension method provided by LINQ.

It determines whether any element of a sequence contains a given number. It does not tell you how it is searched. That part is hidden behind API: Any.

The benefit of a declarative approach over the imperative approach is improved readability of code.

Another example:

Second example: Imperative way. See containsAllMen function: focuses on describing how a program operates (Control flows are involved: iterate and if)

This function “ containsAllMen “ tells you how searching is implemented and control flow like if clause determine the condition.

Now see the declarative way, it is using lodash’s every to determine the condition.

The declarative way: Using every function from lodash.

This technique is language agnostic.

You search libraries that do such work for you instead of implementing it by yourself.

Return as early as possible

Do you know how to check if the given year is a leap year or not?

Let us go through a simple leap year code snippet.

 

Tells you whether the given year is a leap year or not.

The above code snippets read like:

If a given year is divisible by 4, 100, and 400 then given number is a leap year.

If a given year is divisible by 4 and 100 but it is not divisible by 400 then it is NOT a leap year.

If a given year is divisible by 4, but it is NOT divisible by 100 then it is a leap year.

Otherwise given number is NOT a leap year.

Did you realize that readability is poor when you use deeply nested conditionals?

Even a simple program becomes difficult to understand.

This is called Arrow anti-pattern because too many nested if represent an arrow-like shape.)

Readability could be achieved by returning as early as possible.

Let’s see via example.

Returning as early as possible

Don’t you think the second one is easier to understand?

It reads like

If a given year is divisible by 400 then it’s a leap year.

Otherwise if divisible by 100 then it’s NOT a leap year.

Otherwise if divisible by 4 then it’s a leap year.

Oherwise it’s NOT a leap year.

Dattatray Kale | Being software craftsman
I am an application developer based in Pune, India. I am willing to wait for the right opportunity – Datta CV PRACTICES…blog.beingcraftsman.com