0rthodontist said:
Did you think I was saying anything other?
Well... yes. Your complaint seems to change with every post.
What if you decided you wanted more descriptive names for your panels than "panel1," maybe something self documenting like "headerpanel"?
If you were working on a project that allowed you to give names like that, then obviously one would write
HidePanel to be able to work with those names. Duh. It almost sounds like you're arguing just for the sake of arguing.
This is an excellent programming practice which is completely incompatible with code referring to its own variables names as strings.
Um... "headerpanel" is a string, you know.
What if you wanted to do two different things?
Are you
seriously objecting to
HidePanel because the only thing it does is hide panels?
This is (to a first approximation) the publish/subscribe design pattern.
A good design pattern applied to the wrong problem is a bad programming practice.
The publish/subscribe pattern is is objectively better for the reusability, maintenance, and readability reasons I mentioned, and has no disadvantages.
Do you seriously think that:
(1) Designing, debugging, and maintaining your pub/sub architecture.
(2) Creating and documenting a panel hiding topic.
(3) Designing, debugging, and maintaining a panel hider object.
(4) Creating the panel hider, and subscribing to the topic.
(5) Every time you create a panel:
(5a) Connecting to the panel hiding topic.
(5b) Registering this panel.
(6) Every time you destroy a panel:
(6a) Connecting to the panel hiding topic.
(6b) Unregistering this panel.
(7) Every time you want to hide a panel:
(7a) Connecting to the panel hiding topic.
(7b) Publishing the name of the panel to the topic.
(I'm not even sure there
is a way to do 6 reliably)
is easier to maintain and read, and has no disadvantages over:
(1) Designing, debugging, and maintaining a function that hides panels.
(2) Every time you want to hide a panel:
(2a) Pass the name of your panel to the function?
Certainly there will be situations where doing this via pub/sub would be desirable. But it baffles me that you think pub/sub would commonly be the right way to accomplish this task, let alone
always.
(and besides, if you were using any sort of pub/sub architecture, I would expect
HidePanel to be the function called by the object that consumes the hiding messages to actually hide the panel)
Yes, this might be a problem if Javascript didn't use garbage collection.
You can't garbage collect it unless you remember to unsubscribe, so that it's entry has been removed from the associative array.