31

I came across this question and while not understanding the syntax of the code snippet, have an idea of the logic required to solve the question. The code is written in Java and I have an understanding of Java but this code snippet is beyond my knowledge. However I believe I could explain the logic required to solve the problem using pseudocode. (see also Algorithm)
I have attempted to explain the logic in the comments but it is getting excessive and I feel like a better explanation could be provided in the form of an answer.

I can understand that there is a few positives such as:

  • It may help the asker understand what needs to be done without writing the code for them, giving them practice and experience themselves.
  • Someone without experience with a library or syntax the asker is using in their code may be able to provide assistance in cases where someone with experience cannot or has not.

However I can also see some negatives:

  • The asker may not understand pseudocode and as such providing an answer in this form (or a form designed to simply explain the logic) may not be well-received.
  • The logic may not be applicable with the syntax of the language used.

For this question (and others) is this bad practice on SO?

7
  • 1
    I guess you provide as good answer as you can. If it somehow is answer to question, then you are good to go with pseudo code, without any code or just by pictures does not matter.
    – gevorg
    Commented Jun 23, 2016 at 5:57
  • 18
    I often prefer pseudo code to a copy-paste-able answer. Pseudo code allows the asker to learn.
    – Cerbrus
    Commented Jun 23, 2016 at 6:28
  • 27
    "The asker may not understand pseudocode" Please explain to me how someone can be a programmer that understands one or more programming languages (and therefore has some basic knowledge about constructs and algorithms), yet cannot read pseudocode. If this person exists, she would be appropriately described as "helpless," not the type of asker we wish to encourage on Stack Overflow. Even a non-programmer should be able to understand pseudo-code with a sufficient amount of effort!
    – Cody Gray Mod
    Commented Jun 23, 2016 at 13:48
  • @CodyGray I often see many users on SO who are coding or learning to code from tutorials. They only have a basic grasp on coding and nothing more extensive than what they have learnt in the tutorials (in which pseudocode would most likely not be taught). Yes, this is not the type of asker we wish to encourage here, but there is no way of stopping these people from asking questions. Tell-tale signs are usually bad grammar/formatting and poorly written code (no intrinsic documentation etc.). While these questions can be flagged or deleted, it is more productive to teach beginners how to use SO. Commented Jun 23, 2016 at 23:56
  • 3
    "there is no way of stopping these people from asking questions." Sure there is. I can think of several ways. One way is to not answer their questions. Another way is to post answers to their questions that are correct and useful to programmers, but not [yet] useful to the OP. They'll either have to rise to the challenge, or take their questions and their laziness elsewhere. A third way is to close the questions before they get answered. Obviously determining which one of these responses is appropriate will need to be done on a case-by-case basis, judging the question, not the asker.
    – Cody Gray Mod
    Commented Jun 24, 2016 at 4:13
  • 8
    The point is, remember the target audience. Our target audience is "professional and enthusiast programmers". Those people would rightfully be expected to understand pseudocode. It is not our job to teach people the basics of programming. If you found an edge case, someone who was actually trying but just couldn't understand your pseudocode, you could take more time to explain it in words or alternate code examples. But you are asking about a general decision-rule here, whether pseudo-code is appropriate. I'm saying it undoubtedly is on a programmer Q&A site.
    – Cody Gray Mod
    Commented Jun 24, 2016 at 4:15
  • 2
    Posting pseudocode means you aren't feeding help vampires and cargo-cult coders. Sure, it's nice to get code, but professional and enthusiast programmers come to SO to learn; if you get code as part of a well-written explanation of how to solve your problem that's a bonus, but not essential. OTOH, it can be easier to write & test a chunk of code than to write a good explanatory code-free answer. :)
    – PM 2Ring
    Commented Jun 24, 2016 at 10:50

2 Answers 2

25

It's not bad practice overall, but in this case, I'm not sure that it would have helped. The question isn't that great and may be closed/deleted at some point, as it has already attracted two close votes. Answering is not the best option in cases like this.

But, if the question isn't a bad question, it is perfectly OK to use pseudo-code. As someone who is active in , I answer many questions that aren't in a language I know (since regex flavors are all very similar). Sometimes, I know regexes aren't the answer (*gasp*) and I will explain, in plain English, what steps they need to take.

While there are some users who just want "teh codez", it's not your responsibility to write it for them. Anyone with some comprehension of English and the programming language at hand should be able to create the code based off pseudo-code. Even if the OP is completely incompetent, your answer may provide a starting point for another answer to implement your algorithm.

-22

In my opinion, if you are writing pseudo-code in an answer, then you should probably take a moment to ask yourself - is this question too broad?. Good questions almost always show sample code, that way, you can build on top of the OP's code when you answer. A question that requires you to write complete (pseudo)code in your answer is almost always unfit to be on SO. Remember, SO is not for general questions and answers.

We almost always expect the OP to specify the language being used when he / she posts a question. Under usual circumstances we don't want generic answers. What the OP (and other programmers who look at the post) usually want is an answer that is specific to the language being used. Personally, I would not go to the PHP tag and look for an answer if I am using Java (even if an answer exists there). I would probably ask the same question in the Java tag (and it would be incorrect to mark it as a duplicate of the PHP one).

Lets take an example :

....
Store data in Map1
Replace all digits in yourString with 0.
Add ourString to a List.
Read input from file X and store it into a String array.
...

Several questions could come up that could determine whether your code would work or not. Assuming you are using Java.

  1. Which Map implementation should I use?
  2. Should I use replace or replaceAll?
  3. Which List implementation?
  4. Should I use Scanner or BufferedReader + FileReader or Files.readAllLines()?

If something is incorrectly assumed in the pseudo code, then the entire code might break (what if you make assumptions that a data structure is mutable but in the OP's language it is not? Or what if a data structure that you use doesn't exist in the OP's language?). Now, we know that pseudocode is not language specific but that doesn't mean that we cannot hit a brick wall when implementing it in a given language.

Does that mean, I always have to write answers that contain code?

No. But you could write something that is language specific.

For example :

....
Create a LinkedHashMap and store data in it.
Replace all digits in yourString with 0 using String#replaceAll()
Add ourString to a LinkedList
Read input from file X and store it into a String array using Files.readAllLines()
Parse Strings as Integers using Integer.parseInt()
...

This way, your answer will still be helpful to a lot of people looking to solve this problem in java.

Finally,

For this question (and others) is this bad practice on SO?

It is not always bad, but I think it would be better to stay away from posting such answers.

7
  • 23
    Meh, too much of this answer sounds like a plea for Stack Overflow to be a code-writing service. I cannot agree with that. In particular, I disagree with the assertion that "Good questions almost always show sample code, that way, you can build on top of the OP's code when you answer." Whether a question should include sample code very much depends on the type of question. And even for questions that do include sample code, it doesn't necessarily mean that a good answer will "build" on that code. That's what they do on HireAProgrammer.com. We provide expert insight that might include code.
    – Cody Gray Mod
    Commented Jun 23, 2016 at 13:46
  • 6
    I must agree with Cody here. I write very little code of any kind in my answers. Even if it's a "what's wrong with this code" question, I'm far more likely to excerpt the incorrect sections from the OP and just explain what's wrong with them, rather than actually writing the correct code. I leave that as an exercise for the reader. Commented Jun 23, 2016 at 14:29
  • @CodyGray - I agree, not all questions can have code. I also accept that we can have good questions without code. My point is that it is if you don't want to give exact code, you could still write language specific instructions on how to achieve something. It would make the OP's job a lot more easier. You are talking about people who know the structures of the language they are using but are looking for an approach. I am talking about the people who are completely not familiar with the data-structures of a language, yet want to implement a given algorithm.
    – TheLostMind Mod
    Commented Jun 23, 2016 at 15:42
  • @NicolBolas - I am not saying that one should always write code in his answers. I am saying that it is acceptable (or even good) to write pseudo-code that is kind of language specific. That way, the OP doesn't have to worry about what data-structures / classes / methods to use. I understand SO is not a coding service but then again we don't want really general answers too. Which one do you think is clearer? - read all lines from a file using Files.readAllLines()... parse each String as an Integer using Integer.parseInt() or read all lines. For each line read, parse it as an Integer?
    – TheLostMind Mod
    Commented Jun 23, 2016 at 15:48
  • You can, of course, answer in any way that you like. Plenty of people do exactly as you describe, and I don't necessarily have a problem with it. What I do have a problem with is making it sound like this is a requirement or expectation of answers on Stack Overflow. If you think an answer that includes a sample implementation in a specific language is the most helpful answer, then post away. There are, however, plenty of cases where I don't think that is true. Like Nicol, the majority of my answers, at least the ones I'm proud of, contain very little code. I find plain English easier to read.
    – Cody Gray Mod
    Commented Jun 24, 2016 at 4:18
  • @Cody Gray: This answer reads to me like "I really hate pseudocode and you should too"
    – BoltClock Mod
    Commented Jun 24, 2016 at 9:46
  • 3
    I don't see how the use of pseudocode is any indication of a question's broadness (breadth?).
    – BoltClock Mod
    Commented Jun 24, 2016 at 9:46

You must log in to answer this question.

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