Here are a few of my tips for focusing while coding (YMMV):
- Have a compelling reason or goal for why you are coding or learning to code
- Use the Pomodoro Technique
- Experiment with music, white noise, no music
- Take walks when stuck
- Make sure to take breaks to recharge. During breaks focus more on your senses rather than your thoughts.
- Use pen and paper to brainstorm especially before coding.
Most importantly, remember why you began coding–because it is fun. Focus on doing fun coding tasks as much as possible and the concentration and flow will often take care of itself.