Asking Smart Questions

09 Jun 2023

It’s widely known that working smarter is better than working harder no matter what the task at hand may be. Therefore, when asking questions it is our perogative as developers to look for solutions or seek help in a sharp and focused manner, but the effort must be put in to find a solution on our own before reaching out to others for help. Naturally when I first started programming I had a TON of questions, and a lot of times when I went looking for answers to my questions online, I would find answers on StackExchange or whichever forums I came across that answered a certain question through other techniques I’ve never seen before. Althought I didn’t find a specific answer to my question immediately or the answer to my question was solved in a way I did not understand, I learned so much more about other concepts/techniques that if I was just concerned with getting a straight to the point answer, I would have never learned what I did. Here’s an example of a question that could have been solved with just a little bit more research and effort from the developer.

File opening with a program
Asked today
Modified today
Viewed 6 times
-1

I'm building my text editor in c++ with sfml. The problem I have is that windows doen't let me edit more files at once.

I mean i can't select, for example, 2 files and say edit. Windows will just start 2 times my .exe. How to fix this like notepad++ do?

Thanks in advance

URL - Link

Regarding OP questions, the questions was very briefly described but not only that it is a question which can be easily solvable through a little more research through Google or just from utilizing the correct documentation online. According to the article “How to Ask Questions The Smart Way” by Eric Steven Raymond, the very start of a question should have a header which is concise and specific, for example(as listed in the article): (Stupid Question –> HELP! Video doesn’t work properly on my laptop! —— Smart Question –> X.org 6.8.1 mouse cursor on Fooware MV1005 vid. chipset - is misshapen). Then the next step which is the most important step, is to describe in detail about what the problem is, what did you try to do to solve the problem, and perhaps a visual example of the problem(screenshot/screen record). Although there a lot more intricacies to asking a smart question, here’s a good example of a smart question asked below:

Why is processing a sorted array faster than processing an unsorted array?
Asked 11 years, 2 months ago
Modified 5 days ago
Viewed 1.8m times
27078

In this C++ code, sorting the data (before the timed region) makes the primary loop ~6x faster:

#include <algorithm>
#include <ctime>
#include <iostream>

int main()
{
    // Generate data
    const unsigned arraySize = 32768;
    int data[arraySize];

    for (unsigned c = 0; c < arraySize; ++c)
        data[c] = std::rand() % 256;

    // !!! With this, the next loop runs faster.
    std::sort(data, data + arraySize);

    // Test
    clock_t start = clock();
    long long sum = 0;
    for (unsigned i = 0; i < 100000; ++i)
    {
        for (unsigned c = 0; c < arraySize; ++c)
        {   // Primary loop.
            if (data[c] >= 128)
                sum += data[c];
        }
    }

    double elapsedTime = static_cast<double>(clock()-start) / CLOCKS_PER_SEC;

    std::cout << elapsedTime << '\n';
    std::cout << "sum = " << sum << '\n';
}
Without std::sort(data, data + arraySize);, the code runs in 11.54 seconds.
With the sorted data, the code runs in 1.93 seconds.
(Sorting itself takes more time than this one pass over the array, so it's not actually worth doing if we needed to calculate this for an unknown array.)

Initially, I thought this might be just a language or compiler anomaly, so I tried Java:

import java.util.Arrays;
import java.util.Random;

public class Main
{
    public static void main(String[] args)
    {
        // Generate data
        int arraySize = 32768;
        int data[] = new int[arraySize];

        Random rnd = new Random(0);
        for (int c = 0; c < arraySize; ++c)
            data[c] = rnd.nextInt() % 256;

        // !!! With this, the next loop runs faster
        Arrays.sort(data);

        // Test
        long start = System.nanoTime();
        long sum = 0;
        for (int i = 0; i < 100000; ++i)
        {
            for (int c = 0; c < arraySize; ++c)
            {   // Primary loop.
                if (data[c] >= 128)
                    sum += data[c];
            }
        }

        System.out.println((System.nanoTime() - start) / 1000000000.0);
        System.out.println("sum = " + sum);
    }
}
With a similar but less extreme result.

My first thought was that sorting brings the data into the cache, but that's silly because the array was just generated.

What is going on?
Why is processing a sorted array faster than processing an unsorted array?
The code is summing up some independent terms, so the order should not matter.

URL - Link

In the question asked above, the OP specified in the header exactly what they were curious about and included two examples of code written in C++ and Java showing that they did the work to compare the time it takes to process a sorted and unsorted array. The question is very clear along with the visible effort that the OP showed, and is a prime example of how we all should be asking questions whenever we reached a point where we did all we could to try to find an answer but still couldn’t find one. In comparison to the first question asked, the OP asked a clear question where the intent was obvious and not obscure like most questions can get, there is no immediate assumptions about why this might be, no groveling, it is asked within the right forum category, and overall has a nice flow/grammatical structure all around. A great question can really involve the community and further spark more conversations about things we never even knew of, furthering our knowledge of programming and computers.