• 0 Posts
  • 2 Comments
Joined 2 years ago
cake
Cake day: June 30th, 2023

help-circle

  • I feel like variable or function names that become overly verbose indicate that a specific type or a separate class should be considered.
    I see it as a mild code smell.

    Something like int intervalSeconds = 5 could maybe have a type that extends an int called seconds. So then you are declaring seconds Interval = 5.
    It describes the unit, so the variable name just describes the purpose.
    You could even add methods for fromMinutes etc. to reduce a bunch of (obvious) magic numbers elsewhere.

    To extend this contrived example further, perhaps there are a couple of intervals. A refresh, a timeout and a sleep interval.
    Instead of having.

    int sleepIntervalSeconds = 0;
    // etc...
    

    You could create an intervals class/object/whatever.
    So then you have.

    public class Intervals {
        public seconds Sleep
        public seconds Refresh
        public seconds Timeout
    }
    

    The class/object defines the context, the type defines the unit, and you get nice variable names describing the purpose.