I want to dispatch events to announce the progress of an asynchronous process. The event should contain 2 properties: work done and work total.
As the name suggests 😉 i could use ProgressEvent; it has bytesLoaded and bytesTotal properties that i can use. However, my async process isn’t loading bytes, its processing pixels, so the property names are a bit misleading for my use case – although the class name is perfect.
The alternative is to create a custom event with two properties that i can name how i like. But this means another class added to the code base.
So my question is; Is it better to reuse an existing class where the properties are suitable but maybe the naming isn’t ideal; Or to create a custom class that perfectly fits the requirement? Obviously, one extra class is no big deal, but OOP is all about reusing stuff so adding an unnecessary class does make me uneasy.
I await your thoughts…
PS: This is my first question on stack so be gentle
For clarity, I’d create a new class. Adding a new class is not much overhead at all, especially for something simple like an event. I find that code is more readable when I don’t have to make mental translations (like
bytesLoadedreally meanspixelsLoaded). To me this is akin to choosing poor names for variables.In addition, by going the other route and re-using the
ProgressEventclass, I would feel compelled to document the code to indicate that we’re dealing with pixels rather than bytes. That starts to get messy if you have a lot of classes that uses the event.Re-use is great, but I’d opt for clarity as long as it doesn’t impact your productivity or the app’s performance.