Which one do you prefer? A. Explicit `.parse` : `...
# library-development
c
Which one do you prefer? A. Explicit
.parse
:
Copy code
val names = json at JsonPath.parse("$.*.name")
B. Implicit parse:
Copy code
val names = json at JsonPath("$.*.name")
🅱️ 6
🅰️ 1
j
I believe in this case the action of parsing is not what matters to the caller. So I prefer B because it looks like making a typed version of the string pattern, which is what matters to me (a bit like regexes)
c
Ah, Regex is a good example indeed. I was initially thinking B was more readable, but then I thought of
Instant.parse
,
LocalDate.parse
,
String.valueOf
, etc and I couldn't find an example in the stdlib that just used a constructor.
h
It depends on the use case: your sample looks like a DSL, so I would prefer to not use a parse function to improve readability. In std lib/normal usage, I would prefer a parse function to make clear, parsing the function could throw a parsing error, and it is easy to create a parseOrNull function.
A InstantOrNull function looks strange (to me)
2
true 3