New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduced 'added' and 'subtracted' functions #132
Conversation
…dding and subtracting time but returning a modified object copy, leaving the original moment object intact. Also made a short, simple unit test and updated the docs. Did not commit build generated files.
Isn't this just the following?
I think it may be confusing to have both an |
Yes, I see your point. However I think that programming with immutable types is a good idea, and code with immutable types has a much lower bug frequency. These 2 functions encourage keeping the moment objects immutable by providing a clear and short expression. I think that many API users expect something like these functions to exist, and I believe it increases the readability of the code. Personally I would prefer to only have the immutable versions of these functions, but that would break existing code and also possibly present a performance problem in some scenarios. In a purist stance, I believe it is a design mistake to have a datetime type mutable. |
FWIW, I was also surprised at having to do |
Native dates in javascript are mutable.
Wouldn't it stand to reason that a wrapper around native dates is also mutable? |
I'd suggest one of the nice things a wrapper could do is provide an immutable interface to the native mutable data type. |
I am hesitant to do this because of a couple reasons. 1 Performance decrease While it's not a huge decrease, it is around 25-35% slower to switch to immutable. This is because we have to clone the moment any time we want a modification. This will also increase memory usage as well. 2 No way for developers to use mutable moments Right now, moments are mutable with the option to clone, leaving the first moment untouched. If we force cloning, there is no longer an option. All manipulation methods will clone. Perhaps a better solution would be to make it clear in the docs that when you use |
You'd have to define mutable versions of the methods regardless, at least for use by the library internally, at which point, you might as well expose them anyway. Blue sky, I'd have gone with So as much as I like functional methods to be the "obvious" ones (it would spare my code base about a hundred |
One argument from my side against the performance aspect, is that I use clone() a lot - I prefer to not modify existing objects whenever possible, and so I wouldn't experience much of a performance decrease. I do agree that this change makes the API slightly more confusing. The "good" thing about this though, is that it forces the API users to think about the fairly important aspect of object mutability. timrwood: Don't feel bad if you decide to reject this pull request, it's ok. |
Yeah, I think the problem is getting people to think about whether or not to I'll add documentation on this, so hopefully that will help. |
Introduced 'added' and 'subtracted' functions on moment prototype, for adding and subtracting time but returning a modified object copy, leaving the original moment object intact. Also made a short, simple unit test and updated the docs. Did not commit build generated files.