The Most Important Programming Lesson
In the fall of 2012, I walked into my graduate advisor’s office and asked her which computer science class she recommended for me to enroll in. I explained that I was a complete novice in programming. She suggested Introduction to C Programming. After attending a few lectures, I discover that the majority of the students I spoke to in this introductory course had some prior experience in programming. Six weeks and 80 hours of work later, I dropped the course.
Enter spring semester of 2013. I enrolled in an easier computer science course, Introduction to Computer Programming via the Web. I breezed through the first quarter of the course, executing HTML and CSS with ease. Then, we started Javascript (JS). That feeling of constant anxiety and stress from my previous computer science course returned in full fashion. It was too late in the semester to drop the course, so I asked a friend for help.
One afternoon he walked me through my JS code and explained how you could add a few pieces of code here and there to test if your functions were working as intended. What he showed me was a very basic form of debugging, the process of identifying and removing errors from computer hardware or software. Neither he nor I understood the impact this 60-minute tutoring session might have had on my career trajectory. Looking back, it is not hyperbole to say this was the most important lesson I have ever learned in computer science and technology.
If you have never written any code before, imagine your assignment was to draw the perfect bird. Every time you finished the drawing and turned it into the teacher, the teacher would immediately decide if it is correct or incorrect (i.e. binary outcome). The teacher would rip up the paper every time it was not flawless. If you asked the teacher what was wrong, they would explain to you the issue in a foreign language (i.e. computer error language). Eventually, after the teacher has ripped up a few dozen attempts they might accept your drawing and your score goes from wrong to correct. Having read that story, you might not think my next statement is so bold (pun intended).
Nothing in the academic world tests your patience and ability to withstand constant rejection more than learning to write code.
The only thing that provides solace when learning to code is if you understand the fundamentals of debugging. Now, when I draw a bird the teacher will provide feedback if the beak looks good, if the color was appropriate, and if the size makes sense. With all of that feedback, you are much more likely to have your submission be correct without so many rejections.
Whether you write code regularly or not, the fundamentals of debugging can be extremely useful in your life. They apply whenever you need to use any kind of software application or tool. Based on my personal experience in technology, here is a list of my favorite debugging tips and tricks for various software apps and tools.
Using Web Applications
Web applications are computer programs that a user accesses through a web browser. Common web applications are Facebook, Gmail, and Salesforce. If you are having issues using a web application, the issues can often be resolved with one of the following:
- Try a different browser. Various functionality might not work in some browsers
- Turn plug-ins off. Plug-ins can influence web applications in the browser
- Clear the cache in your web browser
- Restart the web application
- Check whether the issue is specific to your version of the application.
- Confirm which security role(s) you have. Certain functionality might not be visible because you don’t have the privileges.
Importing Data Files
Importing data files (e.g. xls, CSV, pdf, etc.) to web applications or within a piece of code is a common task. Common issues tend to be related to the following:
- Check if column name references are correct. They might be case-sensitive.
- Open up the file in a text editor. Excel can manipulate data files.
- Watch out for leading zeros and/or leading zeros being stripped. Check the data type, whether it’s a string, float, or integer.
- Remove any characters that cannot be recognized. Try encoding files in UTF-8, UTF-16, or ASCII.
- Remove leading and trailing whitespace from data
- Check that the file is the correct file type (e.g. xls, csv, pdf, jpg)
- Confirm all values in a column are within the accepted rules. Some columns require certain data types (e.g. string, integer, etc.) and/or they might require the value exists in a controlled list (e.g. only “red” and “blue” are accepted under a colors column)
- Delete any blank rows prior to imports
- Email notifications after import might be sent to your spam folder
All Programming Languages
- Check variable values throughout a script to see how they change
- Print out text within functions to test whether and when they are executed
- Use an Integrated Development Environment (IDE)
- Use lots of comments throughout your code
- Stackoverflow and Google are your friend
- Check which version of the programming language or library you are using. Things can dramatically change between versions.
- Break down lengthy pieces of code into shorter snippets. For example, instead of testing code with a giant JSON string, try testing it out on a small subset. Better yet, create your own mini-JSON string to test things out.
- Write down the computer’s actions step-by-step for each line of code. Python Tutor provides a great visual that walks you through the code execution line by line. The web tool works for Python, Javascript, Java, C, and C++.
- Check whether someone created a package for your specific use case that could make life easier. For example, manipulating data in Pandas is way easier than using native Python.
Programming in Javascript
- Print text or variable values using console.log( ) within functions to test whether and when they are executed in a script
- Clear your browser cache often
- Try a different web browser – plugins can interfere with code
- Upgrade your web browser – older versions might not be supported
Programming in Python
- Object names are case-sensitive (e.g. “Variable_name” is not the same as “variable_name”)
- Pay close attention to error messages. They usually explain which line number the problem started at.
- Check whether it’s a Python v2 vs. Python v3 change
- Python has fantastic documentation
Programming in SQL
- When testing scripts, utilize LIMIT for faster performance
- Check whether there are special syntax rules or limitations due to the SQL database you are using. For example, MySQL uses # to begin a comment line, while PostgreSQL uses — for comments
- Do not name a table or variable one of the reserved words
- Confirm the output table is correct by replicating the intended actions on a subset of data in Excel.
The Most Important Debugging Advice
When it came to learning computer science and programming, I wish my professors emphasized the importance of learning to debug code. I hope my tips and tricks can reduce your anxiety and frustrations when writing code. Oh and I saved the best piece of advice for last.
If you cannot solve a problem, sleep on it and return later.
Seriously. Coming in with less stress and a fresh perspective can help you identify solutions in minutes compared to the hours you spent the day before. This advice has paid dividends for me time and time again. The other day I spent 5 hours on a JS script getting nowhere. I came back to the problem several days later and quickly learned it was a cache issue when I reopened my browser. The only time when this advice does not work is when there is no time. Be careful out there, fellow procrastinators!
~ The Data Generalist™
Data Science Career Advisor
When I get desperate, I start turning off large chunks of the code by commenting them out. Then I proceed to turn the code back on, piece by piece, until I hit the error. It doesn’t tell me what’s wrong, but at least I know where the error must be happening.