This is how
\sloppy is defined (from the LaTeX sources, which you can read with
texdoc source2e or via CTAN or texdoc.net):
So these are the things
\tolerance to 9999: this is not as bad as the name makes it sound. If you're not inclined to pay attention to underfull/overfull box warnings and fix them, in fact this is never a bad idea: all that a higher
\tolerance (but non-infinite, i.e. less than 10000) does is allow TeX to consider worse line breaks, while still trying to generate an optimal paragraph. It is highly unlikely to make the output worse and the worst you can say about it is that it makes TeX work harder, but on today's computers the difference can be measured in milliseconds.
0.5pt. This only affects what warning is shown to you: only lines which are overfull or underfull by more than
0.5pt are warned about. (The default is
3em. This is the most crucial thing for preventing overfull lines IMO (what you called “overshoots on the right margin”). It is additional stretch that TeX adds to each line, after everything else has failed. You can set it as large as you want, if you're willing to accept extra-large spaces between words on such lines. (If you're not going to rewrite text for dealing with overfull boxes, extra-large spaces are definitely better than lines sticking out of the right margin!)
Now that you know and understand what
\sloppy does, you can decide for yourself whether or not you'd prefer to apply it globally, rather than go by maxims like “global
\sloppy is bad practice”. There are cases where that makes sense, and cases where it doesn't: if you have such hard-to-break lines throughout your document, then it most definitely makes sense to globally take measures that will result in the best output.
Personally, for the case you mentioned (document with many hard-to-justify lines because of variable names in
\textt forming unbreakable boxes), this would be my order of preference:
- Leave everything at the default settings; manually watch out for and fix overfull and underfull boxes by rewriting text, etc. This degree of polishing you can do if your document is worth the effort and you're sure you've already polished the content enough (which you should always do first). Else, below I assume you're not going to manually fix overfull boxes.
- Globally (across all paragraphs of this type) use whichever parameters of
\sloppy you think you want: either
\sloppy itself, or
\tolerance 9999 along with as much
\hfuzz as you don't want to get warned about, and as much
\emergencystretch as you can tolerate (the higher the better for avoiding overfull lines).
- Give up on paragraph justification, and use ragged-right text. Text won't line up at the right margin, but you won't need any awkward spaces or hyphenation.
- Use hyphenation inside the
\textt variables, globally: there are ways to do this, but as you said it can suggest to the reader that the variable is hyphenated, which I would really avoid.
The choice between (2) and (3) (
\sloppy-like, or ragged-right) would depend on the exact document. Of course everything is subjective and a function of your aesthetics.