Byebug: The One Bug You Actually Want!
Debugging is the process of finding (and correcting) errors in a computer program. When programmers think that the program may have an error, they debug the program. They look for an error, and when they find it, they try to correct it so the program will work correctly. This improves the quality of the product.
When I first began my journey to become a software engineer, I had a very movie-esque interpretation of how programmers debug their programs. I actually imagined scenes from the Matrix where large strings of 0’s and 1’s would appear on the string and you had to somehow input the correct sequence to solve the puzzle.
Let’s go back in history to find the first recorded “computer bug”…
Dr. Grace Murray Hopper (1906–1992) was a naval officer and held titles of both a master’s degree and a Ph.D. in Mathematics from Yale University. Dr. Hopper is widely known as one of the earliest programmers and was credited in developing the Harvard Mark II and III, early generation 16bit computers. If you want to learn more about her amazing story, click here.
As Dr. Hopper was running calculations on the Mark II, she started getting a glitch. She looked inside the machine and found… a bug (a moth if you want to be more specific)!
After many years of programmers examing code line by line, CRT terminals emerged as a solution to make the debugging process quicker than before. These terminals allowed for programs to run certain parts rather than having the entire run from start to finish every time. During these times, debuggers were people. This posed another problem… people are not perfect. They make mistakes.
Then along came command-line debuggers. Programmers far and wide gave a collective sigh of relief. This gave them the power to be even more specific in running their code, giving them access to look directly at the code that they needed.
After using a debugger known as Pry for so long in my previous experience with Ruby, I grew accustomed to throwing a ‘binding.pry’ in my code. In Mod 2 of my coding boot camp, I learned of a new debugger, ByeBug. At first, I didn’t give it any thought.
“Oh, I don’t need another debugger…”
How wrong I was! Byebug is the popularized debugger for Rails and it packs more features than Pry that will make your web application developing that much easier!
If you’re up to date with Rails, Byebug should already be added into your gemfile. Byebug implementation works similarly to Pry, you can drop it in the middle of your code in order to halt the code in order for you to debug. The difference is that in Byebug, you need to start up your Rails app with
and then navigate to the page in your localhost:3000 which has the Byebug in order to activate it in your terminal. From there, Byebug shows you 10 lines of code surrounding where it stopped. Now, you have some commands that you’re able to implement:
- n (next) <number> → goes to the next line of code, going over methods. You can add a <number> in order to jump more lines at a time.
- s (step into) <number> → goes to the next line of code, going into methods. You can add a <number> in order to jump more lines at a time.
- c (continue) → continues executing the program until the next bug or conclusion.
- eval <variable> → allows you to track a variable when you step into methods. Once you are in the method, eval command evaluates the variable and displays the return.
- restart → if you made changes to your methods while in Byebug, you can enter restart to reflect those changes.
- params → return a hash of params sent back from the halted code.
- q (quit) → quits Byebug.
- save → exports Byebug history onto a file in your directory.
- h (help) → displays list of commands.
- irb → executes an IRB session.
For more commands, click here.
Now that you have byebug in your toolkit, you are able to use both Pry and Byebug to help debug your errors!
Note: There is a gem called ‘pry-byebug’ that combines the power of Byebug and the Pry REPL! Find it here.
In conclusion, don’t make the same mistakes as I have. Debuggers. Are. Your. Friend. Don’t be afraid of new techniques, embrace them!