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 \sloppy does:
- Sets
\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.
- Sets
\hfuzz and \vfuzz to 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 0.1pt.)
- Sets
\emergencystretch to 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.
\urlof theurlpackage (instead of\texttt), instructing the macro to break at any letter, with this preamble addition:\makeatletter \g@addto@macro{\UrlBreaks}{\do\/do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X\do\Y\do\Z}\makeatother– Steven B. Segletes Oct 01 '17 at 16:21hyperref, that package will try to turn your\urlmacros into hyperlinks. – Michael Palmer Oct 01 '17 at 18:06setting, for example:\setlength{\emergencystretch}{6pt}`. If I understand this correctly, LaTeX will then first try to typeset a paragraph using its usual strict settings, and if that fails, will make a second try that uses the specified amount of extra horizontal space. – Michael Palmer Oct 01 '17 at 18:15\sloppysets\emergencystretch; see my answer below. In fact,\sloppysets it to the far larger (and therefore more useful) value of3em, which amounts to30ptat the default settings. IMO it makes no sense to declare “\sloppyis bad practice” and then implement a poorer version of\sloppymanually. :-) (In fact, simply increasing\emergencystretchwithout increasing\toleranceis likely to make TeX resort to the "emergency" even when there's no serious emergency.) – ShreevatsaR Oct 01 '17 at 18:35\sloppyas bad practice; I just offered an alternative for the OP to consider. Unlike\sloppy,\emergencystretchattempts to typeset each paragraph in a strict fashion first and resorts to doing it sloppily only when that initial strict attempt fails. The exact length value that I suggested was obviously not set in stone; it would be up to the OP to determine what value would be the best trade-off between unsloppy appearance and reduction of manual line breaking. – Michael Palmer Oct 01 '17 at 19:05\sloppyis defined using\emergencystretch. If you have an example where setting only\emergencystretch(without also increasing\toleranceas\sloppydoes) results in better output, I'd like to see it. :-) – ShreevatsaR Oct 01 '17 at 19:18\urlapproach, but don't want hyperlinks, then perhaps an answer based on this one could achieve what you want: https://tex.stackexchange.com/questions/219445/line-break-in-texttt/219497#219497 – Steven B. Segletes Oct 01 '17 at 19:34