Should numbers inside the text be placed inside a math environment?
For example, if I am writing
...indent code by 4 spaces..., should I write it this way or
...indent code by $4$ spaces...?
Perhaps the output is the same but semantically they are not and maybe LaTeX can treat them differently in some cases. Which is the right one?
For instance, how does LaTeX treat the numbers in the counters it uses for numbering the sections or pages, etc? Does it treat them as numbers in a
Math environment or as numbers inside text?
Of course, if the output can never be different in any situation, then well, it just doesn't matter.
The answer to your question strongly depends on whether the math-mode numerals appear to be (more or less) the same as text-mode numerals, for the font you use. If the two sets of numerals happen to be exactly the same, nobody will ever be able to tell unless they have access to the source code...
However, if they differ noticeably in appearance -- say, because "oldstyle" numerals are used in text mode, which in general should not be used in mathematical expressions -- you obviously need to be careful to distinguish between the two types of numerals.
The following is an excerpt from Knuth's article "Typesetting Concrete Mathematics" (TUGBoat, 1989) [Aside: The textbook Concrete Mathematics was typeset using
Concrete Romanas the text font and
Euleras the math font]:
What's the upshot of all this? I'd say it's the following: even if at present you're using text and math fonts that have identical-looking numerals, you should still be willing to make the extra effort today to set those numerals that are parts of math expressions in math mode -- as long as there is a chance that you might wish to re-publish your piece one day using fonts for which the math- and text-mode numerals are no longer the same.
What, then, are instances of when you should use text-mode numerals? In addition to the examples given in the preceding quote, I'd also nominate the following: page numbers; numbers of chapters, sections etc; dates (December 25, August 1, ...), and (depending on the circumstances) equation numbers. Obviously, this short list is not meant to be exhaustive.
Addendum: I just noticed that the first two up-votes to this answer earned me my 10,000th point in the TeX.SE group. Thanks everyone!! :-)