Skip to content
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

Remove mutable state from Evaluators and some related, poorly tested functions #655

Merged
merged 8 commits into from
Nov 30, 2024

Conversation

minichma
Copy link
Collaborator

The complexity of occurrence evaluation is impacted by the internal state maintained by classes implementing IEvaluator and related. This PR aims to reduce that complexity by eliminating much of this state. Only a few functions rely on this state, and these are either poorly documented, untested, or better suited for user implementation. The benefits of removing this state appear to significantly outweigh the drawbacks.

Most notable the following public methods are removed:

  • ToDo.IsCompleted(): poorly tested, i.e. only basic execution paths are tested, but not the more complex ones.
  • ToDo.IsActive(): see IsCompleted()
  • CalendarEvent.Occurs*(): untested and they seem to be better suited for user implementation.

As a benefit the following internal state can be eliminated:

  • IEvaluator.Periods
  • much of the other mutable state of IEvaluator, i.e. .Evaluation*Bounds
  • Evaluator._mEvaluation*Bounds

Copy link

codecov bot commented Nov 27, 2024

Codecov Report

Attention: Patch coverage is 65.62500% with 11 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
Ical.Net/CalendarComponents/Alarm.cs 0% 9 Missing ⚠️
Ical.Net/Evaluation/Evaluator.cs 0% 1 Missing ⚠️
Ical.Net/Evaluation/TodoEvaluator.cs 80% 1 Missing ⚠️

Impacted file tree graph

@@         Coverage Diff          @@
##           main   #655    +/-   ##
====================================
+ Coverage    60%    61%    +1%     
====================================
  Files       100     99     -1     
  Lines      4657   4555   -102     
  Branches   1106   1080    -26     
====================================
- Hits       2811   2787    -24     
+ Misses     1379   1302    -77     
+ Partials    467    466     -1     
Files with missing lines Coverage Δ
Ical.Net/Calendar.cs 63% <ø> (+2%) ⬆️
Ical.Net/CalendarCollection.cs 21% <ø> (+1%) ⬆️
Ical.Net/CalendarComponents/CalendarEvent.cs 71% <100%> (+3%) ⬆️
Ical.Net/CalendarComponents/RecurringComponent.cs 63% <ø> (-<1%) ⬇️
Ical.Net/CalendarComponents/Todo.cs 64% <100%> (ø)
Ical.Net/Evaluation/EventEvaluator.cs 67% <100%> (ø)
Ical.Net/Evaluation/RecurrencePatternEvaluator.cs 69% <100%> (-<1%) ⬇️
Ical.Net/Evaluation/RecurrenceUtil.cs 88% <ø> (+2%) ⬆️
Ical.Net/Evaluation/RecurringEvaluator.cs 82% <100%> (-1%) ⬇️
Ical.Net/VTimeZoneInfo.cs 36% <ø> (+1%) ⬆️
... and 3 more

@minichma minichma force-pushed the work/minichma/feature/remove_some_state branch from 54573de to a025e65 Compare November 28, 2024 14:49
@axunonb
Copy link
Collaborator

axunonb commented Nov 28, 2024

We could set nullable enable for changed files. Even one file per PR would be fine.

@minichma minichma force-pushed the work/minichma/feature/remove_some_state branch from 0949024 to b33e704 Compare November 28, 2024 15:43
@minichma
Copy link
Collaborator Author

It would certainly be great to make some progress on the nullable front, but I'd also appreciate not to grow the PR mor than necessary and keep concerns separated.

@minichma minichma requested a review from axunonb November 28, 2024 15:44
@minichma
Copy link
Collaborator Author

@axunonb What do you think of removing those methods I mentioned. It would certainly be possible to implement them also without the state that was removed, but I struggled with understanding the intended purpose, because partly untested and no sufficient docs. Do you agree that it should be ok to remove them in the first v5 version? Should people complain, we could re-evaluate and re-introduce.

@axunonb
Copy link
Collaborator

axunonb commented Nov 28, 2024

struggled with understanding the intended purpose, because partly untested and no sufficient docs

There is always a certain doubt when changing code without docs and missing unit tests. Sometime there is code that might aim to serve achieving a certain target, but with the full implementation left.
If cutting out doesn't break existing (other) unit tests let's be brave.

@minichma minichma marked this pull request as ready for review November 28, 2024 16:05
@minichma
Copy link
Collaborator Author

Alright, its just the two tests in TodoTest that I removed, but they also only covered certain execution paths.

@@ -13,37 +13,16 @@ namespace Ical.Net.Evaluation;

public abstract class Evaluator : IEvaluator
{
private DateTime _mEvaluationStartBounds = DateTime.MaxValue;
private DateTime _mEvaluationEndBounds = DateTime.MinValue;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a sidenote: Initializing with DateTime.MinValue in ical.net is a potential risk because we can't create a DateTimeOffset with it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely agree. So another benefit of them being gone. There are quite many of those magic values throughout the lib. Should implement Nullable in most of those places.

Copy link
Collaborator

@axunonb axunonb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All fine, if you think CodeCov coverage warnings can be neglected.

@axunonb
Copy link
Collaborator

axunonb commented Nov 28, 2024

It would certainly be great to make some progress on the nullable front, but I'd also appreciate not to grow the PR mor than necessary and keep concerns separated.

Yep, let's try to pick one file after the other, instead of a big bang. If helpful, include in a PR, otherwise chose a separate PR.

@minichma
Copy link
Collaborator Author

All fine, if you think CodeCov coverage warnings can be neglected.

Yes, I think so. The number of missed lines is reduced. The reduced number of hits originates from code that has been deleted, which basically is good for the code quality. We don't have new issues and I think that most new code is covered. The modified code that isn't covered hasn't been covered before as well and I don't feel in the position to write all those tests in a meaningful way at this time.

Most notably the TimeZoneEvaluator isn't covered, because it isn't used anywhere and not tested either. The reason seems to be that timezone evaluation is done via NodaTime, so maybe it would be best to remove it altogether, as nobody seems to be able to maintain it. And restore it, should demand arise? What do you think?

@axunonb
Copy link
Collaborator

axunonb commented Nov 28, 2024

timezone evaluation is done via NodaTime, so maybe it would be best to remove it altogether,

Good finding, and I'm sure nobody will even notice that it's gone. Yes, let's remove the class.

…ng the dependency on `PeriodListEvaluator`. Make related methods private or internal. Avoid using `IEvaluator.Periods` so it can be removed.
…ounds` and `.Periods` properties, because it isn't needed anymore and removing it vastly reduces complexity.
…y on internal state of `Evaluator` that should be removed. They are fully untested, so the intended functionality and usage pattern are rather unclear. Moreover it seems that the functionality can be implemented by the user quite easily and exactly to their requirement.
@minichma minichma force-pushed the work/minichma/feature/remove_some_state branch from b33e704 to 64c1847 Compare November 29, 2024 09:53
Copy link

sonarcloud bot commented Nov 29, 2024

@minichma
Copy link
Collaborator Author

Removed the TimezoneEvaluator. Was a little unfair regarding the tests of Todo though. There was quite some code related to testing Todo.IsComplete and .IsActive, although they don't cover all the code paths. Anyhow, I restored them and simplified the code so it doesn't rely on IEvaluator.Period state, which is to be removed, so everything fine with that now.

Copy link
Collaborator

@axunonb axunonb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks

@minichma minichma merged commit e77681c into main Nov 30, 2024
5 of 7 checks passed
@minichma minichma deleted the work/minichma/feature/remove_some_state branch November 30, 2024 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants