DEV Community

Samuel Lubliner
Samuel Lubliner

Posted on

Tips for programming and debugging code

Rubber Duck Debugging

  • Try talking through the problem as if you're explaining it to someone else.

Pseudocode

  • Write out the steps in comments.

Make the Invisible Visible

  • Print to verify what each step did
puts "My var here: #{my_var}"
Enter fullscreen mode Exit fullscreen mode
  • Make instance variables and embed them in the view.
<%= @my_variable.inspect %>
Enter fullscreen mode Exit fullscreen mode
  • Use the interactive console to display the contents of params, variables, and experiment to find solutions for the error.

  • Read the server log. (Clear with āŒ˜+k)

Try debugger

Add debugger to stop execution and inspect variables
c continue
n execute next line

Restart / Refresh

  • Restart server
  • Hard refresh the page

How to Solve It

  1. Understand the problem
  2. Make a plan
  3. Carry out the plan
  4. Reflect how could it be better

Understand the problem

  • What am I trying to accomplish?
  • Restate the problem.
  • Use a picture or a diagram.
  • Gather information to find a solution.
  • Understand all the words used in stating the problem.
  • Ask questions.

Top comments (0)