25

I'm asking in reference to this question that I just asked.

Often, I'll write a piece of code that works, but is ugly or inefficient. I know that Stack Overflow is not Code Review and isn't meant to be a place where people discuss the quality of code, but is it OK to ask a specific question regarding how to make a small chunk of code better, even if I already have a solution, as in my linked question? Or is this better suited to Code Review?

1
  • 1
    Just to note: small chunks of code fit into a larger puzzle. Its generally how a chunk of code fits into the larger puzzle that needs work, not just a small grouping of lines of code.
    – Gimby
    Commented May 23, 2018 at 8:04

4 Answers 4

32

Code Review has very strict rules about what’s on-topic and what’s off-topic, so a vague question about making “code better” isn’t going to work there in the same way as it’s not going to do very well here.

If you have a specific requirement for making the code “better”, like

  • Use less memory
  • Take less time
  • etc.

then asking here is fine.

You have a problem (using too much memory, taking too long) and as long as you phrase the question in those terms you should be OK.

17
  • So asking for alternative strategies would count as too vague, then?
    – sacuL
    Commented May 22, 2018 at 21:31
  • 14
    @sacul yes, that would too vague and also counter productive. You haven't defined what's wrong with your code so any suggestions are likely to be no better and could be worse in other ways.
    – ChrisF Mod
    Commented May 22, 2018 at 21:31
  • Yeah, I guess I can see that in retrospect... not saying I'll do this (I won't), but wouldn't that logic encourage one to pretend to not have a solution, and ask one's question anyways?
    – sacuL
    Commented May 22, 2018 at 21:36
  • 1
    @sacul there's nothing to stop you posting your question and solution as a Q&A pair. Don't accept your own answer though, let other people suggest their solutions. You might get a better one.
    – ChrisF Mod
    Commented May 22, 2018 at 21:37
  • 1
    "then asking here is fine." -- did you mean "then asking on [coderview.se] is fine? Here, such a question would be closed as off topic or opinion-based. Commented May 22, 2018 at 21:55
  • 3
    @ivan_pozdeev - no, "here" is Stack Overflow. If I'd meant Code Review I would have said "there".
    – ChrisF Mod
    Commented May 22, 2018 at 21:57
  • 1
    Then I stand by my statements that it would be closed. A code can be "improved" in a zillion ways, so it's impossible to give a definite answer, and every such suggestion will be equally valid. Commented May 22, 2018 at 22:00
  • 6
    @ivan isn't that what the 'if you have a specific requirement for making the code "better"' part takes care of?
    – Patrice
    Commented May 22, 2018 at 22:09
  • @Patrice good luck formulating a requirement that can be satisfied in a countable number of ways. If I make a program use n-1 bytes, or n-100 bytes, or n*3/4 bytes, this will all count as "use less memory". Commented May 23, 2018 at 0:51
  • @ivan I mean.... n-100 is clearly the optimal choice, for n<400 here. And after n*3/4 is better. I don't see the problem here..
    – Patrice
    Commented May 23, 2018 at 1:10
  • @Patrice the problem is that there's an infinite number of possible answers, and none of them can be a definite, complete one. This is off topic on SO. Commented May 23, 2018 at 1:14
  • 1
    @ivan_pozdeev (1) "There are multiple ways to answer" is not at all the same as "every answer is equally valid". (2) "good luck formulating a requirement that can be satisfied in a countable number of ways. If I make a program use n-1 bytes, or n-100 bytes, or n*3/4 bytes, this will all count as 'use less memory'" -- This kind of literalism is misguided. The good subjective/bad subjective guidelines are not, and never were, meant to be used in this way.
    – duplode
    Commented May 23, 2018 at 1:16
  • But every answer isn't equally valid. In your own example one is better for some scenarios and others are better for others (just like a lot of good stack overflow answers). Anyway I guess we just can agree to disagree here @ivan
    – Patrice
    Commented May 23, 2018 at 1:16
  • @ivan_pozdeev I can't think how to formulate a requirement that can't be satisfied in a countable number of ways. There are a countable number of programming languages, and a countable number of programs in each, and the product of two countable sets is still countable. Did you mean "finite" rather than "countable"? Commented May 23, 2018 at 5:08
  • 1
    I always thought vague questions about making code better is exactly what the bulk of Code Review is about (while specific questions about making code better is fine too). I suspect asking about only 4 lines of code is the bigger problem there. Commented May 24, 2018 at 8:59
6

I know Python (but not Pandas), and that question looks fine to me. You have a single specific problem, and you've shown us what you've currently tried. It gives the desired output, but you want to know if there's a better way (more readable &/or more efficient). That's fine, question code on SO isn't required to be broken, IMHO. Because your question is focused it means that possible answers can also be focused.

However, if you posted a complete application that does a whole bunch of different things, and you wanted us to improve the whole program, then the question would not be a good fit for Stack Overflow. That's essentially a bunch of different SO questions bundled together, and while each item in the bundle may be useful to future readers the whole bundle is unlikely to be so.

The bottom line is that we want questions where the answers are likely to help multiple future readers as well as the OP.


We sometimes get complete but broken programs posted by new coders, where their code has several unrelated problems, and all of those problems need to be fixed in order to achieve a working program. That's not a great fit for SO, but it's tolerable (IMHO) when the code is for a small homework exercise. It can be very instructive for a student to see a working version of their code, with brief explanations of what they were doing wrong. And such answers can be helpful to other students in the future.

OTOH, there are a handful of common problems that arise in such code, and there's no real need to write a new specific answer because a good duplicate target already exists, eg Asking the user for input until they give a valid response. Of course, there's a good chance that there are existing questions with nice solutions about the same (or very similar) homework problem, in which case the new question should be closed as a dupe, although in some cases it may be useful to write a short specific answer for the new question.

5

It can be on topic -- if you manage to formulate it just right. Which is the tricky part, and if you're new, you're very likely to slip and get it closed.

A question on SO must be:

  1. asking how to solve a specific, practical programming problem.
  2. fully, definitely answerable in up to several paragraphs.
  3. while specific, be general enough to be useful for future readers with similar problems.

So, you need to:

  • clearly formulate what specific problem you're trying to solve, and how it is practical if that's not obvious. This will satisfy 1).
    • "make code less ugly" is not a specific problem, and "make it use less resources just because" is not a practical problem. "Suggest an algorithm that would solve more-or-less recurring problem X faster than in O(n^3)" (if there's a reason to believe that it exists) or "what way(s) library Y provides to solve problems like Z" are.
  • show your research. This is critical:
    • First, the more theory needs to be explained, the more efforts are needed to answer fully -- so the better you need to show that you're worth expending that effort on you.
    • We also need to know what level to explain things on, what you already know and what pieces of information you're missing. This drastically reduces the space and effort required for a full answer -- so makes the question more likely to satisfy 2)
  • Isolate your problem from details not immediately relevant to it, and compose a Minimal, Complete and Verifiable example. This is the way to present a problem statement and illustrating code to satisfy 3).

If all this sounds overwhelming to you, it probably is. I said this is very tricky to get right.

6
  • "what elegant way(s) library Y provides to solve problems like Z" is not a practical, specific problem. What is an objective way of evaluating "elegance"? Sounds primarily opinion based to me. Commented May 23, 2018 at 16:57
  • @MikeMcCaughan I hesitated to add "elegant" but ultimately decided in favor of it because in the IT croud, this term is more or less well-defined. It basically means: "not only possible, but the lib is clearly designed to support this way / it doesn't require jumping through hoops." Commented May 23, 2018 at 17:04
  • @MikeMcCaughan added a clarification comment. If clarification is needed, this is probably a needless complication, and this matter is irrelevant for this particular question... Commented May 23, 2018 at 17:28
  • "elegant" part is very vulnerable to "opinion-based" close
    – user3458
    Commented May 23, 2018 at 18:50
  • "3...be general enough..." is, unfortunately, not a requirement, or hasn't been recently. Go ahead and ask your own very specific case, SO Code Writing Service will "swing into action with keenness and speed". "Free and worth every penny" is our motto.
    – user3458
    Commented May 23, 2018 at 18:52
  • @Arkadiy we can still eel our way out of it by closing it as "lacks an MCVE" and/or downvoting. Non-reusable "work requests" are off topic, there isn't a close reason for them only because no-one has come up with a good enough formulation that's resistant to abuse. Commented May 23, 2018 at 19:21
5

Pandas Python package has a complex API. It is perfectly ok to ask how to do a specific task more idiomatically using pandas if you think your approach "seems messy". Readability counts (PEP 20 -- The Zen of Python)

"Pythonic (pandorable) way to do X" is a legitimate question. It may not always have a definite answer but often there is a "neater" way to do X: "There should be one-- and preferably only one --obvious way to do it."

For example, compare:

for item in sequence:
    do_something(item)

vs.:

#XXX non-idiomatic
i = 0
while i < len(sequence):
    do_something(item)
    i += 1

there is no question what variant is more pythonic. For a Python programmer there is nothing "opinion-based" here.

6
  • I would say asking for a "Pythonic (pandorable) way to do X" should be qualified with a specific concern, e.g. code maintainability, readability, performance, ability to extend. Far too often, even with pandas, I see "pandorable" equated to the need for a one-liner which, when satisfied, leads to bad practice. A good example is "just use pd.DataFrame.apply(foo)". Readable? Yes. One-line? Yes. Good practice? Probably not.
    – jpp
    Commented May 25, 2018 at 8:32
  • @jpp: If you have "a specific concern" then you ask about your specific concern. "Pythonic" encompasses code maintainability, readability, performance, ability to extend, etc if you don't have specific preferences to shift tradeoffs from the default position.
    – jfs
    Commented May 25, 2018 at 9:13
  • @jsf, I would like to agree with you. But I know for a fact many new users equate Pythonic with "one-linize"..
    – jpp
    Commented May 25, 2018 at 9:14
  • @jpp: many people believe in many false things. It doesn't mean that facts are matter of opinion. «Pythonic doesn't mean "one-liner"» is a fact. It is not an opinion.
    – jfs
    Commented May 25, 2018 at 9:36
  • That's essentially what I was going for... I just thought my original way was hacky and messy, and expected someone to have a less roundabout alternative. Thanks for pointing out the "readability counts" clause!
    – sacuL
    Commented May 25, 2018 at 12:42
  • Wait, do Pandas users really say "pandorable"? Commented May 17 at 6:34

You must log in to answer this question.

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