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

A well-organized system #3

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Conversation

engelthehyp
Copy link

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).

Copy link
Author

@engelthehyp engelthehyp left a 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.

Comment on lines +108 to +115
const currentTimeInfo = new TimeInformation(
working,
workTime,
restTime,
timeLeft
);

theText = standardText(timeFormatter.textFrom(currentTimeInfo));
Copy link
Author

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.

Comment on lines +83 to +97
if (resetTimer) {
buttons.forEach((b) => b.remove());
endTime = getTargetTime(working ? workTime : restTime);
buttons = [
makeButton(
"Reset",
(currentButtonsAmount = modes.length),
resetProgram
),
];

soundEffect.play();

resetTimer = false;
}
Copy link
Author

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.

Copy link
Author

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.

Comment on lines +132 to +135
windowWidth / 2 - buttonWidth / 2,
windowHeight / 2 +
idx * buttonHeight -
(modes.length * buttonHeight) / 2
Copy link
Author

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])));
Copy link
Author

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.

Comment on lines +152 to +157
button.position(
windowWidth / 2 - buttonWidth / 2,
windowHeight / 2 +
currentButtonsAmount * buttonHeight -
(totalButtons * buttonHeight) / 2
);
Copy link
Author

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>
Copy link
Author

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());
Copy link
Author

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.

Comment on lines +10 to +20
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.
Copy link
Author

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.

Copy link
Author

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?

Comment on lines +30 to +34
return {
hours: Math.floor(diff / 60 / 60),
minutes: Math.floor(diff / 60),
seconds: diff.toFixed(2),
};
Copy link
Author

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.

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.

1 participant