-6
\$\begingroup\$

See From the below javascript code snippet which one is considered to be most appropriate? [on hold]

Scenario 1:

try {
  elements.personWrapper.getElement('span[name=personClass]').set('innerHTML',''); 
} catch(TypeError) {
  elements.personWrapper.getElement('input[name=personClass]').set('innerHTML', '');
}

Scenario 2:

var span_element = elements.personWrapper.getElement('span[name=personClass]');
var input_element = elements.personWrapper.getElement('input[name=personClass]');
if (span_element) {
  span_element.set('innerHTML', '');
} else if (input_element) {
  input_element.set('value', '');
}

Further, OP specifically stated to ignore the fact that the code is user defined

Above code snippet achieve the same goal (Please do not consider the readability or naming conventions, its been edited). So, I would like to know which one is better. Anyone please provide your valuable suggestions.

Please note that either of the one get's set while the page get loaded. The span and input elements are created dynamically. So, the goal is to set the innerHTML if it's a span or set the value if it's an input type element.

I want to know which code snippet is more efficient in terms of code execution and less error prone, Thanks!

The code is obviously relating to an HTML document and can be trivially substituted for document.querySelector(), .innerHTML and .value without in any way affecting the original intent of the OP or the resulting code execution.

An edit was suggested which substituted stadard DOM methods for the user-defined methods and removed reference to ignore the user-defined methods, which was rejected for the reason that the edit changed the original intent of the question; though am unable to determine how the edited code affects the original question whatsoever.

The question should not have been closed in the first place. The original question is clearly on-topic for Code Review SE, even with the user-defined code at the original question. Kindly re-open the question.

\$\endgroup\$
1
  • \$\begingroup\$ @Mast Am stating the votes to close are erroneous on their face. The question is clear and on-topic for this site. "down" vote the original question, if you must. Though closing the question under an erroneous pretext prevents users who do understand the question from helping OP. As yet, am the only user that am aware of that tried to edit the original question, to address whatever concerns the users who voted to close the question had. It is still baffling why the question was closed in the first place. The context is an HTML document two methods of setting .innerHTML or .value. \$\endgroup\$ Commented Dec 15, 2018 at 20:05

4 Answers 4

8
\$\begingroup\$

Let me quote the question piecewise and explain why it's off topic with the relevant sections of the help center


(Please do not consider the readability or naming conventions, its been edited)

This implies that the code presented is not the code that the user is actually working on. They ask us to ignore readability and naming conventions. This is already a big warning sign, because it goes against the following rule in the help center:

Details matter! In order to give good advice, we need to see real, concrete code, and understand the context in which the code is used.

This code is not real, concrete code, the question does little to nothing to establish any context in which the code is used.


Above code snippet achieve the same goal

snippet implies the code is stripped of any context that would also be relevant for a review. For all we know, the user has 250 of these "snippets" copy-pasted and adjusted manually. This again goes against the rule requiring the real, concrete code.


So, I would like to know which one is better.

This is asking us to compare two snippets that "achieve the same goal", and state which of them is "better". It doesn't define any useful metric by which we could assess the "goodness" of a given snippet. While this is not strictly off-topic (the help-center explicitly allows disregarding the focus that OP asks for in a question), it doesn't bode well either.


I want to know which code snippet is more efficient in terms of code execution and less error prone.

Again: snippet. The code has been ripped from it's context. In the question itself OP states that the elements the code iterates over have been dynamically generated. Why OP needs to now amend them is not made clear in any way. No context is given on how the elements are generated or how the snippet given is invoked.

In short: this question gives too little context to provide a comprehensive code review.


Note that I'm not saying the question isn't answerable. It may well be answerable, but it's not a question that this community wants to answer. That's the whole idea of defining scopes for sites on Stack Exchange.

In conclusion: The question you present here is clearly off-topic. It violates the rule that code without context and snippets are disallowed.

\$\endgroup\$
17
  • \$\begingroup\$ The context is clearly an HTML document and DOM methods. The code used at the original question is clearly the real code, even if abstracted to functions not bearing the name of standard DOM methods. The question is precisely on-topic for Code Review SE. Proof: The current answer to the question provides an answer to the question. \$\endgroup\$ Commented Dec 15, 2018 at 19:47
  • 2
    \$\begingroup\$ @guest271314 at the danger of repeating myself. it's not about the function's names. That's not why the question was closed. It's about "an HTML document" not being sufficient context for a proper review. That's the issue. Also your "proof" falls prey to the fallacy "post hoc ergo propter hoc". Your argumentation is based on the fact that you've answered. That doesn't make the question on-topic though. It being answerable has no bearing on it's topicality \$\endgroup\$
    – Vogel612 Mod
    Commented Dec 15, 2018 at 19:49
  • \$\begingroup\$ An HTML document is sufficient context. OP is only asking whether to use try..catch or check if element exists in DOM first before setting either .innerHTML or .value. The first approach catches errors if .innerHTML or .value is attempted to be set on null. The second approach checks if the element exists first. Rather simple question. Not sure how any confusion as to context could possibly be the case. \$\endgroup\$ Commented Dec 15, 2018 at 19:50
  • 2
    \$\begingroup\$ "An HTML document is sufficient context" - ---5--- 7 people think otherwise. If you wish to get the question reopened, please flag it for reopening \$\endgroup\$
    – Vogel612 Mod
    Commented Dec 15, 2018 at 19:52
  • \$\begingroup\$ @guest271314 And because they thought otherwise, they voted to close the question. That's the close reason, as indicated by the 3 answers provided so far, one of which by someone who actually voted to close the question (me). It got closed because the question is in violation of the rules provided in the help center, we even cite which ones specifically. \$\endgroup\$
    – Mast Mod
    Commented Dec 15, 2018 at 19:54
  • \$\begingroup\$ Yes, they think otherwise. It would not be the first instance of a faction or majority in or of any "community" being factually wrong or incorrect in their individual assessment of facts; or at the very least deciding not to consider that others within "community" other than themselves are able to both understand and answer the question; and thus, the question must be closed for their own individual decision. Again, proof of the incorrectness of such opinion is the answer posted by this user. \$\endgroup\$ Commented Dec 15, 2018 at 19:54
  • \$\begingroup\$ @Vogel612 How to flag for reopening? \$\endgroup\$ Commented Dec 15, 2018 at 20:03
  • \$\begingroup\$ @guest271314 apparently I misremembered something. Only users that have the privilege to "Vote to close" questions (3k reputation and beyond) can nominate a question for reopening. \$\endgroup\$
    – Vogel612 Mod
    Commented Dec 15, 2018 at 20:05
  • \$\begingroup\$ @Vogel612 So be it. The closure of the question, from perspective here, is erroneous. Tried to edit the question for the presumed reason for users voting to close the question. As it turns out, users voted to close the question for reasons that do not concur with. The question is clear and has context from perspective here. All can do is what have: make it clear that the votes to close the question are erroneous. Suggest an edit if there are issues with the question. Though cannot see any issues with the question, other than formatting. \$\endgroup\$ Commented Dec 15, 2018 at 20:11
  • 3
    \$\begingroup\$ @guest271314 You mention trying to edit the question to make the code on-topic but just to be clear that kind of edit requires the OP. After all how can you know what the original unmodified code was? An edit like that, by the OP, in which they added the necessary context would go a long way to make the question on-topic. \$\endgroup\$
    – Summer
    Commented Dec 15, 2018 at 20:51
  • \$\begingroup\$ @bruglesco "After all how can you know what the original unmodified code was?" It is obvious that the abstraction of the user-defined code is reduced to DOM methods, whether .querySelector() or other DOM method that gets an DOM element is irrelevant to the question. The user-defined abstraction is not what the question is about. The question is about whether to 1) attempt to set .innerHTML or .value of an element and catching and handling errors if the element does not exist in the DOM; or 2) checking if the element exists in the DOM before setting .innerHTML or .value \$\endgroup\$ Commented Dec 15, 2018 at 21:39
  • 1
    \$\begingroup\$ @guest271314 A custom mod flag with a comment to ask for it to be reopened could get it looked at by Mods but won't push it into the review queue apparently. Probably not the solution you were looking for bur may be good to know in the future. \$\endgroup\$
    – Summer
    Commented Dec 15, 2018 at 23:23
  • \$\begingroup\$ @bruglesco FWIW Evidently the OP asked the question at SO. At least one user there suggested posting the question here; albeit with at least one user raising the same concerns regarding context that were raised here stackoverflow.com/questions/53794187/… \$\endgroup\$ Commented Dec 16, 2018 at 4:52
  • 3
    \$\begingroup\$ @guest271314 yes I saw that. You should be aware that people at SO so frequently redirect users incorrectly to post here that we have a meta question about it and a bot that monitors SO comments and posts in chat to warn us. The user who raised the concerns about context was correct. \$\endgroup\$
    – Summer
    Commented Dec 16, 2018 at 4:55
  • \$\begingroup\$ @bruglesco Did not check if the question had been posted at SO before posting the answer or this question. The user who made that observation is well-suited to make such a judgment. Am also well-suited to sustain disagreement with that conclusion, without rancor, based on the assessment already posted at the answer and at this question. \$\endgroup\$ Commented Dec 16, 2018 at 5:00
5
\$\begingroup\$

I see that you are saying that "the context is apparent by the code itself." and "There is no difference from the original code and the edit to the code submitted". Let me reply to these things and possibly some others of your concerns.

First, let's talk about the answerability of this question. Is the question answerable? Well, sure, you posted an answer to it. And I agree with this answer, considering the question it's a very good answer. But your answer basically explained what the difference was between the two scenarios. The first one can fail if the input element does not exist, and the first one doesn't even attempt to change the value, only the innerHTML. So you basically pointed out what the differences was in what the code does.

For me, the original question poses more questions to me than answers. On Stack Overflow I saw the OP wrote:

please note that there is no flag values (IF block conditional checking parameter) that can be used to determine whether the dynamically created element is of span or input type. That is why the first code snippet used a try-catch block and the 2nd code snippet introduced both element, assuming that any of these will hold an object other than null at run-time

And my question to this is: Why isn't there a flag for this? How are these dynamically elements created? How are they stored? Can't you use different lists for span and input elements? Why are they stored? What are the elements used for? How can I be sure that these objects always exists? My goal as a reviewer is not to just answer the question, my goal is to give the best possible advice. I can't do that because I have so many questions about how this code is used. This is what other people mean by "lack of context". So the best possible answer we can give at the moment is what you wrote in your answer. But we want to give better advice than that. We want to help out more than that. But we can't, without the answer to the questions I posed here. And you can't answer these questions just by reading the code that is in the question.


About your edit, I understand that you mean well, and as others have said the things you edited were not at all a problem with the question. The things you edited did not help in answering the questions I posed above. No one except the OP can answer the questions I posed above.

Editing the code in a question should only be done by the OP on Code Review Stack Exchange. There are a few reasons for this, for example:

  • The code in the question is the code that is being reviewed, it's the OP that is asking for a review, not you. We want to review the OP's code, if you want your code reviewed then please post a question yourself.
  • Only the OP of a question knows the natural habitat of the code and can answer questions about it. What if a question pops up about "Why did you do this?", if you had edited the code then the OP's only response would be "Umm, well, I didn't. Someone else did. I don't know".

So in conclusion, this question gives me more questions than what I can give answers, and therefore I also consider that it lacks context in order to be answered properly. As said, I would like to be able to give an even better answer than what you have already given.

\$\endgroup\$
4
  • \$\begingroup\$ Your answer presents rational points. However, from perspective here, none compelling enough to warrant closure of the question as it presently stands; where OP has put forth a modicum of effort to ask a specific question, irrespective of whether or not the entire program has issues which would lead to not using either of the design patterns at the question. The question asks about two different approaches to handle getting and setting a DOM element and value of an element, or not; your curiosity as to the entirety of the program cannot be satisfied when the question is closed. \$\endgroup\$ Commented Dec 16, 2018 at 20:46
  • 1
    \$\begingroup\$ @guest271314 First of all, Code Review Stack Exchange is generally not about specific questions. We also want to look at the bigger picture here. Secondly, the reason for putting the question on-hold (which is a more preferable term than "closing") is to make it possible for the question to be edited and improved, and questions like mine answered, so that it makes better answers possible. \$\endgroup\$
    – Simon Forsberg Mod
    Commented Dec 16, 2018 at 22:09
  • \$\begingroup\$ The design patterns at the question are general enough. Do the questions posed at your answer essentially state that OP of the question should not use either approach; that any manipulation of HTML strings or DOM elements should take place at the creation of the dynamic element? If that is the case, an answer to the question can present that option and address the two patterns OP describes. If OP has control over the creation of the HTML, then they can use the option which does not apply either approach described at the question; else, use one of the two options listed, and state why at answer \$\endgroup\$ Commented Dec 17, 2018 at 1:47
  • 2
    \$\begingroup\$ @guest271314 I don't know if OP is in control of the creation of the elements or not. We have experienced too many times on this site that we try to answer vague questions with too little context (like this one) and then the OP comments that "This answer is not really applicable to me because XYZ". To prevent such scenarios, we put the questions on-hold and ask the OP to improve the question rather than waste everyone's time by posting irrelevant answers. \$\endgroup\$
    – Simon Forsberg Mod
    Commented Dec 17, 2018 at 13:24
4
\$\begingroup\$

A statement assuring us that the missing context is irrelevent is not the same thing as having the full context and doesn't mean we don't need it.

Furthermore the statement implies that the code has been altered (presumably to be more generic, but I don't know because I don't have the full context.) This makes it example code which is also off-topic.

Lastly, the question explicitly asks for a choice between two options and asks us to ignore other aspects of a Code Review. This is a best practices question and belongs on a different Exchange. Not an "any and all aspects" review request.

\$\endgroup\$
1
  • \$\begingroup\$ As stated at the comment at the answer the context is apparent by the code itself. 1) Use try..catch and handle errors if the element does not exist in the DOM, or 2) check if the element exists in the DOM first. The question is precisely on-topic for Code Review SE. Why users decided to vote to close the question is a mystery not resolved by either of the answers to this question thus far. \$\endgroup\$ Commented Dec 15, 2018 at 19:29
3
\$\begingroup\$

"Further, OP specifically stated to ignore the fact that the code is user defined"

OP asking us to ignore anything is something we simply don't allow.

From the help center:

Is it actual code from a project rather than pseudo-code or hypothetical code?

The code in question, is not.

Do I want feedback about any or all facets of the code?

You can ask for focus points, but you can't exclude things from review. The moment you upload your (real) code, it's fair game for the reviewers.

\$\endgroup\$
8
  • \$\begingroup\$ Edited the original question to use standard DOM methods. The edit was rejected citing the reason that the edit substantially changed the original intent of the code, which it could not possibly do. Even the original code is essentially the same as DOM standard code. Nothing is excluded from the original code posted by OP, no matter the user-defined abstraction. The original question is quite simple: 1) Try to set .innerHTML or .value of element first, and catch errors if the element does not exist, or 2) check if element exists first, then set .innerHTML or .value of element. \$\endgroup\$ Commented Dec 15, 2018 at 19:19
  • \$\begingroup\$ @guest271314 Are you questioning the edit or the closure in this thread? Please be specific. \$\endgroup\$
    – Mast Mod
    Commented Dec 15, 2018 at 19:21
  • \$\begingroup\$ Both. The question should not have been closed in the first place. The reason cited for closure is that the question was off-topic, presumably, due to use of user-defined methods. The user-defined methods are irrelevant. The question remains the same either way. Try to set .innerHTML or .value first, and catch errors, or check if element exists first, then set .innerHTML or .value. It is obvious the context is an HTML document and DOM methods. \$\endgroup\$ Commented Dec 15, 2018 at 19:22
  • 4
    \$\begingroup\$ @guest271314 "presumably due to use of user-defined methods". No. That's not why the question was closed. The question was closed, because the code is not the real code that OP is working on. Code Review does not allow minified, sanitized code, unless only minimally sanitized to protect business confidentiality. Code Review is not intended to answer "abstract best practice" questions. It explicitly says so in the help center \$\endgroup\$
    – Vogel612 Mod
    Commented Dec 15, 2018 at 19:25
  • 2
    \$\begingroup\$ It's not off-topic due to user-defined methods (unless those methods aren't included, in that case it can be closed for lack of context since we really need to see what's going on before having an informed take on the matter). The problem is with the code being edited so much that it's simply no longer the original code. It's also way too snippety to be reviewable. Snippets can't be reviewed, they have no context. Context is important! \$\endgroup\$
    – Mast Mod
    Commented Dec 15, 2018 at 19:26
  • \$\begingroup\$ @Vogel612 The code is the real code. Even if you do not see that, other users can, specifically this user. \$\endgroup\$ Commented Dec 15, 2018 at 19:27
  • \$\begingroup\$ @Mast There is no difference from the original code and the edit to the code submitted by this user. \$\endgroup\$ Commented Dec 15, 2018 at 19:28
  • \$\begingroup\$ @guest271314 I wrote an answer detailing things out a bit more. Hope that helps \$\endgroup\$
    – Vogel612 Mod
    Commented Dec 15, 2018 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .