-2
\$\begingroup\$

Below is my draft:

I have a working code to have 2 search bars with dropdown suggestions, selectable by both mouse and arrow keys in TypeScript and Fresh/Preact. In there I have to explicitly declare individual hooks and containers for different lists.

Pieces of working code

In an attempt to modularize it, I attempt to set hooks for the hooks:

Pieces of hooks of hooks

It doesn't work. Why is it the case?

My next step for this is to have the suggested lists disappear when they are unfocused by integrating it with Detect click outside multiple components

The draft is about what I've learned, tried, intended, struggled and questioned about, and it's more about helping myself think better and clearer. I don't intended to write it to any specific site. From my feeling SO will accept it – but that's a trivial fact – and CR won't like the why the try doesn't work – but I don't have any exposure to the site to confirm that. That's why I ask this question.

Bending the question to follow the site better is fine for me. It's just that I think if adding the attempt is fine, then when it doesn't work it's natural for the readers to be curious on why. So having a side, off-topic question on why it doesn't work in a main, on-topic one is acceptable, as I think answers including it enhance the learning better.

Should I just ask how to improve the working code without mentioning my try? It can be asked in SO. Or is it fine to include to the post?

\$\endgroup\$
1

2 Answers 2

4
\$\begingroup\$

Getting the original reviewed is on-topic. Asking why your attempt to refactor it is asking us to fix it and thus not on-topic. But you can absolutely include it as a 'this is what I've been thinking of', if you're not completely set of having that approach included in the end result. The suggestion can be freely ignored by reviewers, that's up to them

Practical example: I wrote this many years ago.

Relevant snippet:

I'm looking for a more idiomatic way to solve this. I think it can be done in a one-liner, something like:

return arr.sort((function(a, b) { return a - b; })).reduce(function(a, b) { /* */ });

But I'm pretty sure I'm heading the wrong way with the above.

What you suggest in your draft is not the same (and would likely not be well received), so you'll have to decide for yourself whether this is an acceptable deviation or not.

Keep in mind that for the question to be on-topic at all, you must act as maintainer of the code. Asking us to review a snippet you copied straight from someone on SO would not do. If you have adapted the code to your specific use-case and that works for you, we can review that. SO deals with example code, we deal with the actual code. Please see our guide for more differences.

So having a side, off-topic question on why it doesn't work in a main, on-topic one is acceptable, as I think answers including it enhance the learning better.

No. It is not. You can suggest it as a way of thinking to help the reviewers see what you have in mind. As additional context. You can not ask why something doesn't work. If you want to know about the why of something that does not work, there are other platforms available. Possibly Stack Overflow. On Code Review, you can ask about code that does work. And if you show something you've tried that looked promising but didn't work as a bit of additional context, there's no harm in that. However, that does not mean fixing your proposed implementation is in any way within scope.

\$\endgroup\$
0
1
\$\begingroup\$

We can't review the code that doesn't work. But it may be helpful to include something like that as an illustration of why you're not using that approach. That may avoid reviewers suggesting the exact thing you tried.

I recommend you use blockquote markdown to make it clear that this code isn't to be reviewed:

> ```
> /* the approach that sounds reasonable
>  * but doesn't produce the same result.
>  */
> ```

or

>     /* the approach that sounds reasonable
>      * but doesn't produce the same result.
>      */

However,

having a side, off-topic question on why it doesn't work

No, that's not permissible. If you don't know why some code doesn't work, Code Review is not the place to ask. Stack Overflow may be able to help, and if so, it's worth distilling a minimal reproducible example for that site and then incorporating what you learn into the code before asking for review here (not the other way around, because that is more likely to waste reviewers' time on code you eventually discard).

\$\endgroup\$

You must log in to answer this question.

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