The only reason I can think of is that dates are immutable objects, so by calling plusDays you're not adding days to the original object but creating a new one with new properties, but that's very vary subtle.
This is exactly the reason. Imagine you had some kind of api for manipulating ranges of dates for scheduling purposes. It might expose methods letting you make a statement like:
var workdaySchedule = initialSchedule.withoutWeekends();
This reads very similarly to the English statement: "The workday schedule is the initial schedule without weekends". It doesn't imply changing the initial schedule, it implies the work schedule being a different, new thing.
Now instead imagine it was named:
var workdaySchedule = initialSchedule.removeWeekends();
This is confusing. Is initial schedule being modified? It certainly sounds like it, because it sounds like we're removing weekends from it. But then why are we assigning it to a new variable? Although these two naming schemes are very similar, this one is much less clearly evocative of what's happening. This would be more appropriate if removeWeekends
did change the initial schedule, and returned void- in which case withoutWeekends
would be the confusing option.
This is essentially a declarative vs. imperative distinction. Are we declaring that the workdaySchedule
is a particular thing, or are we carrying out a list of imperative instructions (like "remove") to make that particular thing? Usually, imperative naming makes more sense when you're mutating values, and declarative makes more sense with immutable values, as the above example demonstrates.
In your case, you have exactly the same thing. If I saw: tomorrow.plusDays
, I wouldn't imagine that tomorrow
was being mutated, whereas tomorrow.addDays
, I'd think it might be. This is somewhat subtle- but not necessarily in a bad way. Without having to think about it too hard, this naming naturally sets your thinking along the right lines in terms of whether or not you're mutating. To make this distinction between these imperative and declaritive styles clearer: "add" (and "remove") are verbs, whereas "plus" (and "without") are prepositions.
sqrt
which takes the square root. Naming this methodtakeSqrt
might seem to make sense according to your rule, but naming it this would not make the method more readable nor would it make it clearer.sqrt
is just a word that programmers are expected to recognize and know. The English word is "square root" by the way. But naming things according to what is natural in English is not good. Take the word "illicit", for example, a perfectly fine English word. However, if someone named their method, sayisIllicit
I think I would want to tear out my eyeballs every time I looked at this method call. It just looks awful and there has to be a better way to express the idea.sum
sounds wrong in this context. I prefer .net'sAddDays
.Math.addExact(1, 2)
because you say "add 1 and 2".tomorrow.plusDays(2)
because you say "tomorrow plus 2 days". IfaddExact
were a member ofInteger
somehow it would have been1.plusExact(2)
.plusDays
to return a new date x number of days into the future, whereasaddDays
I might expect to mutate the original object. That's just me though, I'm not all that familiar with Java.