1

I know we are generally not supposed to touch other people's code when editing, but sometimes I come across old questions that have a lot more code than is needed for understanding the problem and getting to the answer. In fact, it makes it slower for future visitors, both to grasp the problem and to scroll down to the answers.

Here is an example.

Is it OK to just remove that first large code block?

3 Answers 3

3

Sure, you can edit out anything irrelevant that you come across, whether in a question or answer, whether old or new.

The catch is, it actually needs to be irrelevant, and you need to be certain that it is irrelevant. And I'd go so far as to say that most of the time, that is not going to be the case.

For prose, it is a bit easier to make this determination of irrelevance. Everyone knows you can edit out greetings, salutations, platitudes, and other meaningless ramblings. But for code, it is a bit more difficult to decide what is irrelevant. You have to be a language expert, and you probably have to be certain of what the answer is before you can decide.

In this particular case, I'm hesitant to say for certain since I'm not a Java language expert, but I don't think the code is irrelevant. As others have said, it is important to provide context. That small snippet in isolation may not be enough to understand how the code in question works. I'd certainly err on the side of leaving it be.

I do agree with giusti, though, that you could optimize the presentation of the question by reordering the code blocks. Put the important stuff first, and the optional context second. This addresses your concerns about simplicity and making it easier to understand what the question is asking. There will still be a need to scroll, but scrolling isn't that difficult if you know that you can skip the rest. A big block of code is honestly pretty easy to ignore, if you decide you don't need to study it.

2

In any case, you could probably reorder the question so that the most relevant piece of code comes first and the larger context appears later.

Something like this:

I am trying to understand the behavior of the method SetText in the following piece of code:

int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);

question is an int here, I wonder how this works. More strangely, if I change question into a literal int, e.g. 1, then the app won't work.


For context, this an example from Big Nerd Ranch Guide where the aforementioned text is used:

longer piece of code follows

This is a general case suggestion. I didn't thoroughly consider the impacts of doing so in the specific question you linked. But it might work.   :)

0

In my opinion, some of the information from that large block of code may still be useful (otherwise it is difficult to infer the variable types).

However, most of that large block of code certainly seems irrelevant, thus making it difficult for future visitors to grasp the problem quickly, like you mentioned.

You must log in to answer this question.

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