I genuinely enjoy coding assignments. In my school, a lot of programming courses have projects that the students must complete. The projects can be rather intimidating to people who aren't experienced in writing long programs. There are also test cases that the project needs to take into account as well. I helped many people get better grades in their coding assignments and projects, and no, I didn't just give out my code. I thought a lot of people can benefit from these tips, so I decided to write a post on it. Enjoy!
Read the specifications carefully
Your assignment will have some sort of specification that describes what you are building. Make sure to read this carefully. If you received it in a PDF, try printing out first and then reading it. You can highlight some important parts in the spec, and physical paper tends to be much easier to read for me. Specs are your instructions, and you want to make sure that you understand them perfectly before you begin.
Draft first, code later
After reading the specs, you would have some idea of what is expected of you. It is very tempting for many students to jump into their IDEs or text editors to start coding. I want to emphasize one thing.
If there's anything you take away from this post, it is this point. You shouldn't jump to coding immediately. Take out a piece of paper and draft your program. If you have an iPad or a digital note-taking device, create a new note and scribble down your plans first.
The reason why you want to do this is that coding isn't really about the code. It's rather an art of system design. A programming project is like an architectural project. You don't want to build a building without a blueprint.
Too many times I saw people mindlessly coding away, only to get stuck in the middle because something breaks. It's like trying to build a skyscraper with wood, only to realize later that there's a limit to how tall you can go.
Here are some things that I consider when planning out my code:
What data structures am I going to use? Which one would do the job the best?
How would function A work? B? C? What algorithm would it use?
What is the desired flow of data?
What input am I accepting and what output am I returning?
Do I wrap the variables and functions in a class? Or is it unnecessary?
How do I deal with memory allocation? Where do I create a new object in the heap?
Do I pass by value or by reference?
Trust me when I say this, you want to plan first before taking action. You will see a noticeable improvement in your development experience.
Write modular code
I mentioned above that coding is mostly the art of system design. I still think this is true, but that doesn't mean that you are excused from writing terrible code. You are only doing yourself a disservice when you write ugly, unreadable, and unmaintainable code.
Style is a personal choice, and I'm not the one to bash people for their taste. However, there are some practices that you should adhere to help you help yourself.
One of these is to write modular code. This means abstracting away as much code as possible. This also connects to the DRY (don't repeat yourself) principle. If you see yourself writing lots of repeated code, you may want to consider abstracting that away into a function that you can reuse later. This way, your code becomes much easier to read, and it becomes much easier to debug.
Make sure you know how to use a debugger
This will save you lots of time. I know a lot of people default to print debugging, and I am guilty of this too. If you are unfamiliar with it, print debugging is a technique where you would insert print statements in areas of the code where you think an error might occur, or would print out variables at certain points in the code. Print debugging is useful when you need to do some quick sanity checks, but they aren't very efficient. You could get away with it for most of your student career, but I'd strongly suggest you learn how to use a debugger. Here are some reasons why:
Your code gets really, really messy, really, really quickly.
If you have a code that outputs to the command line or a file, print debugging will mess your output up, making it time-consuming to read and check. Scrolling through the terminal isn't the best way to use your precious time.
If you want to check items inside a container via print debugging, you would need to loop over the container and print out each item. This is ugly.
Sometimes you need to check memory addresses and stack, and the debugger will do this for you.
Sometimes you just wish you could track a variable during its lifetime. A debugger helps you do this.
If you are using Visual Studio Code, I'd recommend you learn the built-in visual debugger. It's a very easy-to-use debugger that saves you so much time. I saw a noticeable improvement in my ability to fix bugs after learning how to use a debugger.
Explicitly list out possible vectors for errors
Also another debugging tip. When you are writing your code, you want to stop and think about what possible errors might occur. For example, if your code inserts items into an array, you want to acknowledge that a very possible error could be something to do with the index. If you are working with pointers, you always want to make sure that it is deleted, and make sure you aren't trying to double-free the memory in the heap or to use a dangling pointer. I will make a tutorial on pointers soon, so don't worry if you don't understand these just yet.
The point is, if you wait until the very end to check for bugs, you need to scroll through a couple of hundreds of lines to find where it could've occurred. If you list out possible error spots, then you have a much easier time identifying them.
This leads me to another great practice: testing each function individually. You want to make sure that each function works as expected on its own. I see many assignments which ask students to write test cases to test their code. However, these generally test the entire program rather than individual functions and methods. Write unit tests if possible! I know writing tests take a long time, but debugging takes longer in my experience. You can check out my post on testing your code in Go here!
This is a bit of a detour from my usual programming language tutorials, but I wanted to take the time to address some challenges incoming developers and CS students might face. I faced a lot of challenges as well, and I didn't have these resources. I've accrued specific knowledge from doing lots of coding and failing lots. This blog is meant to help people start their developer journey, and I think this post holds to that ideal. For the more experienced developers reading this, please share your two cents on tips and tricks!
Thank you for reading! You can also read this post on Medium.
Top comments (0)