Identify if a Variable Holds a Date, a Time, or Both [VD18]

Identify if a Variable Holds a Date, a Time, or Both [VD18]

Tip VD18

Clearly indicate in variable names what kind of “date” or “time” information is stored in a variable so that programmers know what to expect.

The following guidelines can provide distinctions:

  • If a variable just holds a date without time information, use date terminology.

  • If a variable just holds a time without date information, use time terminology.

  • If a variable holds both, indicate that in the name with a phrase like date_time (shorthand for “date/time”, “date and time”, or “date with time”), which is a concise phrase that leaves plenty of room for other important adjective phrases in variable names.

    • Timestamp is another term that is often understood to contain both “date” and “time” components and thus can be acceptable to use. However, it may not be as clear as date_time, which can more clearly indicate that both “date” and “time” components are involved.

These naming practices will prevent a programmer from trying to use a variable and not being able to get the data they expect (or getting more data than anticipated). While some programming languages have distinct data types for these different entities that can help prevent misuse, it’s best to include relevant information in the names themselves since that information will be available everywhere a programmer might read the variable name being used.

The following examples show these distinctions:

Find the experts Ad.png