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

  • deadcream@kbin.social
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    I prefer to encode quantities as types (and store value with most precision inside) and provide functions that return it in desired unit as int/long/whatever.

    E.g. Duration type that stores nanoseconds and has to_seconds(), to_milliseconds() etc. It just feels more natural to me. Why should some function care which units are used? It just needs “duration” and will convert to desired unit internally (also it won’t be part of its api which is good because it’s unnecessary restriction).

    Of course some C++ devs will disdain this approach because it’s inefficient to pass highest precision value around when its not needed but for my use cases it doesn’t matter.

    Also, you should always use standard types when available. E.g. C++ has std::chrono, in Java world there are java.time types and kotlin.Duration.