Mark Stewart July 14 2020
Print something at the beginning or end of a method or function to know if it’s being called. Print what your variables are. Print what your variables’ classes or types are. There is no such thing as too much information about what your code is doing.
>>> a = [1,2,3,4] >>> print(a) [1, 2, 3, 4] >>> print(a.__class__) <class 'list'>
Don’t forget to label what you’re print-ing to make sure you know which variables are which.
>>> print("a.__class__:", a.__class__) a.__class__: <class 'list'>
Is clearer than this:
>>> print(a.__class__) <class 'list'>
This labeling is indispensable when you’re printing multiple variables in a file or project. It’s very easy to forget which variable in your output is which, especially if many of them are similar.
If you’re writing a web app, printing will generally be visible in your server logs. You can also show variables’ values directly on your web page.
Check out this blog post about puts debugging in Ruby.
Google your problems, google your errors, google things you’re not sure how to use, google for whether or not there are easier ways to do what you’re doing.
Always try to take a second to step back and ask yourself: do you think what you’re trying to do seems like something a lot of other programmers would probably need to do sometimes? If so, there are almost certainly written solutions for it, and there’s a good chance, if it’s a simple operation, that there’s already a simple built-in function or method for it. One of my favorite discoveries of built-in functionality ever was finding out that Ruby on Rails provides the
distance_of_time_in_words_to_now method out of the box. If Ruby and Rails can ship with solutions to problems that specific, there’s a good shot your tools may have a solution to any simple problems you’ve come across, too.
Particularly if you’re a relatively new programmer, it will be pretty rare for you to come across a problem that hasn’t already been encountered by literally thousands of programmers before you. Don’t re-invent the wheel if you don’t have to.
Continue your coding journey with Kickstart Coding's unique online program.
Apply now and "audit" a video class for free!
Kickstart Fundamentals - 1.1 - HTML