Patents are (theoretically) a pragmatic compromise between 1) the benefits to society of having people publish their ideas, and 2) the drawbacks to society of giving someone a time limited monopoly on the exploitation of the idea.
For software I believe the drawbacks in general far outweigh the benefits. There might be examples of "nobody else would have thought of this within the patent period" inventions, but they are rare enough that it does not make sense to have special laws for those cases.
In the physical "better mousetrap" world the issue is different. The situation is similar to the Hacker News startup world in that ideas are plenty. However, the cost of productizing the ideas is high, and there is less of a first mover advantage, access to distribution channels is more important. So patents in the "better mousetrap" world protects not the idea, but the investment of turning the idea into a product. The drawbacks of patents here are also smaller, as you rarely build unrelated products upon the "better mousetrap", so the cost of a monopoly often stays limited to the specific market. Patents here might be a good compromise.
In medicine there is a similar situation in that many ideas are "free" from public funded research, but useless without the very expensive clinical trials. The patents basically protects the investment in clinical trials, and no so much original research. However the cost of patents is huge. People die because they can't afford patented medicine (which include initial cost for the clinical tests, marginal cost, distributed cost of failed products, and profit), even though they could easily afford the marginal cost alone. It would be good to get rid of patents here, but they must be replaced with another system to cover the cost of the clinical tests.