From de48c8c90187aa554978ac7399dae639cd86e56c Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 17 Jan 2024 23:25:45 +0000 Subject: [PATCH] Add daily email for 2024-01-17 Please don't use short variable names --- source/_daily_emails/2024-01-17.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 source/_daily_emails/2024-01-17.md diff --git a/source/_daily_emails/2024-01-17.md b/source/_daily_emails/2024-01-17.md new file mode 100644 index 00000000..a8ce658d --- /dev/null +++ b/source/_daily_emails/2024-01-17.md @@ -0,0 +1,30 @@ +--- +title: Please don't use short variable names +date: 2024-01-17 +permalink: archive/2024/01/17/short-variable-names +snippet: | + Why use variable names like $k, $v and $i instead of $key, $value and $index? What benefit does it add? +tags: + - software-development + - clean-code +--- + +When learning to code, one of the most confusing things was using short variable names in documentation and other people's code. + +Things like `$k` and `$v` instead of `$key` and `$value` within loops, `$i` instead of `$index`, or `$e` when working with Exceptions. + +I've also seen slightly better names, such as `$idx` for index or `$ctx` for context. + +But what does this achieve? + +Why not write the full variable name and clarify what it refers to? + +It would be easier to read and understand for anyone reading the code, including Junior Developers and people new to your team or application. + +There are no limitations - at least in the languages I use - to force this, such as a maximum number of characters in a file, so why not write the full variable name? + +The only reason I can think of is to save time by pressing fewer keys, but code is read more than written, so it should be optimised for readability. + +Your tests and CI pipeline don't have a preference. + +The people reading the code will.