17

So, I am pretty new to Stack Overflow and programming in general.

Someone asked a question how to do something (getting a binary representation of a float in a string) in C++. I said I supposed it might be easier to inline some assembly into your C++ code for that than to do it in C++. I also provided a code example of how to do it. My answer got a couple of downvotes. You can see it here:

Showing binary representation of floating point types in C++

So, when are such answers acceptable and when not? If somebody asks how to do something in CSS, and you say that it's perhaps better to inline some JavaScript, that answer is not so unacceptable, right?

10
  • 16
    There's no real general rule. If readers feel that it's a useful solution to the problem, they'll upvote, if they don't, they'll downvote. Calling out to another language isn't universally useful or not useful. Apparently readers felt that it wasn't a useful solution in your case. (I'm certainly inclined to agree with their assessment as a non-expert in the field.)
    – Servy
    Commented Jan 2, 2018 at 16:21
  • 41
    This: This code makes a bunch of assumptions about the computer architecture and I am not sure on how many computers it would work is generally not an indication of a good answer. That might've cost you some downvotes. Answers should be broadly applicable. Furthermore, the OP asks for library functions, and you're not providing any. That's also why the question is closed. But this is all speculation, only the voters can explain their reasons
    – Erik A
    Commented Jan 2, 2018 at 16:24
  • 9
    Inline assembly is a sledgehammer. It has it uses, but it just isn't the appropriate tool when you need to drive a screw. Commented Jan 2, 2018 at 16:36
  • 17
    You are not even saying under which constraints your solution works. You just say that there are some assumptions but not which. Also describing what the code does and how it solves the problem is always a good thing.
    – BDL
    Commented Jan 2, 2018 at 16:53
  • If they answer the question. I see only a little chance for that in most cases, but I wouldn't close it out everytime.
    – peterh
    Commented Jan 2, 2018 at 16:57
  • 1
    As for the downvotes received at your answer (I didn't DV), we decided to close the question as off-topic, because it asked for a 3rd party resource primarily. Though one of the reasonings that were discussed in the comments, it's closed because asking for something that's beyond the c++ standard, and is a compiler implementation detail. We cannot answer that question reasonably or concisely without taking a specific compiler implementation into account. Well, the accepted answer shows a pretty portable way, but the question itself is still off-topic in its current form.
    – user0042
    Commented Jan 2, 2018 at 18:27
  • 1
    Providing an answer that uses different technology from what the OP asked for is always risky. Often it's right to tell them about a different technology that might be appropriate to their needs (or indeed, to tell them that they are tackling the problem using the wrong tools), but very often there will be technical or social constraints which prevent them using your preferred choice of technology, meaning that it's not an answer they can implement. Commented Jan 3, 2018 at 11:06
  • 2
    If asm() command would be something external or require more effort (installing some library, learning another language), then it may even treated as an insult. The point here: why would OP has to learn another language? But since asm() is there, it's ok, you can use it to provide an answer. The question is only how good and useful such answer is. Asm inline is an expert technique and unless you are the one, be ready to get criticized for offering such a solution.
    – Sinatr
    Commented Jan 3, 2018 at 11:13
  • Note that I used a similar logic to answer a Swift question using Python: stackoverflow.com/a/46034647/1033581
    – Cœur
    Commented Jan 4, 2018 at 7:22
  • 1
    @Sinatr No. The answer is not in C++. Try compiling the code fragment for ARM or using a compiler that has a different assembly language syntax. The answer given does not answer the question in any reasonable way.
    – JeremyP
    Commented Jan 4, 2018 at 10:22

4 Answers 4

39

Such an answer is acceptable as a Stack Overflow answer (from a general rules perspective).

Your answer presumably got downvoted because people didn't think it's a good answer (presumably because bringing assembler into C++ is something those users consider to be something one should only do when there isn't a reasonable alternative).


We have some fairly clear rules about what's acceptable as an answer, but what's a "good" or "bad" answer is highly subjective and Stack Overflow allows up-/down-voting for just about any reason (other than targeting a specific user).

When we step outside what a language can natively support and start talking about using specific uncommon libraries to allow any given language to be supported (outside of ones used or mentioned in the question), I'd say we might be taking a leap towards the unacceptable side of things (but drawing a line there is something that's probably better left for if/when this ever becomes a problem).

6

I'd say that it's a matter of which options are highly likely to be on the table as far as most users are concerned.

Most C++ environments provide some form of inline assembly, so an answer that uses this is not entirely unreasonable. Similarly, users of CSS will often have JavaScript available to them as an option, so it's not unreasonable for an answer to suggest JavaScript as an alternative to straight CSS.

By contrast, just because you're using C++ doesn't mean that you're using Lua. You could be using Lua in your C++ application, but it's certainly not a fundamental part of the environment you're working in. So I would say that such an answer is fairly out-of-bounds.

So long as the answer isn't trying to conflate the two options, so long as it makes clear that this solution is not pretending that CSS is JavaScript or that assembly is C++, it should be fine.

3
  • Excellent CSS-JavaScript comparison.
    – Sinjai
    Commented Jan 4, 2018 at 0:43
  • @NicolBolas actually in most cases it is unreasonable to suggest JavaScript answers to CSS questions, because that's not what the OP is asking for, and especially because there is certainly already a JavaScript question asking the same thing where they should be posting their answer. Only if the OP is asking for something that can't really be done in CSS, or if they tag the question with JavaScript, should one answer with a JS solution.
    – TylerH
    Commented Jan 5, 2018 at 14:28
  • IMO, most of the time OP is looking for a working solution that fits within their environment. They may not specifically care that it includes JS within a CSS related answer, provided their environment supports it. That being said, there's something to be said for keeping code clean...inlining code usually isn't what I'd classify as clean. Commented Jan 5, 2018 at 14:48
4

Always. They are always acceptable so long as they are answering the question.

That said, if it isn't a good or "useful" answer, it will more than likely be downvoted very strongly. Good answers to the question probably end up getting upvoted. Good answers end up on top, bad on bottom, system seems to work to me.

1

I am wont to disagree with the "it's acceptable" responses here. If someone asks how to do "X" in language "Y", and you provide an answer in language "Z", then that's specifically not answering the question. I'd downvote it unless it provided something beyond the equivalent of "language Y is dumb, see how easy it is in language Z".

In this specific case, providing an answer in assembler is likely to be non-useful, as assembler is, by nature, non-portable.

If the question is "How do I do X in language Y with conditions CZ" such that a embedded other language is a valid option, then "yes", it's acceptable.

6
  • 6
    You're positing an answer that's useless in language Y, but the OP posits an answer that embeds language Z in language Y. It's still written in a manner specific to language Y, then, and for use in language-Y programs. I don't see this answer supporting the claim that such items are non-useful. Commented Jan 3, 2018 at 20:41
  • 2
    (Personally, I'm very active in the bash tag. If answers that used awk, Python, jq or other easily-embeddable-but-separate languages weren't allowed in the bash tag, we'd have far fewer questions for which correct, high-performance answers existed at all). Commented Jan 3, 2018 at 20:43
  • 3
    @CharlesDuffy C++ does not have an embedded assembly language. The question asks for a C++ solution. The answer given by the OP is not in C++.
    – JeremyP
    Commented Jan 4, 2018 at 10:18
  • 2
    Nor is awk or jq part of bash, but you can embed them just the same. Likewise, while C++ may not "have" an embeddable assembly language, it certainly does have constructs one can use to embed assembly. Commented Jan 4, 2018 at 11:09
  • 2
    Also, XY Problems are extremely common, and so often solving the problem the OP is having does not mean answering exactly the question they've written. Commented Jan 5, 2018 at 0:19
  • can XY problems be flagged as such?
    – adam rowe
    Commented Jan 5, 2018 at 16:22

You must log in to answer this question.

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