Whenever you store a value that has a unit in a variable, config option or CLI switch, include the unit in the name. So:
maxRequestSize
=>maxRequestSizeBytes
elapsedTime
=>elapsedSeconds
cacheSize
=>cacheSizeMB
chargingTime
=>chargingTimeHours
fileSizeLimit
=>fileSizeLimitGB
temperatureThreshold
=>temperatureThresholdCelsius
diskSpace
=>diskSpaceTerabytes
flightAltitude
=>flightAltitudeFeet
monitorRefreshRate
=>monitorRefreshRateHz
serverResponseTimeout
=>serverResponseTimeoutMs
connectionSpeed
=>connectionSpeedMbps
EDIT: I know it’s better to use types to represent units. Please don’t write yet another comment about it. You can find my response to that point here: https://programming.dev/comment/219329
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.It’s also 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 warranted.
There are plenty of times where the type is just something generic like an integer and making a wrapper type is not worth the effort and this is a useful approach.