DEV Community

Oliver Davies
Oliver Davies

Posted on • Originally published at oliverdavies.uk on

Please don't use short variable names

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.

P.S. Need help or want another pair of eyes on your code? Book a 1-on-1 consulting call or an online pair programming session with a 100% money-back guarantee.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay