Intro to Programming Tutorial menu

Debugging and Which Language to Learn First

Every programmer makes mistakes. Debugging is the skill of finding and fixing them, and it is just as important as writing code.

What is a Bug?

A bug is a mistake in a program that makes it stop or give the wrong result. Debugging means finding the bug and fixing it. There are three main kinds of errors:

Error typeWhat happensExample
Syntax errorThe program does not start at allA missing bracket or quote
Runtime errorThe program starts, then crashesUsing a variable that does not exist
Logic errorThe program runs but gives a wrong answerUsing + where you meant *

Logic errors are the hardest, because there is no error message. The computer happily gives you the wrong answer.

Reading an Error Message

Here a variable name is spelled wrong:

Example

python
price = 500
print(prise)
Output
Traceback (most recent call last):
  File "shop.py", line 2, in <module>
    print(prise)
          ^^^^^
NameError: name 'prise' is not defined. Did you mean: 'price'?

Read an error message from the bottom. The last line tells you the type of error (NameError) and the reason. The lines above tell you the file and line number. Python even suggests the right name.

JavaScript gives a similar message:

Example

javascript
let price = 500;
console.log(prise);
Output
Uncaught ReferenceError: prise is not defined

Finding a Logic Error

Ayesha wants the average of three marks, but the answer looks too big:

Example

python
a = 60
b = 70
c = 80
average = a + b + c / 3
print(average)
Output
156.66666666666666

The computer did the division first, as in maths: only c was divided by 3. The fix is to add brackets:

Example

python
a = 60
b = 70
c = 80
average = (a + b + c) / 3
print(average)
Output
70.0

A Simple Debugging Method

When something is wrong, follow these steps:

  1. Read the error message carefully, especially the last line and the line number
  2. Print values in the middle of your code to see what each variable really holds
  3. Change one thing at a time, then run the code again
  4. Explain your code out loud, line by line, to a friend or even to a rubber duck
  5. Search the error message online; many people have met the same problem

Printing values is the easiest trick and works in any language:

Example

javascript
let price = 120;
let quantity = "3";
console.log("price:", price, "quantity:", quantity);
console.log(price + quantity);
Output
price: 120 quantity: 3
1203

The result 1203 is a clue. quantity is the text "3", not the number 3, so JavaScript joined them as text instead of adding. Writing let quantity = 3; without quotes fixes it.

Take a short break when you are stuck. Many programmers find the bug within a minute of coming back with fresh eyes.

Which Language Should You Learn First?

Now you have seen two languages side by side. Both are excellent first choices. Pick based on what you want to build:

If you want to...Start with
Build websites and web appsJavaScript (after HTML and CSS)
Automate tasks, work with data or AIPython
Get the gentlest start, with very clean codePython
See your work in a browser straight awayJavaScript
Make Android apps laterPython or JavaScript first, then Kotlin

The most important advice is: pick one and stick with it for a few months. Jumping between languages slows you down. The ideas you learned here (variables, conditions, loops, functions) carry over to every language.

Good Habits for Beginners

  • Write a little code every day, even 20 minutes
  • Type examples yourself instead of copying and pasting
  • Build small projects: a bill calculator, a marks grader, a quiz game
  • Keep your code tidy with clear names and a few helpful comments

Exercise

Exercise

A program runs without any error message but shows the wrong total. What kind of error is this?

Learn Intro to Programming with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp