JavaScript Debugging Tips

Debugging code is a challenging task. When your code doesn’t work, it can be challenge to find the cause of the problem. You may do things like comment out the code or make a small change to see if that fixes the issue. What do you do when these tricks don’t help you find the bug?

JavaScript Debugging Tips
Photo by Mitchell Dunn from Burst

If you are like many JavaScript developers, you start with console.log to find the problems in your code.

When to use console.log()

Console.log() is simple and easy to use debugging tool. You can have it display a message so you know that the code worked. Or you can use variables and expressions to determine if you are getting the right values.

When to use debugger

What do you do if the problem is hard to find? You can use a debugging tool. A debugging tool is a software tool that helps you identify coding errors. You can step through your code line by line to see how it is working. You can stop executing the code at certain points. It can help you to read error messages and handle exceptions.

Your browser has powerful debugging tools include the console.log. Each browser is a bit different on the development tools that it offers. Learn more about the basic functions of your browser’s development tools.

Tips for debugging JavaScript

  1. Start with the console.log(). You can always switch to using other debugging methods if the console.log doesn’t work for you.
  2. Read the error message. Error messages may include helpful details. Like the line number where the error occurred, type of error and a brief description.
  3. Use Breakpoints. Breakpoints allow you to pause your code at a specific line. You can step through your code line by line and see how it is working.
  4. Add the debugger statement in your code. It acts like a breakpoint in your code. When the code reaches this point, it pauses at debugger. If no debugger functionality is available, your browser ignores this statement.
  5. Handle Exceptions. Exceptions are errors that when your code is executing. They can occur when you have syntax, logic or runtime errors. Make sure you are handling exceptions. Exceptions can help you to identify and resolve issues in your code.

Debugging like coding takes practice. Use this tips to help you get better at finding issues in your code. If you still can’t find it, take a break. Walking away from the problem sometime help. So can talking to others also helps.

Read more on debugging

7 Tips for Commenting Your Code

Your code doesn’t need to be commented. Or does it? Clear code needs to be easy to read. You may not be the only one who has to maintain this code. Write comments so the future person who updates this code can understand why you did things. Remember that person may be you.

7 Tips for Commenting Your Code
Image by Gerd Altmann from Pixabay

Use these tips for commenting your code for the better.

1. Comments should be meaningful

They shouldn’t duplicate the code. Comments should compliment your code. You write to explain why the code works this way.

2. Add comments when fixing bugs

It helps you to understand what changed and why. Include references to issue numbers to provide further information. Anything else that you used to help solve the problem.

Everyone codes code they found on the internet. It helps to add links in the comments so you remember where you got the code from. Add who wrote it and what problem it fixes.

4. Sometimes your code isn’t finished

Use comments to remind everyone that there are limitations to this piece of code. Add TODO to remember what you need to finish.

“You should first strive to make your code as simple as possible to understand and without relying on comments as a crutch. Only at the point where code cannot be made easier to understand should you begin to add comments.”

-Jeff Atwood, Code tells you how, comments tell you why

5. Comments should explain why you did something

If your comments are confusing, remove the comment. Keep comments that are clear and easy to understand.

6. Keep your comments brief

Comments can take time to read. You want to make them as short as possible.

7. Remember to update your comments

As you change your code, your comments may need updating to reflect changes.

Comments can’t fix or excuse bad code. They should compliment it. Commenting your code should help you understand why you chose to do something.

For getting better at writing comments, read Best practices for writing code comments.

Anything else that you used to help solve the problem.

How to Deal With Developer Burnout

Everyone can feel tired, bored or exhausted from doing work. Why? Too busy. Your tasks are repetitive and unchallenging. Or you deal with people who drain you. You need to focus on things that you can control to help deal with developer burnout.

How to deal with burnout

Here’s a list of things to try:

  1. Take a day off and do nothing. Relax, sleep or go somewhere different.
  2. Change your sleep routine. It may need a bit of tweaking. You may need to change your nightly routine.
  3. Break a habit or pattern. Take a different route to work. Eat at a new place for lunch.
  4. Drink water. Stay hydrated during the day.
  5. Spend time in nature. Go for a walk in a garden or a park.
  6. Take regular breaks during the day. Get up walk around or stretch.
  7. Do something creative. Write. Photograph. Or make something not related to work.
  8. Move your body. Talk a walk. Do yoga or pilates. Anything that gets you to move.
  9. Listen to a new podcast. If you listen to programming podcast, choose one on a different topic. Try out something you don’t normally listen to.
  10. Learn something new. Read a book or watch a video. Try a new programming language or tool.
  11. Say No to additional tasks at work. See if you can arrange to do them at a later date.
  12. Change your coding environment. Use a different text editor. Or go outside and code.

You want to try these things to avoid developer burnout and before deciding to quit your job. Small changes can help. If they don’t, then you can find a new job later.

Creating a simple weather app with Open Weather API

How do you get the weather? You could use an app or create your own. First, you need a way to get weather data. You can use an Application Programming Interface to get the data that you need.

Creating a simple weather app with Open Weather API

Where can you find APIs to use in your projects? Try ProgrammableWeb. It is an API directory that lets you find the right API for your project. I found a weather API called Open Weather. Open Weather allows you to get the current weather data, hourly or daily forecast and more.

With Open Weather, I created a simple weather app. It gets the current temperature, daily forecast (highs and lows) and weather conditions. Then, it tells you if the weather was right for riding a bike.

Getting Started

Before writing any code, I looked at Open Weather’s documentation. It explains how to use their API. They include examples using different programming languages like JavaScript. These examples are helpful were helpful to learn what I could do with the API.

On their examples’ page, I found Weather.js. Weather.js fetches data from Open Weather for you. It makes it easy to get weather information from Open Weather.

Building the App

Before building the app, I researched other weather apps to get an idea of what I wanted mine to look like. Then, I sketched out an idea on paper.

I chose to use HTML, CSS and JavaScript. Since I am familiar with Bootstrap, I used it as well. I built my prototype with Bootstrap’s starter template. Then, I wrote my own JavaScript file to fetch data from Open Weather using Weather.js.

Open Weather has weather icons. Weather.js doesn’t use those icons. I looked at the JavaScript and wrote code to get the icons.


Weather.Current.prototype.icon = function () {
  return this.data.list[0].weather[0].icon;
}

Now, my app shows the current and forecast temperatures, weather icon and conditions.

Bike Weather App Screenshot

What to do differently

Open Weather returns weather information for a specific location. Instead of hardcoding the location, I would use the location of the browser. Right now, I used Bootstrap for the UI. I would use a different tool for handling layout like FlexBox or CSS Grid.

Write Better Documentation for Your Code

Do you write documentation? Most developers love to write code. They write code that solves problems. It helps people to get something done. If they write documentation, it is quickly written. A task to be completed so you can get back to writing code.

Write Better Documentation For Your Code
Image by Christine Sponchia from Pixabay

How do you choose a new framework? You look at the features to see if it does what you want. Then, you read the documentation. If it is hard to read or non-existent, you’ll get frustrated and choose a different solution.

If you want people to use your code, you need to write great content.

How do you write better documentation?

  • Decide who you are writing it for
  • Decide what you are going to include
  • Make it easy to use
  • Keep it up to date
  • Read great documentation

Decide who you are writing for

You need to know your audience. Are they developers, clients or customers? Developers want to know how to use your code. Or what can they do with it. Your clients may want step by step instructions on how to use it.

What are you going to include

Decide on what you want them to know. Create a list of questions or items that you need to cover. A technical audience may want to read tutorials, how-to-guides and/or references. Beginners learn better with simple step by step instructions. For everyone, you may want to include videos as well.

Make it easy to use

No matter what you are writing, decide how you are going to organize it. If you are writing a tutorial, you may want to start by explaining what you are planning to talk about. Use formatting styles like headings, bulleted or numbered lists and bold or italics. Formatting styles help guide the reader through your documents.

Keep it up to date

Developers make improvements and changes to their code. When they do, the documentation needs to change as well. Plan time to update it. Apps and other projects fail because the content isn’t kept up to date.

Read great documentation

You can learn how to write better by reading. These style guides show you how to write great developer content.

More Resources