# Is code “working as intended” if significant portions are hardcoded?

I was looking at the question How to output to an XML file in Python?, and to me it clearly seems to be off-topic, but I wanted some second opinions.

In a nutshell, the program takes data from an input file (the filename is hardcoded) and then runs a solver that generates an output in an array format. The asker wants to know how to convert that output array into an XML file, programmatically. Normally this would be a clear case of "code not working as intended", since the asker wants an XML output and they are not getting it. However, at the very end of the program, the asker has already hardcoded some lines to produce an XML file that just happens to match the array output from the solver. For this reason (or because they misunderstood the question), they claimed that the code was "working as intended" when asked.

My judgement is that the code is not working as intended, and therefore off-topic, because the output would become wrong if the asker fed the program a different input file. Do you agree? More generally, if we're given code with a lot of hardcoding in it, what criteria should we use to judge whether or not it's "working as intended"?

• Thanks for this post, I've put a binding 4th close vote... This has all the signs of a post that will be flagged in a few months by the OP with something like "Hey I need to delete this question and it's not letting me" or similar. – Mathieu Guindon Nov 12 '20 at 22:58

That question sure looks like a "gimme teh codez" disguised as a request for a code review. I guess it's fair to ask "what qualifies as 'working' and who are you to judge my code if it seems to fit the rules?"

A comment on another answer pointed out that fizzbuzz is contrived but could still be considered reviewable. Its also hard to try and game the system with fizzbuzz. Maybe the only real answer then is the same as Justice Potter's definition of obscenity: "I know it when I see it."

That's not working by any reasonable definition - changes to the input file would be expected to make different output.

One could claim that's actually what's intended, but then it's not a very useful program, and really not worthy of review.

• Fizzbuzz programs are not particularly useful either, yet (some of) those are deemed reviewable. It might be dangerous to use lack of usefulness as a reason to disqualify a piece of code. I do see a good case to close the question for lack details or clarity though. – Mast Nov 12 '20 at 20:11
• @Mast, I do agree with your comment, but perhaps take a view that Fizzbuzz has at least educational utility when done well (or at least attempted in good faith). – Toby Speight Nov 13 '20 at 21:04
• I know. But that's something to clarify in an answer, not in a comment :-) I'm just pointing out the flaw in the current statement, nothing more. – Mast Nov 13 '20 at 22:27

Let's assume that the question is On-Topic but not one that you believe is a good fit for The Community, you should Downvote the question.

The Community guides the types of questions that it wants to answer in the end. What I mean by this is that we have guidelines that help users to structure good questions, but there are users that will try to circumvent the guidelines on purpose to get what they want from our Community. So, while they matched all the criteria for a good question, maybe it isn't of good quality, meaning that we don't close, but downvote instead.

Voting is what makes the Stack Exchange sites work as a community.

Unfortunately, the question was deleted so I can not judge it for my self, and I don't want to judge the specific question based on a second hand summery, so instead I put forward my opinion on the topic as stated in the title:

Yes, "hardcoded" code can be said "working as intended" if it produces the expected output.

Unless you have the specification requirements for the code that state all the possibilities it should accommodate and it does not accommodate some of them due to hardcoded parts, then the only measure of correctness you have is the validity of the output.

If this validity was achieved by improper coding, it seems to me this is exactly what this site is for - to offer a better way.

In my opinion, allowing the code to handle different format of input falls under "robustness" not "correctness".

Of course even I would draw the line at a case where the presented code would output hardcoded expected output for specific input, and then the OP would ask for the actual algorithm to produce said output from the input, but that is an extreme case and I would be surprised if question like that actually popped up...

Edit: I would also like to put this question up for consideration:
LeetCode 3Sum Java Solution

It doesn't have hardcoding issues, but the OP states clearly that the code times out instead of passing the tests it is explicitly designed to pass. Yet it has 2 upvotes and none has commented that it is incorrect.

This honestly brings me some confusion regarding this community and its rules...

• We long ago determined that time-limit-exceeded code is allowable, if it's functionally correct but has performance problems. What's not allowed is code that isn't functioning. – Toby Speight Nov 18 '20 at 22:30
• @TobySpeight problem is, without knowing the test input that fails, can we be 100% sure it just exceeds some set time limit and not stuck in an infinite loop? – Lev M. Nov 19 '20 at 0:58
• That's a good point, and it's where we have to trust the poster, who says they honestly believe the code to be correct. In this particular case, we're talking about a known failure to satisfy functional requirements. – Toby Speight Nov 19 '20 at 9:31
• Asking to fix known bugs is specifically off-topic. – Toby Speight Nov 19 '20 at 9:32
• @LevM. On the one hand "many eyes make all bugs shallow" so yes we can be 100% sure if we read the code. On the other hand it is easy to abuse sites that have lenient policies. – Peilonrayz Dec 20 '20 at 12:39