There are downsides to being distracted. (See above.) But here I want to talk about the upside to being able to distract oneself when appropriate. A good example is the famous Stanford marshmallow experiment. Here is the current opening paragraph of the Wikipedia article on the Stanford marshmallow experiment:
The Stanford marshmallow experiment was a series of studies on delayed gratification in the late 1960s and early 1970s led by psychologist Walter Mischel, then a professor at Stanford University. In these studies, a child was offered a choice between one small reward provided immediately or two small rewards (i.e., a larger later reward) if they waited for a short period, approximately 15 minutes, during which the tester left the room and then returned. (The reward was sometimes a marshmallow, but often a cookie or a pretzel.) In follow-up studies, the researchers found that children who were able to wait longer for the preferred rewards tended to have better life outcomes, as measured by SAT scores, educational attainment, body mass index (BMI), and other life measures.
One detail not mentioned in the Wikipedia article that I remember reading somewhere is that many of the children who didn't eat the marshmallow managed this feat by distracting themselves somehow (despite the lack of outer distractions in the room).
Distracting oneself can be a good way to resist temptation. For example, if tempted to eat something with sugar in it (see Sugar as a Slow Poison, turning one's attention to a good TV show out of immediate reach of anything sugary can reduce the strength of that temptation. At a bigger level, constructing an interesting life for oneself in other ways can provide distraction sufficient to reduce the strength of the temptation to lie, cheat and steal.
For moments of temptation when one is caught otherwise unprepared, it is good to have ready a powerful, innocent mental distraction. For some, a good math problem would work; for others, a memorized poem or song might work. One way or another, figure out a way to distract yourself from temptation.
If there is no way to distract yourself from a temptation, chances are the temptation has an element of addiction or obsession to it. Assuming it is a bad addiction or obsession, it is then good to get help when one runs up against a temptation not amenable to the power of self distraction.
If the temptation is the temptation to get distracted, that is not the subject of this post. But the image at the top may be helpful.