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

  • cgtjsiwy
    link
    fedilink
    English
    arrow-up
    20
    ·
    2 years ago

    In languages with static and convenient type systems, I try to instead encode units as types. With clever C++ templating, you can even get implicit conversions (e.g. second -> hour) and compound types (e.g. meter and second types also generate m/s, m/s^2 and so on).

    • CoderKat@kbin.social
      link
      fedilink
      arrow-up
      4
      ·
      2 years ago

      A good example is Go’s time package. You’d normally express durations like 5 * time.Second and the result is a time.Duration. Under the hood, it’s just an int64 nanoseconds, but you’d never use it as a plain nanoseconds. You’d instead use it like d.Seconds() to get whichever unit you desire.

    • deadcream@kbin.social
      link
      fedilink
      arrow-up
      3
      ·
      2 years 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.

    • janAkali@lemmy.one
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      2 years ago

      I like how in Nim you can create a type and then overload default operators to support custom types (operators are just functions with 2 arguments in Nim), in example: you can do Hours + Seconds or kilometers * miles, etc. It feels very organic and not like a “hack”.