-
Notifications
You must be signed in to change notification settings - Fork 1
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
A well-organized system #3
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An improvement, to be sure - there are still things we would benefit from changing, but even so, general changes to program behavior should now be much easier.
const currentTimeInfo = new TimeInformation( | ||
working, | ||
workTime, | ||
restTime, | ||
timeLeft | ||
); | ||
|
||
theText = standardText(timeFormatter.textFrom(currentTimeInfo)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section of code now adheres to the Open-Closed Principle.
if (resetTimer) { | ||
buttons.forEach((b) => b.remove()); | ||
endTime = getTargetTime(working ? workTime : restTime); | ||
buttons = [ | ||
makeButton( | ||
"Reset", | ||
(currentButtonsAmount = modes.length), | ||
resetProgram | ||
), | ||
]; | ||
|
||
soundEffect.play(); | ||
|
||
resetTimer = false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be better, we should strive for separation of concerns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be ideal to make a function call instead of setting a global variable used in another function, maybe making a resetTimer
function would do the job.
windowWidth / 2 - buttonWidth / 2, | ||
windowHeight / 2 + | ||
idx * buttonHeight - | ||
(modes.length * buttonHeight) / 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is complex logic that would be good to describe in more detail.
) | ||
) | ||
.map((b) => b.size(buttonWidth, buttonHeight)) | ||
.map((b, idx) => b.mousePressed(() => setMode(modes[idx]))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may not be the best solution, but at least we depend only on a parameter.
button.position( | ||
windowWidth / 2 - buttonWidth / 2, | ||
windowHeight / 2 + | ||
currentButtonsAmount * buttonHeight - | ||
(totalButtons * buttonHeight) / 2 | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic looks to be duplicated in makeButtonsFromModes
. Needless to say, this is not ideal.
<body> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script> | ||
<script src="static/js/p5.sound.js"></script> | ||
<script type="module" src="static/js/main.js"></script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separation of concerns - I called this one "Code-Interface Segregation".
|
||
function resetProgram() { | ||
//executes once to reset program | ||
buttons.forEach((b) => b.remove()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do see this call repeated in a few places. It's probably wise to extract this into its own function.
var timerRunning = false; | ||
var buttons = []; | ||
var currentWorkTime, currentRestTime; | ||
var working = true; //false=resting | ||
var currentButtonsAmount = 0; | ||
var buttonWidth, buttonHeight; // Should be constant, but due to the way processing works, must be set in `setup()`. | ||
var endTime; | ||
var resetTimer = false; //for pomodoro screen | ||
var theText; //for pomodoro screen - global | ||
var timeLeft; | ||
var soundEffect; // Should be constant. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these should be able to be turned into let
without unexpected behavior. Ideally we would remove as many variables as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a dataclass? Could this design be better?
return { | ||
hours: Math.floor(diff / 60 / 60), | ||
minutes: Math.floor(diff / 60), | ||
seconds: diff.toFixed(2), | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we made this into an actual class, we could get the benefit of better type inference in the IDE and reader clarity. A named class is always clearer than Object
.
Structural redesign, modularization, and separation of concerns. Still works just the same (except the resting screen is a less harsh blue and I added a new timer mode).