As someone who enjoys writing code and tries to get more people interested in software development, I've met my fair share of people who think I'm crazy for having fun while writing code. And don't get me wrong, I've also dreaded and hated writing code as well. Ultimately for me, it depends on the "why" that really determines whether I'm going to enjoy or dread the time I spend at a keyboard staring at a bunch of random characters that look like someone let their child press random keys on the keyboard.
So what is the "why" behind writing code? For me, it's the motivation for why I'm spending my time trying to build something with code rather than spending time with my friends and family. And when I think about the fact that instead of writing code, I could be having fun, it means I really need to be motivated. So let's consider some different scenarios and how they may be more motivating than you think.
Getting a new job
For some, the idea of getting a new job might motivation enough to learn how to code. But if you're busy, have a family or maybe have a decent job, it's hard to sacrifice a fair amount of time to learning how to code. on the other hand, if you think that a new career as a software engineer can be a path to financial freedom or financial security for you and your family, it seems a lot more motivating. Another reason could be if you have an idea or want to start your own business one day and either having the skills of a software engineer or building the business as a software engineer is the path to achieving that goal. So the framing of what you want and how important it is to you can change the motivation behind the activity.
Building something for yourself
So easily one of the most motivating things for me is to build something that I'm going to use. If it's something that will make my life easier or more enjoyable, I'm going to be more motivated to spend time building it and fixing it. One of the best techniques when learning how to code is to come up with something you wish someone else would have built (or maybe someone built it already but you think it could be better). With this in mind, as you learn how to code, you're developing to skills that will eventually let you build that thing you had in mind. For me, it's much motivating seeing that each page I read, lesson I take, course I complete is building up to allowing me to build X. A real-life example of this was learning how to build a web scraper to get Instagram data automatically. Instead of going to Instagram everyday to look at how many followers I had or who followed/unfollowed me, I wrote a program that did it for me and I would just look at the results whenever I was curious.
Building for someone else
Another great motivator is others. Similar to the previous section on building something for yourself, having a goal in mind that can be used by someone else can be really motivating. Not only does it give you a clear goal, but having someone else you can tell about your progress or talk about the project with will help keep you on track. Again, each hour you spend learning more gets you a little closer to seeing someone use the thing that you build. One time this worked for me was when I had a friend who wanted a reminder before going to bed of their first meeting the next day. So I figured out how to build a bot that checked her Google calendar and then sent her a message including the meeting details, time and location each night. It was fun because we would try it out and she gave me feedback on changes and even when it didn't work, it was an opportunity to fix it make it better. Having an actual user giving you feedback is great motivation and gives you a sense of accomplishment when you get it working.