Simplify time assignment logic in AddModifyItem#15
Merged
Conversation
Refactor the `switch` statement in `AddModifyItem` to remove explicit casting of `newTime` to `TimeSpan`. Instead, directly assign the `Value` property of `newTime` to `ItemToModify.Bedtime` and `ItemToModify.Waketime`. This improves code clarity and reduces the risk of casting errors.
Codecov Report✅ All modified and coverable lines are covered by tests. 🚀 New features to boost your workflow:
|
jdubar
approved these changes
Nov 10, 2025
Owner
jdubar
left a comment
There was a problem hiding this comment.
Nice, simple change!! That's really cool - I wouldn't have known about Value otherwise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Simplify time assignment logic in AddModifyItem
Refactor the
switchstatement inAddModifyItemto remove explicit casting ofnewTimetoTimeSpan. Instead, directly assign theValueproperty ofnewTimetoItemToModify.BedtimeandItemToModify.Waketime. This improves code clarity and reduces the risk of casting errors.Why?
While casting does work, it's heavy-handed and possibly slower (ever so slightly).
newTimeis already of typeTimeSpan?(orNullable<TimeSpan>). It already knows its "inner" type isTimeSpan; it doesn't need to be told this. The problem is that this is a nullable type, so how do you get from nullable to "not nullable" AFTER you know that it's not null (because of your earlier check).The correct way is this notation
.Value.This says "OK, it's not null, so get the value then."
Also, though I think I prefer the
newTime is nullbecause it is expressed in "positive" logic, there is a "1st half" to the.Valuenotation for Nullables.You can write this:
if (!newTime.HasValue)instead of what you have:
if (newTime is null)However, again, I prefer to read "positive", as opposed to "negative".
And
new time is nullreads A LOT BETTER THANnot new time has value... YUCK!IF, however, the condition were the inverse, again because it is "positive" I would prefer this:
if (newTime.HasValue)over the negative
if (newTime is not null)They've made C# syntax so much easier to read around null checks, so yes, this second line isn't awful. It still reads just fine, but
newTime.HasValue, especially when the next code would benewTime.Value, it fits.Thinking in negatives is hard for most Humans.
Last note
A good reason why you probably haven't seen
.HasValueand.Valuemuch for nullables is because thenull propagation?.andnull coalesce??operators:newTime?.Seconds ?? 0;Has helped to reduce so much null-check boilerplate code.