Are vectorization requests on-topic?

Currently, there are 62 questions tagged , nearly all of which say "The performance sucks; how can I vectorize this code?".

Recently, How to efficiently apply correction to a time series with fully vectorized code in Matlab was closed, with this comment:

This question is off-topic because you have reviewed your own code, and decided that it needs to be vectorized to make it work better. Once you have vectorized the code you can bring that code back for review. Alternatively, we can review the code you currently have, but reviewers may just recommend that you vectorize it - which you already know. Note, we review the code you have, not the code you want to have. – rolfl♦

How is this question different from the 61 other questions? Should vectorization requests be considered off-topic altogether? Or are there specific guidelines as to what constitutes a valid vectorization question on this site?

• Saving this one until that diamond disappeared ;) – Raystafarian Apr 19 '18 at 21:13

I'm going to come off a bit harsh with this, but wow does that tag need help.

I think that, in the case of , the tag itself has a lot of problems.

First off, the tag-usage wiki doesn't actually tell anything about how to use it. The hover is useless:

Vectorization refers to a programming paradigm where functions operate on whole arrays in one go. This affords benefits in terms of function calls, memory access, parallelization and code expressiveness. Some programming languages, such as MATLAB, are optimised to give the best performance when vectorized.

And then the actual Wiki:

Vectorization refers to a programming paradigm where the process of loop-based, scalar-oriented code is instead written using matrix and vector operations. Vectorization has the following benefits:

• Performance: Vectorized code has better performance regarding function calls and memory access, and as a result, often runs much faster than the corresponding code containing loops.

• Appearance: Vectorized code appears more like the textbook mathematical expressions, making the code more comprehensible.

• Less Error Prone: vectorized code is shorter than loop based code, hence there are fewer opportunities to introduce programming bugs.

Some programming languages, in particular MATLAB, are optimised to give the best performance when vectorized.

I mean, what is that supposed to tell anyone?

If we want to keep the tag (which I personally suggest burning it due to the confusion it causes), we should reword the hover summary to talk about when to use it, and I think it should only be used if the code is already vectorized. If the question is about currently-vectorized code, the tag applies, and that's it. If the OP wants to vectorize it, well, they can post a self-answer with the vectorization, or tag it with the hopes that someone will vectorize it for them.

As with all questions, they can talk about what they want to see, but the should only be used when the question has vectorized code.

Specific question

Since I hammer-closed the question, and I added the comment, let me explain the reasoning I had, starting with a hypothetical case:

Hypothetically, someone asks a question: "this is my code, it works..."...

I then answer that question with: "Your code is very procedural and it would be much better if it was object-oriented"

That would make for a decent code-review question/answer.

If the OP then asks: "Can you make it object-oriented for me?" then it is no longer a code-review, but a "gimme-the-code" question.

In this case, the same thing has happened, except the OP has done a self-review.... they have looked at their code, and their code's performance, and identified an issue: "If this code was vectorized, it would be more efficient!". They are now asking: "Please vectorize my code".

The question is not asking for a review. It is asking for code, and in my opinion, it is off-topic.

Now, having said that, if they successfully vectorized the code, we would be on-topic to review that working code too.

Vectorization in general

I don't consider the tag to be off-topic in general. I think applying the tag to code containing vectorized operations to be a good thing.

I think the statement that the current tag is misleading, is overstating the problem. The current tag is:

Vectorization refers to a programming paradigm where functions operate on whole arrays in one go. This affords benefits in terms of function calls, memory access, parallelization and code expressiveness. Some programming languages, such as MATLAB, are optimised to give the best performance when vectorized.

I find that tag info to be clear, and accurately represent what vectorization is. Similar terms are "Object Orientation", "Recursion", etc.

The concern that the tag implies that people can ask to have their non-vectorized code to be converted to vectorized code is unfounded. That would be similar to worrying that people who tag their question with want their code converted to use linked lists.

Having said that, a scan of recent questions tagged with shows that many questions are incorrectly tagged. A good example is:

Find the minimum value that data could have had before it was rounded which I feel should be untagged (and currently has 4 close-votes - probably because of kick-back from this meta post). Note that the answer reviews the actual code and recommends recursion and not vectorization.... This is a good example of a mis-tagged question.

Just because people don't read the tag wiki does not make the tag bad. This is simply a case of un-tagging the tag from questions that don't require it.

I have edited the tag to add the text: Use this tag if your code contains vectorized operations

• I like it - brief, but to the point. :) (As those usage summaries should be.) – Der Kommissar Apr 19 '18 at 20:05
• I am downvoting because I disagree with the close reason - see my answer for that. – Simon Forsberg Apr 20 '18 at 8:39

rolfl's argument is that "I would like to vectorize this code to make it more efficient. Does anyone see a way to do this?" is off-topic because it asks for the code.

I disagree with this. "How do I vectorize this code" is a specific refactoring question (I believe this has been brought up on meta before but I have not yet found it). I honestly don't think we should differentiate between "How can I vectorize this code?" and "How can I make this code more object-oriented?" or "How do I write this code on just one line?" or "How can I make this code ?" In all cases, the author has done a self-review, they know what the problem with their code is.

If you argue that the question becomes on-topic if they ask for "How can I increase the performance of the code?" instead of asking for how to vectorize it specifically, I'm going to say that I don't think we should be so pedantic and require such a simple edit to a question to be the difference between on-topic and off-topic. The goal on code review is always: Make the code better/faster/harder/stronger (in some order).

Let's take a look at our on-topic questions, shall we?

• 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 rather than pseudo-code or hypothetical code? Yes
• To the best of my knowledge, does the code work as intended? The current code, yes. It works, but needs refactoring or performance boost. Doesn't make it off-topic.
• Do I want the code to be good code? Yes
• Do I want feedback about any or all facets of the code? A lot of our questions don't but we give feedback about all facets of the code anyway.
• In this case, the question is off-topic because it fails the "any or all facets of the code".... the question could be made on-topic by removing the request to vectorize the scalar code, and the tag abuse could be resolved by removing the vectorization tag. Editing the question could perhaps bring it on topic again, but the change is too radical to present it as the OP's original post. – rolfl Apr 19 '18 at 21:06
• @rolfl 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 Apr 19 '18 at 21:26
• We need to separate out the issue of on-topic from the issue of tag misuse. It's hard to treat them together. – rolfl Apr 19 '18 at 21:43
• This specific question does not ask for any code review. It uses the code as a "spec", and says "I need the same thing but vectorized". It does not want any form of review on the code given in the post. In fact, it ends: "Here is a working example using a for loop .... How can I vectorize this problem?" – rolfl Apr 19 '18 at 21:46
• @rolfl And how is that any different from "I need the same thing but object-oriented?" and where is this line that determines whether specific-refactoring questions are on-topic or not? – Simon Forsberg Apr 19 '18 at 21:48
• It is no different from "I need the same thing but object-oriented".... – rolfl Apr 19 '18 at 21:48
• I think you're right though - it's pretty pedantic. But, I think that's a different thing altogether - "this loop is slow, how can I speed it up" vs "How can I make this recursive?" That does seem to matter here. – Raystafarian Apr 20 '18 at 3:25
• @Raystafarian 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 <some other formulation> it would be on-topic."? That's not a comment I am willing to write. – Simon Forsberg Apr 20 '18 at 8:38
• @SimonForsberg that's my point, I wouldn't want to leave that either, but sometimes it seems that's the way the site leans – Raystafarian Apr 20 '18 at 10:35
• @Raystafarian The question we should be asking is if that's the way we want the site to go. – Simon Forsberg Apr 20 '18 at 11:35
• Yeah, maybe we should be asking that – Raystafarian Apr 20 '18 at 23:53

I suggest that we put an end to the because the naming is misleading. The tag is misleading because it leads users to think that we offer help in vectorizing code, GIMME TEH CODEZ, which we don't do.

I think that we should create a new tag, Vectorized, that lets reviewers know, "hey, this code is vectorized code" or "I want my Vectorization reviewed"

I have also posted a Meta for the Burnination and Creation --> Should we Vectorize [tag:vectorization] questions with a [Vectorized] tag?

and to 202_accepted's point, new tag = new description

• BURN IT TO THE GROUND – Der Kommissar Apr 19 '18 at 19:54
• No, it's a good tag. Just because some people have misused it does not make it bad. – rolfl Apr 19 '18 at 19:57
• Read your edit and agree, @rolfl – Malachi Apr 19 '18 at 20:05

A thought crossed my mind. Simon asked:

How does this question differ from "How can I make this code more object-oriented"?

To my mind I see some difference, though I don't know whether it's enough to matter. With a question like "how can I make this code more object-oriented," it's possible to give instruction or advice on that topic without directly writing the code for them. With vectorization, that seems a little more difficult.

For example, on a question like "how can I make this code more object-oriented," I could probably give high-level advice like, "Rather than using a separate x and y variable, you could make a Point class for holding positions. A Point class might have methods for adding or subtracting points or adding a vector to a point and other common actions." I probably wouldn't write an entire new class into an answer, though I might put a few snippets in.

For "how do I vectorize this code," I feel like there are fewer high level answers to that question. If you're iterating over a 2D array, you can probably give advice on whether iterating over columns first or rows first would be a better idea and why, but I feel like a lot more of it is just writing the code. But that's just a feeling based on my experience in vectorizing code.

Does this distinction matter, or is it a distinction without a difference? Does the degree of high-level overview vs. writing actual code matter? Is one preferred over the other? My guess is that it doesn't matter, but I thought I'd put it out there to see what others think.

• Note that vectorization in Matlab is quite different to vectors and points in other math and physics contexts: See some documentation. Having said that, I believe your basic point (no pun intended) is still valid. – rolfl Apr 22 '18 at 10:05