# Can you show me how to apply a specific strategy to my working code to improve it

As brought up here, I'm not sure when the last time we (as a site) discussed how to determine if questions are on- or off- topic.

A lot of questions currently fails the "any or all facets of the code" and yet are not closed. How does this question differ from "How can I make this code more object-oriented"?
– Simon Forsberg♦

It is no different from "I need the same thing but object-oriented"....
– rolfl♦

If the difference between an off-topic question and an on-topic question is as simple as editing the pure-english part that the OP most likely haven't given very much thought, should we then tell the OPs "Sorry, you are asking for how to change your code - if you change it to it would be on-topic."? That's not a comment I am willing to write.
– Simon Forsberg♦

I'm not picking on diamonds, it just seemed like a relevant way to start this

Related:

It seems we don't really have an agreed-upon opinion on certain types of questions, questions like:

• "How can I refactor this function?"
• "Is there a better way to perform this algorithm?"
• "What can I do so this loop handles edge-cases that I'm aware of?"

Essentially, what's the difference in the wording of the question? What's the difference between a native-English speaker's vs a non-native speaker's wording structure?

### To the best of my knowledge, does the code work as intended?

On-topic

• Q: "Here's my game and it works as I intend"
• A: "You missed an edge case of i=0

Off-topic

• Q: "This game works except for when i=0
• C: Off-topic sorry

On-topic

• Q: "Here I'm iterating through a bunch of different things, but it takes forever and sometimes freezes"
• A: You should try refactoring like this

Off-topic

• Q: How can I refactor this function so it doesn't freeze?
• C: Off-topic

In these cases one of us could easily delete or reword one sentence for it to switch from off- to on-topic. It seems it all comes down to one guideline:

To the best of my knowledge, does the code work as intended?

How strictly do we, as a site, want to adhere to this aspect of the on-topic test? Do we want to penalize users that are aware of a broken edge-case? Or should we base that on whether they are a beginner or not? How can we tell who is a beginner in whatever language it is?

Should I just pretend I don't see a broken edge case if I'm posting in a new language? Would that change whatever answers I was going to get anyway?

I'm asking a question here that isn't about what's right or wrong, I'm asking a question here about how we want to culture of this site to handle these types of things.

How on- or off-topic are these questions?

1. I know a dictionary would be faster, but I couldn't get it to work so I'm using arrays?
2. How can I use a dictionary here instead of arrays?
3. I need help refactoring this part of my code so I don't need to keep repeating it, even though that works.
4. This game works and I know it breaks without sanitized input, but I haven't gotten to that part yet.
5. This game works but I'm not sure how to handle non-sanitized input.
6. I'm J̶o̶n̶ ̶S̶k̶e̶e̶t̶ near expert in C#, but this is my first shot at Brainfuck. I can see where my structure is deficient, but I don't know how to do it the Brainfuck way. Sorry, this is a Skeet-free site

There are infinite examples of questions that could be switched between on- and off-topic by changing one or two words.

And I know not everyone one a site is going to always agree, and we shouldn't, but there should be a general trend for new users to get a feel of site culture.

• There is a subtlety that you need to add to your post to make it relevant to the comment of mine you have quoted. My concern is not so much with a user who says: "I want a specific aspect of my code reviewed", rather, the issue with the question in the recent discussion you reference is more: "I know a specific technique that I need to use to improve my code, I just don't know how to do it, can you show me?" – rolfl Apr 22 '18 at 2:59
• @rolfl if you'd like to clarify that, feel free. If you want to remove it, that's fine too. Without it I think the question is still fine. – Raystafarian Apr 22 '18 at 3:08
• I think my concern is that the title you have given the post does not reflect the scenario that's been discussed recently. I'm going to suggest the title: "Can you show me how to apply a specific strategy to my working code to improve it." I feel that title better reflects the actual on-topic issue we are struggling with..... – rolfl Apr 22 '18 at 3:15
• Sure thing, thanks – Raystafarian Apr 22 '18 at 3:22
• An example to consider as an extreme case, consider a question: "I have written this working program in language X but I know language Y will be faster, can you show me how to do it in language Y?" ... is that on-topic? – rolfl Apr 22 '18 at 3:22
• Also related: codereview.meta.stackexchange.com/q/2285/31562 – Simon Forsberg Apr 22 '18 at 9:00

Note: even though I (now/again) have a diamond next to my name, this answer is not intended to be anything other than my own opinion. This answer is not site policy/scope.

This meta question boils down to determining how handle a question of the form:

• I have a problem <insert description here> that I have solved
• I have code that works. It is <insert code here>
• I know there is a specific strategy <insert strategy here> that will improve the code/program
• How do I apply that strategy to my code?

tl;dr: in my opinion, questions like this are off-topic, and editing them does not (in spirit) help bring them back on-topic.

Examples of questions that fit this form:

• How do I make this code object oriented?
• How do I make this code more functional?
• How do I convert this loop in to a map-reduce operation?
• How can I make this function recursive?
• How do I vectorize these scalar operations?
• How do I use high-precision math?
• How do I run this SQL query with a CTE instead of temp tables?
• How do I use a linked list instead of an array?
• How do I use OpenGL instead of DirectX?

Or, put more bluntly using the analogy of an original description of Code Review: "Does this code make my ass look fat?" In the context of this meta post, "How do I put pleats in my code to make my ass look thinner?"

## On Topic?

Running through the standard on-topic questions:

• Is code included directly in my question? - YES
• Am I an author or maintainer of the code? YES
• Is it actual code from a project? YES
• To the best of my knowledge, does the code work as intended? MAYBE
• Do I want the code to be good code? YES
• Do I want feedback about any or all facets of the code? MAYBE

In my mind, the two on-topic question where things are fuzzy, are whether the code works as intended, and whether the OP wants feedback on any/all facets of the code.

### Any/All

The any/all facets is actually somewhat simpler to address. The OP has already determined that all code that is not compatible with the new strategy is junk code and will be replaced, and that they do not want that code reviewed. This is a fuzzy argument, but, for example, in the Matlab Vectorization question, all the code was scalar, so what's left if the OP wants only vectorized operations? Does that make a difference?

Note that the On-Topic page states: "If you have a working piece of code from your project and are looking for open-ended feedback...". Is this looking for open-ended feedback?

The any/all issue comes down to semantics. It is one thing to say: "I know that my code has indentation issues so please ignore those problems".... it's another thing to say: "I know the final version of my code will be restructured, and I don't know what's going to change, so just ignore the things that will need to be changed after applying the new strategy"

My opinion is that "No", the asker does not even know what code they are keeping and they do not want a review on the code they are replacing anyway.

Thus, the question is off-topic, but this is the weaker reason for off-topic compared to the next reason: works as intended.

### Works as intended

This is where I feel the issue is less fuzzy. The OP clearly gets accurate results from the existing code, it "works", but is it "working as intended"? No, the OP clearly intends to rewrite (parts of) the code to use the new strategy.

The OP clearly intends for the code to work with the new strategy, and it currently does not use that strategy, so the code clearly does not "work as intended". The question is off-topic

How is this different from performance questions? Questions where the poster asks: "My code works, but is too slow. How can I improve the performance?"

While it is clear that a performance-question has code that is not working in the intended way (as fast as the asker wants), as long as the asker is looking for "open-ended feedback" then it is OK. A special exception has been made for performance problems in competitive code too.

The difference between general performance questions and specific strategy questions is that in a performance question the poster does not yet know what strategy to apply. It's as simple as that. The asker knows that something is slow, but do not know how to take the next step in solving it.

As soon as the asker knows what the next step should be, they no longer need a code review, but they need a new iteration of their code.

## How to handle these questions?

A number of strategies have been applied already, or suggested. They are things like:

### Close the question

It does not work using the intended strategy. It does not work as intended.

This is what I did when I encountered the question. Paraphrasing what I said in my comment: "You have already reviewed your code, you already know your next step, after applying your next step, bring your code back for a review!"

### Edit the question

Edit the question to remove the request to apply the specific strategy. The question now reads: "I have this problem, with this code to solve it, please review it."

The question is now clearly on-topic. It is working code, with a request for a general review.

But.... is this a safe thing to do? How about editing a question that says "My code throws an exception, I don't know why!" and removing that from the description. Is the question now on-topic because it no longer says there's a problem? Clearly the code does not work as intended.

Also, here's a novel option: Edit the question to remove reference to the specific strategy, and then also answer the question and say: "Your code could be improved by using this specific strategy!".

• This is a very good answer and I agree with every word. My two cents: If we start accepting questions requesting to apply a specific strategy/feature/pattern we'll become a give meh teh codez community and no longer a Code Review. We wouldn't be reviewing and suggesting things anymore but rather implementing new features on request. Maye there should be a new community created... like Code Request... – t3chb0t Apr 22 '18 at 14:41
• This is actually my opinion as well – Raystafarian Apr 22 '18 at 21:24
• I disagree with this, unfortunately I am quite busy the coming days and will go on a trip so I don't know when I can write a competing answer. – Simon Forsberg Apr 23 '18 at 9:20
• @simon no rush, I'm not going to choose an answer or anything. – Raystafarian Apr 26 '18 at 5:00
• Extending the "fat ass" analogy is great - that really works for my neural net! – Toby Speight May 2 '18 at 13:03
• @Raystafarian I haven't really forgotten about it, but right now I feel like my view is quite different to others so I'm not really motivated to post my answer just to get downvoted. I have been discussing other examples with rolfl every now and then though. – Simon Forsberg Jun 6 '18 at 14:25

Off-topic

• Q: "This game works except for when i=0
• C: Off-topic sorry

I would disagree that this is always off-topic. This is on-topic if the code is only expected to work for positive numbers.

What would make it off-topic is evidence that it is supposed to handle the 0 case, but it doesn't. For example, "Can you show me how to make it work for i=0?" is clearly off-topic.

• That's a fair point, yes – Raystafarian Apr 22 '18 at 21:23
• Provided OP clearly states they doesn't care for the 0 because it never occurs... otherwise I'd see it as off-topic. – t3chb0t Apr 23 '18 at 7:49
• A simple "this code isn't intended to handle the i=0 case" is a good re-write that makes it unambiguously back on-topic again. – Toby Speight May 2 '18 at 13:06

# Code that does not work as intended is off topic

I think it's good to try to get us all on the same pages with this. Seems to me that generally we collectively are, but just as with code, it's useful to look at corner cases. With that said, I'd probably mark something like this as off-topic:

• Q: "Here I'm iterating through a bunch of different things, but it takes forever and sometimes freezes"
• A: You should try refactoring like this

If it freezes, it's not really working as intended in my view (unless part of the specification is "program must sometimes randomly freeze"!)

# "How do I ...?" is probably off topic

Questions of the form "How do I ..?" are probably generally off topic. Such a question suggests a request, essentially, to review code that has not yet been written which is not what we're all about. If someone writes "can you show me how to ... ?", the chances are really good that it's not on topic.

# Performance issues are a separate matter

If the code does what we want, but not as fast as we want, that's what the and tags are for, in my view. Generally it's fine to point out (maybe even with a small example) how code performance could be improved (and it might not even involve the specific technique the asker may have mentioned), but I usually stop short of rewriting the whole thing for several reasons.

1. I usually don't want to spend that much time
2. it's often better for people to learn by applying ideas to their own code rather than just being handed a new version
3. we don't want to become a "give meh teh codez" site as eloquently put by @t3chb0t.

I don't make that a strict rule for myself and of course don't advocate that for anyone else, but I think it's useful to try to gauge how best to provide a useful answer not only to the original asker (one person) but particularly to the many people who might read it later, perhaps as the result of a web search.

So a question like this Calculating the harmonic average had an answer that was apparently helpful to the asker, but it was also viewed over 450 times, so it's probable that showing a worked example was a useful strategy here.

Also, some questions that specifically ask about efficiency, like ToDo List Program Efficiency that simply have bigger problems to solve first.

# Expect that the user has actually compiled and run the code

Code that doesn't compile, that has obvious errors or omissions, or that simply don't match what the user has said the code is supposed to be doing tend to get a close vote from me. I don't necessarily assume that every beginner programmer has written and run a full regression suite, but I expect that at least a minimum level of effort has been expended. If the code compiles but is simply poor code, I'll make some allowances if there is also a tag, but if it fails on an obvious use, I tend to downvote rather than close.

# My philosophy

"If it's not worth the asker's time, it's not worth mine."

I try to help people understand what is wrong with their question if I vote to close or downvote, but if someone else has already made the point I would have, I don't bother with the question again unless and until the issues are addressed.

As someone who uses this site to learn I feel like I should explain that I use this site specifically because it's not stack overflow. For this reason, if it's not something that is closer to what's on SO - not what's supposed to be on SO, but instead what's actually on the site - I feel like it's on topic.

The simple fact of the matter is that code review is meant to help people improve their code. If They missed an edge case, that needs to be pointed out to them. You're really doing this site a disservice if you don't. In fact if you get too strict with the rules with code review at all, you really defeat the point of code review.

I'm by no means an expert programmer, but the whole purpose of having Code Review in the Stack Exchange community when SE already has SO seems to be to provide a place where people can learn through having their code scrutinized without breaking SO rules. When you really get down to it sometimes SO is just bad at doing things. I would never rely on SO to explain best practices to me. I would never ask SO for help with making my code better. I'd only ask SO with help getting my code to work, and if I'm really being honest about that Slack is far superior to SE when it comes to that in general.

I'd also like to point out that I correct my code when someone points out something code breaking so this i=0 edge case would just prompt me to attempt to refactor quickly.

As for explaining how to make code more functional or OOP, I feel like that's more on answers than the askers; you should just tell people when and how their code would benefit from different organization and provide implementation examples for them. If we start doing this, then there's no reason to ask for it. I also feel like this is kinda the point of Code Review as well.

Code Review should also make an effort to be nice and welcoming. Good culture always beats community guidelines or rules. That's why Slack teams are better than Stack Overflow at the moment. The way I see it, the adaptation of rules < culture threatens to make CR less useful than Slack teams by a severe margin too. I'd prefer it if that didn't happen.

• Note, the following scenarios are very different in my mind: "Are there any edge cases I am missing?" vs. "I know there's this one edge case I am missing, how do I fix it?" Are those situations both on topic then? In my opinion, the first case is on-topic, but the second one is off-topic (the code does not work as intended). – rolfl May 3 '18 at 14:48
• @rolfl One of those is an SO question. One isn't. The second case could be on this site though IMO. If they're asking about how to do something in a paradigm they suck at and it's clear in the code that what they tried to implement is garbage then there's an argument for including it, just based on my guess that SO would not handle that at all given their whole "we're not here to code for you" excuse. There's also the question of where a fix goes in your code. I've spent waaaaay too much time reordering my code just so it's easier to read. When it works it's worth it. – user106363 May 3 '18 at 16:33
• The "one of those is SO" answer is why this question is so subtle. If the user asks for help implementing something specific, then it's no longer a code-review, and it's off-topic on this site - since the user needs help writing code, not help reviewing the new code. – rolfl May 3 '18 at 16:59
• @rolfl that's why I mention culture. Those questions could not be technically wrong due to rules depending on the english around it and what part of the question in stressed, but if it can realistically go on SO and get answered then it shouldn't be here. This does require users to understand what SO is, but I think it's worth far more than trying to write a good rule book or agree on "rulings" for question type. Also we should probably ask SE for a "Help me write code" website. I've heard complaints from people about that in every programming SE sight I've been on. Just give its own site. – user106363 May 3 '18 at 17:03
• I think you make a good point, and I don't think it's a point lost on any of us. You want to improve, you bring us the most basic version that does what you want - it's up to use to show the cases that won't work, how using objects would be better, what the style guides say. Asker's intent was that they got it to work but know they can improve. A-OK. But if you come in saying "I can't get x case to work" then it's off-topic. The simple difference is that we cannot review something that doesn't work - it needs to be fixed, not reviewed. – Raystafarian May 3 '18 at 21:01