In this case, it depends on whether you ever need to have the show() function available as a utility function in other areas of your code.
If you don’t need that, then the first example is equally valid, so long as it’s understood that it’s tied only to the .wrap class, and can’t be used for anything else, unless you refactored it.
With the second example, work is being kept to a minimum in the handler function. As handlers tend to get messy quickly and easily, It’s best for handlers to offload the work to other functions.
What would you say if someone asked you what happens when you click the cover? You would quite likely summarise what happens as “it shows the video”. That is the basis of what to name the function.
A problem is that the code already has a showVideo function.
To more easily combine the code, it helps to recognise that the showVideo function is doing multiple things that are not related to video.
function showVideo(cover) {
hide(cover);
slideCurtain();
initSplitWrap();
}
We could rename that function to openCurtainsAndShowVideo(), but when there’s an “and” in the function name it helps to try and think of a more general term that encompasses both things. Something like startPlayingVideo(), and rename initSplitWrap to showVideo.
// function initSplitWrap() {
function showVideo() {
...
}
function startPlayingVideo(cover) {
hide(cover);
slideCurtain();
showVideo();
}
...
function coverClickHandler(evt) {
const cover = evt.currentTarget;
startPlayingVideo(cover);
}
The click handler function is now nice and simple.
Looking further up from the click handler function we have three methods in startPlayingVideo
function startPlayingVideo(cover) {
hide(cover);
slideCurtain();
showVideo();
}
We can easily move hide and slideCurtain into their own openCurtain function.
function openCurtain(cover) {
hide(cover);
slideCurtain();
}
function startPlayingVideo(cover) {
openCurtain(cover);
showVideo();
}
We can now easily see that showVideo and slideCurtain have zero parameters. Usually that’s a sign of a bad function, because it’s not capable of working under different situations. Usually is best when a function has one or two function parameters.
With slideCurtain, the curtain variable should be moved outside of the function and passed in as a function parameter.
function slideCurtain(curtain) {
curtain.classList.add("slide");
...
}
...
function openCurtain(cover) {
hide(cover);
const curtain = document.querySelector(".outer");
slideCurtain(curtain);
}
The thewrap code in slideCurtain also should move out of the curtain code, and in to the showVideo code instead.