Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDehua Liang
260 PointsHow do I properly debug and test my code?
So I followed the course, and the instructor just kept typing in codes without testing, assuming the code he writes would have no errors or bugs at all. When errors occur, wordpress wouldn't tell you where you did wrong, which line of code didn't work. This makes me so nervous every time I run my code.
I need to test the code little by little before the code gets too big to manage. Any suggestions for debugging along the way? I got several errors after typing so many things at one time.
2 Answers
Sue Dough
35,800 PointsHere is what I wrote in the wiki for my company:
Here are some ways to debug when working on the application.
Wp-Config.php
Edit wp-config.php and set WP_DEBUG to true. It is default to false. It will look like this when debug is set to true.
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*
* For information on other constants that can be used for debugging,
* visit the Codex.
*
* @link https://codex.wordpress.org/Debugging_in_WordPress
*/
define('WP_DEBUG', true);
PHP error log
You can stream the log live by going to the error log location and tailing the log. This will work on Apache setups.
cd /var/log/apache2 && tail -f error.log
PHP Code
The var_dump function displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.
The print_r() displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.
var_dump($expression);
and
print_r($expression);
Benchmarking Speed
// start timer
$time = -microtime(true);
// hello world function
function test_123() {
echo "hello world";
}
// function to check
test_123();
// end timer
$time += microtime(true);
// display benchmark
echo sprintf('%f', $time),PHP_EOL;
Javascript console
You can dump into the console when coding javascript
console.log(var);
Plugins
Deactivate plugins and use process of elimination to figure out which one is causing the issue.
Sue Dough
35,800 PointsAlso look into PHPunit if your serious about testing and use the wp-cli tool to set up php unit easily for your plugins/themes.
Dehua Liang
260 PointsThank you Sue, this is very helpful!
Dehua Liang
260 PointsThanks Simon, I just found that I can enable the debug mode in the wp-config.php to see the error message. I suggest the instructor to add a video about the debug mode as it is important for students to test and debug their code.
Simon Coates
28,694 PointsSimon Coates
28,694 Pointsunder normal circumstances, you want to test early and often. Doing a lot of changes just opens the possibilities for where bugs could creep in. Not super familiar with themes (php?), but you can print stuff to html (or a debug log in a normal environment) to examine what is executing (control flow) and the state of any variables. (really not an expert, so apologize if this is not helpful)