55
A quick tip for better variable names
(programming.dev)
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Follow the wormhole through a path of communities !webdev@programming.dev
Those are just types. You shouldn't write types in the names. It's called Hungarian Notation, but it's just redundant. If you need to check the type of a variable, hover over it and your IDE should tell you that
temperatureThreshold
is typeDegreesCelsius
. No need to add extra cruft. There's also a question of how specific everything needs to be.This is especially problematic if you later refactor things. If you change units, then you have to rename every variable.
Plus, variables shouldn't really be tied to a specific unit. If you need to display in Fahrenheit, you ideally just pass
temperatureThreshold
and it converts types as needed. ATemperature
type that that hasdegreesF()
anddegreesC()
functions is even cleaner. Units should just be private to the type's struct.I absolutely agree. But:
Obviously as a Hungarian I have a soft spot for Hungarian notation :) But in these cases I think it's necessary.
Not sure what languages you commonly work with, but in good modern languages you can simply declare "feet" as an alias of integer (or double?), and no refactoring would be required.
And any good toolchain to parse / generate JSON/etc can absolutely get the types right.