That's true, but I try to make my code as optimal as possible even if it doesn't need to be--so if that if statement doesn't have to be there (especially if it would make the code more readable) then I'd rather not have it there. It makes more sense to me to say "hide this graphic now" than "if this graphic is showing, toggle its visibility." Especially since with most high level programming languages these days you have to check the state within the toggle function (if it's "a", change to "b", otherwise change to "a") so instead of one state check now you've got two.
The best place I can think of where you wouldn't bother checking the state before deciding whether to toggle is if it's somehow related to a control that a user can manipulate to turn something on or off, which implies that they've already checked the state and determined that it should be the opposite of what it currently is (you can only protect users from themselves to a certain point, after which they have to take responsibility for their actions ;) That's really the only place that I can think of off the top of my head where I'd use a toggle function. That, or maybe in a loop that has something that should switch states each pass through.
But yeah, the short back-story is mostly covered in the previous entry about ripping out old code and trying to make it work. There's one point at which it loads several images from a webserver. When it starts loading, it displays (using a toggle function) a banner notifying the user that the images are loading. When a certain image from the set finishes loading, the banner is again toggled (regardless of whether that was actually the last image being loaded, but that's a topic for a different coding lesson ;) The problem is that the user can do the same operation to load more images while the old images are still loading (i.e. while the banner is still showing). The beginning toggle is called, but this time since the banner was already displayed it gets hidden. The images load, including the magic one that toggles the banner, which causes it to display again. After that it stays on even though the images finish loading shortly after that, and the users get confused as to why it's taking so long because the banner hasn't disappeared yet.
While I haven't fixed everything yet, I did change it so now when the magic image loads it hides the banner instead of toggling it. Not as good as I'd like it to be, but at least it's getting closer ;)
no subject
The best place I can think of where you wouldn't bother checking the state before deciding whether to toggle is if it's somehow related to a control that a user can manipulate to turn something on or off, which implies that they've already checked the state and determined that it should be the opposite of what it currently is (you can only protect users from themselves to a certain point, after which they have to take responsibility for their actions ;) That's really the only place that I can think of off the top of my head where I'd use a toggle function. That, or maybe in a loop that has something that should switch states each pass through.
But yeah, the short back-story is mostly covered in the previous entry about ripping out old code and trying to make it work. There's one point at which it loads several images from a webserver. When it starts loading, it displays (using a toggle function) a banner notifying the user that the images are loading. When a certain image from the set finishes loading, the banner is again toggled (regardless of whether that was actually the last image being loaded, but that's a topic for a different coding lesson ;) The problem is that the user can do the same operation to load more images while the old images are still loading (i.e. while the banner is still showing). The beginning toggle is called, but this time since the banner was already displayed it gets hidden. The images load, including the magic one that toggles the banner, which causes it to display again. After that it stays on even though the images finish loading shortly after that, and the users get confused as to why it's taking so long because the banner hasn't disappeared yet.
While I haven't fixed everything yet, I did change it so now when the magic image loads it hides the banner instead of toggling it. Not as good as I'd like it to be, but at least it's getting closer ;)