So, your code must look tidy, and, thankfully, writing such code is quite easy to master, it won’t be an issue. The real struggle is to write code which not only looks visually nice, but is also eloquent and elegant in its structure and logic.
The good news is you needn’t write such code right away. Feel free to start with the one with pretty “ugly” logic and structure, as almost everyone does.
There are two common problems beginner programmers stuck on. The first one — how to build the logic of your program: where you should start, what follows next, etc.
The second problem is how to structure your code so that its parts can “communicate” with each other, passing around all the data, reading it, saving it, etc.
Usually it seems pretty straightforward as an abstract idea in your mind, but how to implement it in code?
Start with small steps
Of course, if you learned programming with good video tutorials or books, you saw how their authors wrote code and likely adopted their style and approach to writing and structuring programs. This will surely help you creating your programs a lot.
But once you start writing code completely on your own (for example, when you have to do programming exercises from your learning course), there’s still often some lack of understanding of what to do and how to do it.
If you just recently started learning programming and now write one of your first programs, usually you have almost no idea how to do what needs to be done.
Then, after thinking it through and, maybe, reading helpful advice on the Internet, you understand how to write some tiny piece of code, doing the very specific job, — say, getting the data that a user of your program enters using a keyboard. That’s a good start.
Gradually your program grows more and more. After getting the user input, you find out how to save it into the database. Then you add the code that now retrieves the user’s data from the database and shows it to them when it’s been requested.
Finally your program is finished. But since it’s been built pretty much chaotically, it’s likely a complete mess in terms of elegance of logic and structure. It may consist of lots of similar or even identical parts, created with the help of a magic trick called “Copy and Paste”. And, chances are, it has no clear structure at all, it’s just a big heap of code that somehow works.
This is a completely normal result for someone who’s just learning programming. The most important thing is that you did it. You wrote the code that does what you want. Well, maybe, not exactly what you want. And maybe it’s a little bit buggy, but still — you did it.
Yes, doing this job you probably broke several main principles of quality code, but that’s not your headache yet. You’ll get there later, when you get dissatisfied with the low quality of your code, and it will be pretty soon, as you continue your learning process according to your learning plan.
But now you just have to get used to coding, to start understanding and feeling what it is and how it works.
Done is better than perfect
When you just learn to code, it’s no shame to use suboptimal solutions in terms of code gracefulness. At this point your main objective is to make it work.
If you don’t know how to code something right or where and how to find the correct solution, it’s better to simply get it done at least somehow, even with the “ugly” logic and structure.
Don’t get me wrong: I don’t advocate “sloppy” coding. You must always do your best trying to make your code as good as possible. But it doesn’t mean that if you don’t know how to code something perfectly then you should just give up.
“Done is better than perfect” — this is a motto of most programmers. Facebook developers even created a famous poster with this phrase.
Even the best programmers write bad code from time to time (due to the lack of time or for other reasons). Don’t let perfectionism prevent you from coding, since it’s all about practice. The more you code and learn during this process, the better you get at it.
When the indulgence for bad code ends
Learning programming is like learning guitar or learning to draw. Nobody’s first chord sounds like a masterpiece, and nobody’s first drawing attempt looks like an art gallery exhibit. You try playing or drawing correctly, but at first the result is usually fairly disappointing. With programming it’s no different.
Of course, all this “indulgence for bad code” is nothing but temporary training wheels. You have to (and, I’m sure, you do) aspire to write logically and structurally excellent code. It’s just not something a beginner programmer can do from the day one. Good tutorials, books, and other learning materials will help you pass this first “messy” stage as quickly as possible.
If you keep learning, the more code you write and the more quality code you read, the neater and logically better your code gets. But it takes time. If you don’t stop learning and practicing, you’ll build up your inner sense of “code beauty” in a natural way.
One day you’ll read some book, watch a video tutorial, or analyze someone else’s source code, and suddenly realize that the code you wrote a month ago had embarrassingly convoluted and distorted logic. And even though it worked just fine, now you’d never write something as “ugly” and bad as that piece of code you were so proud of back then.
That’s the moment your “training wheels” are off.