Self-learning is difficult and frankly, quite lonely. Here’s how you can make it easier.
3 systems to make self-learning easier, Mentors to follow on Twitter and Cool Project Ideas for learning
Simple rules for better programming that are generally neglected by newcomers
“The purpose of style is to make the code easy to read for yourself and others, and good style is crucial to good programming.”
— Brian W. Kerninghan, “The Practice of Programming”
When you’re teaching yourself, it’s easy to pick up bad habits or styles because you don’t have someone checking your work. You don’t have many reference points for clean code because not many books/courses directly address these.
In this article, I want to give you some actionable advice on writing clean, beautiful code.
“Should I be descriptive, or can I be lazy?”
That’s the conundrum we all face when creating a new variable.
I find Brian W. Kernighan’s advice (in the above mentioned book) really helpful here:
n
may be sufficient, npoints
is fine, and numberofPoints
is overkilli
and j
for loop indices, p
and q
for pointers, and s
and t
for strings is so frequent that there is little profit and perhaps some loss in longer names.Anyway, we have it a lot better than Brian did in the ‘90s when he wrote this book —like IDEs that can autocomplete our variable names.
So can you even justify being lazy now?
It’s easy to understand the syntax of writing functions in your favorite language. But it takes practice and some sense of design to learn when to break the code into functions.
One goal is to design functions such that they can be reused when extending your program to new cases.
What more? Making such design choices is what makes programming fun.
Here are three heuristics from Bob Martin’s book “Clean Code” that’ll guide you while making such choices:
When you first write a function it’ll probably come out long and complicated and not follow any of the above rules. And that’s OK. You can refine and reformat your code later. I don’t think anyone could start with writing functions that follow all the rules mentioned above.
Remember they’re function-building goals that you need to strive towards. Don’t let them paralyse you.
Your code should always include comments that serve as documentation for the functions and modules.
Every function definition should be proceeded by a comment that includes:
Apart from that, you should include comments to describe less-comprehensible parts of your code.
Just keep in mind that:
Why?
Because a consistent coding style is a good coding style.
It makes it easy for someone (including you, yourself) to dive into your code and read it in the future.
Remember:
++i
or i++
.And learn to own it.
You’re likely to spend a majority of your coding time banging your head over broken code.
In those moments, you’ll inevitably try to shift the blame to your IDE, compiler, environment, or your machine — especially in the beginning of your journey as a programmer.
You must learn to control that frustration and remember: It’s not the computer, it’s you.
Only when you do that will you be able to diagnose and get to the heart of the problem (even if it’s some problem with the machine).
How to debug your code:
2. Add print statements
You know ... these print statements.
Although adding such print statements isn’t the correct way to debug, I find them incredibly effective at times. Especially, when I’m working with a text editor like VIM and not on a full-fledged IDE that has a debugger (or when you’re too lazy to learn how to use a debugger).
But I have to say, once you learn how to use an IDE debugger, there’s no going back.
3. Use an IDE debugger
A debugger can be so incredibly helpful. Yet, I neglected learning that part of my IDE for a stupidly long time. I’ll suggest you learn how to use the debugger in your favourite IDE, ASAP. Trust me, it’s totally worth the pain.
Personal advice: I’m using IDE debugger because Python provides a debugger in the standard library module — pdb —
and I won't recommend using it.
In the end, remember that the principles of a programming style are based on common sense guided by experience, not on arbitrary rules and prescriptions.
Here are 4 guiding principles that I follow to keep my code clean:
These should help you when you forget any rules or run into new situations in the real world that aren’t covered by these concise rules.
Here are the 2 books that I use to inform my own intuitions about good coding style:
But mind you, only reading about such practices is not enough.
These are difficult-to-grab intuitions, and the best way to develop your own intuitions is through practice.
I have written before that the problem with the teaching-through-small-programs approach is that it doesn’t
In summary, small programs fail to teach the core design choices that one learns to make when creating larger programs working on your side-projects.
So the way you develop these intuitions is by working on projects.
If you’re stuck at the stage of finding ideas for an interesting project, I’ve written an article to help you out:
And if the idea of building your own project still seems too daunting, I wrote a guide to walk you through one of the projects in the above article:
I am passionate about teaching. I will try to keep producing such articles that address the problems that I face myself, with empathy. Subscribe to the Build To Learn newsletter to get updates when I do such guides and articles.
You can reach out to me via Twitter or LinkedIn or the plain old email — nityeshagarwal[at]gmail[dot]com
.
Originally published on the Build To Learn blog.