3 Minutes with Kent

Single Letter Variables (and Types) Considered Harmful

01.11.2021 - By Kent C. DoddsPlay

Download our free app to listen on your phone

Download on the App StoreGet it on Google Play

Hey there friends Today is January 11th 2021, and I want to talk about

single variable names in TypeScript. Well think of variable names in

general, but for some reason in TypeScript when we're making like generic

types or type names whatever we've just I don't know where it came from.

I guess people say it came from C sharp and stuff they do this, but using

the letter T to denote the type force like a generic or something. Pretty

ridiculous. I mean, there are some situations where it's obvious that like

what what the types are.And there's no real better name necessarily

assuming that everybody understands the convention that T stands for type.

But there are so many cases where you've got a couple of different types

that you're providing. Each one has a unique purpose and we could just

communicate that purpose with a actual variable name but instead for some

reason we're using single letter variables. I'm not sure where this came

from but it does need to stop please stop doing this and just use a

descriptive variable name.

And now like this is more like in general, I just noticed.This a lot with

TypeScript definitions you know And it's fine if it's like you're inside of

your own little function and you know like with a map or something we are

mapping over some data and you use D or whatever.

I don't really care about that. I'm just talking about when you're exposing

types to consumers. You wouldn't have like a single letter function that

your consumers are calling or or like objects with single letters for D is

duration, like nobody knows that. So given an actual variable name so that

people know what they're working with and what they need to pass.

As those arguments and we're better I don't like I feel like we don't need

to have this conversation because it's kind of one of those things that we

just everybody knows a single letter variable names not a good thing. Types

are no different make your types longer variable names so that we know what

those things are for.

It's kind of a rant but I yeah just keep in mind that people if you're

going to expose something as like for other developers to use the

developers are going to use them and they may not know what you're talking

about when you.Using single letter variables. So yeah, that's all that.

I have to say about that. I hope you have a stellar week and we'll catch

you all around. Bye.

More episodes from 3 Minutes with Kent