Simplify LocalDate(Time) constructor parameter names
See original GitHub issueThe parameter names are very weird for day-to-day use:
LocalDateTime(year = 2021, monthNumber = 1, dayOfMonth = 1, hour = 12)
I don’t care that month
is a number. That’s what we have a type system for.
OfMonth
for day
is also redundant as that’s obvious from the context.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Refactoring Long Parameter List in Constructors (Java)
Option 1: Use builder pattern Here is the example from IntelliJ IDEA: Select the constructor with long parameter list -> “Refactor” -> “Replace ......
Read more >Why does LocalDateTime not have a String constructor?
You can create an instance of these classes only by calling one of the static factory methods (with ~. parse() being one of...
Read more >Migrating from 1.x to 2.0 - Noda Time
Parameter names. Some parameters have been renamed for consistency. ... The LocalDateTime constructors accepting tick values have been removed.
Read more >LocalDateTime (Java Platform SE 8 ) - Oracle Help Center
LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, ...
Read more >Java – Spring: create LocalDate or LocalDateTime ... - iTecNote
In a spring project, I'd like to create a LocalDate from an @Autowired constructor parameter whose value is in a .properties file.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I believe that numbering months from zero is an abomination and should not be considered a normal practice that we take into account when naming the corresponding parameter/property.
You make a good point about
dayOfMonth
. I agree, nobody will be confused about the meaning ofLocalDateTime.day
.Regarding
monthNumber
,Month
is a nice, well-typed, unambiguous thing.monthNumber: Int
, on the other hand, is not. Unfortunately, many systems use zero-based numbering for months, even while using one-based numbering for days. JavaScript comes to mind:So, it makes sense for
LocalDate(Time)?
to highlight an area that would give potentially misleading results by using a longer name. “monthNumber
? What kind of a number? The usual kind, 1…12, or zero-based?” If we manage to provoke a question of this kind, we have won.In your example (which, I assume, is simplified, but nonetheless),
Month.DECEMBER
is, I think, a better choice, which should be more obvious with a less prominent month number, like 5 or 10.WDYT?