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

Refactor away the bool argument of Timer #2926

Closed
Masterhigure opened this issue Oct 7, 2021 · 5 comments
Closed

Refactor away the bool argument of Timer #2926

Masterhigure opened this issue Oct 7, 2021 · 5 comments
Labels
C-Feature A new feature, making something new possible

Comments

@Masterhigure
Copy link

Masterhigure commented Oct 7, 2021

The bool argument to the Timer constructors should be refactored away.

A function that takes in a bool argument almost necessarily has an internal if-test that can be refactored into two separate functions. And the significance of the bool argument is almost never self-explanatory from reading the function call. (Consider, for instance, the first time you read Timer::from_seconds(2.0, true) in chapter 2.5 of the current Bevy book: The meaning of 2.0 is obvious, but what does a true Timer mean? How can a Timer be false?) These are code smells. In addition, the name Timer, at least to me, does not imply an automatic looping capability, because that's not how the timers you buy in hardware stores work (although I'm willing to concede this particular point).

Also, if someone would want to extend Timer in the future to include more modes, this bool approach is unfeasible.

I propose that Timer is split into two separate timer structs. For instance Timer and LoopingTimer, or TimerOnce and TimerRepeating. (I am not terribly attached to these particular names, and other suggestions are welcome.)

Other solutions (from comments below) include splitting the constructors, or swapping the bool for a more verbose enum TimerMode, both of which are self-explanatory at point of call, and also extensible should other kinds of Timers be desired.

This is related to issue #1127 that wants a Cooldown-type timer.

@Masterhigure Masterhigure added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Oct 7, 2021
@NiklasEi
Copy link
Member

NiklasEi commented Oct 7, 2021

Another option would be to replace the boolean with an enum TimerMode. That could then also be extended with e.g. TimerMode::Cooldown.

@bjorn3
Copy link
Contributor

bjorn3 commented Oct 7, 2021

It would also be possible to use two constructors instead of two structs.

@Masterhigure
Copy link
Author

Another option would be to replace the boolean with an enum TimerMode. That could then also be extended with e.g. TimerMode::Cooldown.

That is... maybe a better option. I'll include it in the main post.

@NiklasEi NiklasEi removed the S-Needs-Triage This issue needs to be labelled label Oct 7, 2021
@Masterhigure Masterhigure changed the title Split Timer into two Refactor away the bool argument of Timer Oct 7, 2021
@Masterhigure
Copy link
Author

Masterhigure commented Oct 7, 2021

Maybe if we resolve this in a way that keeps a single struct, Timer and Stopwatch could be merged? I haven't looked too hard at the available functionalities on either to see if it would make sense, but at a quick glance it doesn't seem entirely unreasonable, neither functionality-wise nor coding-wise.

@arialpew
Copy link

arialpew commented Oct 7, 2021

There's a PR for DiscreteTimer and DiscreteStopWatch to : #2683 (very usefull for FixedTimeStep).

If there's a change, I guess this need to be refined.

@bors bors bot closed this as completed in 73605f4 Oct 17, 2022
james7132 pushed a commit to james7132/bevy that referenced this issue Oct 19, 2022
)

As mentioned in bevyengine#2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation.

This also opens up a way to merge different timers, such as `Stopwatch`, and possibly future ones, such as `DiscreteStopwatch` and `DiscreteTimer` from bevyengine#2683, into one struct.

Signed-off-by: Lena Milizé <me@lvmn.org>

# Objective

Fixes bevyengine#2926.

## Solution

Introduce `TimerMode` which replaces the `bool` argument of `Timer` constructors. A `Default` value for `TimerMode` is `Once`.

---

## Changelog

### Added

- `TimerMode` enum, along with variants `TimerMode::Once` and `TimerMode::Repeating`

### Changed

- Replace `bool` argument of `Timer::new` and `Timer::from_seconds` with `TimerMode`
- Change `repeating: bool` field of `Timer` with `mode: TimerMode`

## Migration Guide

- Replace `Timer::new(duration, false)` with `Timer::new(duration, TimerMode::Once)`.
- Replace `Timer::new(duration, true)` with `Timer::new(duration, TimerMode::Repeating)`.
- Replace `Timer::from_seconds(seconds, false)` with `Timer::from_seconds(seconds, TimerMode::Once)`.
- Replace `Timer::from_seconds(seconds, true)` with `Timer::from_seconds(seconds, TimerMode::Repeating)`.
- Change `timer.repeating()` to `timer.mode() == TimerMode::Repeating`.
james7132 pushed a commit to james7132/bevy that referenced this issue Oct 28, 2022
)

As mentioned in bevyengine#2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation.

This also opens up a way to merge different timers, such as `Stopwatch`, and possibly future ones, such as `DiscreteStopwatch` and `DiscreteTimer` from bevyengine#2683, into one struct.

Signed-off-by: Lena Milizé <me@lvmn.org>

# Objective

Fixes bevyengine#2926.

## Solution

Introduce `TimerMode` which replaces the `bool` argument of `Timer` constructors. A `Default` value for `TimerMode` is `Once`.

---

## Changelog

### Added

- `TimerMode` enum, along with variants `TimerMode::Once` and `TimerMode::Repeating`

### Changed

- Replace `bool` argument of `Timer::new` and `Timer::from_seconds` with `TimerMode`
- Change `repeating: bool` field of `Timer` with `mode: TimerMode`

## Migration Guide

- Replace `Timer::new(duration, false)` with `Timer::new(duration, TimerMode::Once)`.
- Replace `Timer::new(duration, true)` with `Timer::new(duration, TimerMode::Repeating)`.
- Replace `Timer::from_seconds(seconds, false)` with `Timer::from_seconds(seconds, TimerMode::Once)`.
- Replace `Timer::from_seconds(seconds, true)` with `Timer::from_seconds(seconds, TimerMode::Repeating)`.
- Change `timer.repeating()` to `timer.mode() == TimerMode::Repeating`.
Pietrek14 pushed a commit to Pietrek14/bevy that referenced this issue Dec 17, 2022
)

As mentioned in bevyengine#2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation.

This also opens up a way to merge different timers, such as `Stopwatch`, and possibly future ones, such as `DiscreteStopwatch` and `DiscreteTimer` from bevyengine#2683, into one struct.

Signed-off-by: Lena Milizé <me@lvmn.org>

# Objective

Fixes bevyengine#2926.

## Solution

Introduce `TimerMode` which replaces the `bool` argument of `Timer` constructors. A `Default` value for `TimerMode` is `Once`.

---

## Changelog

### Added

- `TimerMode` enum, along with variants `TimerMode::Once` and `TimerMode::Repeating`

### Changed

- Replace `bool` argument of `Timer::new` and `Timer::from_seconds` with `TimerMode`
- Change `repeating: bool` field of `Timer` with `mode: TimerMode`

## Migration Guide

- Replace `Timer::new(duration, false)` with `Timer::new(duration, TimerMode::Once)`.
- Replace `Timer::new(duration, true)` with `Timer::new(duration, TimerMode::Repeating)`.
- Replace `Timer::from_seconds(seconds, false)` with `Timer::from_seconds(seconds, TimerMode::Once)`.
- Replace `Timer::from_seconds(seconds, true)` with `Timer::from_seconds(seconds, TimerMode::Repeating)`.
- Change `timer.repeating()` to `timer.mode() == TimerMode::Repeating`.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this issue Feb 1, 2023
)

As mentioned in bevyengine#2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation.

This also opens up a way to merge different timers, such as `Stopwatch`, and possibly future ones, such as `DiscreteStopwatch` and `DiscreteTimer` from bevyengine#2683, into one struct.

Signed-off-by: Lena Milizé <me@lvmn.org>

# Objective

Fixes bevyengine#2926.

## Solution

Introduce `TimerMode` which replaces the `bool` argument of `Timer` constructors. A `Default` value for `TimerMode` is `Once`.

---

## Changelog

### Added

- `TimerMode` enum, along with variants `TimerMode::Once` and `TimerMode::Repeating`

### Changed

- Replace `bool` argument of `Timer::new` and `Timer::from_seconds` with `TimerMode`
- Change `repeating: bool` field of `Timer` with `mode: TimerMode`

## Migration Guide

- Replace `Timer::new(duration, false)` with `Timer::new(duration, TimerMode::Once)`.
- Replace `Timer::new(duration, true)` with `Timer::new(duration, TimerMode::Repeating)`.
- Replace `Timer::from_seconds(seconds, false)` with `Timer::from_seconds(seconds, TimerMode::Once)`.
- Replace `Timer::from_seconds(seconds, true)` with `Timer::from_seconds(seconds, TimerMode::Repeating)`.
- Change `timer.repeating()` to `timer.mode() == TimerMode::Repeating`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Feature A new feature, making something new possible
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants