A lot of the time, inexperienced or overly cautious developers will tinker, theorise and worry about fixes and finding solutions to bugs or challenges during development. Sometimes it is faster and easier to just begin deliberately breaking code.
This is not counter-productive and can be a powerful development practice to more efficiently work through problems. By deliberately breaking a function or a page early on in the code, and slowly re-enabling chunks can make it easier to determine where issues lie. A lot of developers seem to insist on working backwards from where they are, but it can be a lot faster and easier to return to the start. It is easier to see an issue as it begins to develop or a as a bug is introduced, rather than starting with a problem and trying to define and resolve every symptom by slowly working backwards.
die(); or an
exit; and an
echo or a
console.log();. Displaying the values within variables, objects, arrays etc is an essential part of applying this methodology of breaking early and breaking often effectively. Displaying the values of objects and variables will sometimes even reveal that the expected data type is entirely wrong, and an array is returned somewhere when a string is required. Seeing the value of the data you are working with and comparing to what you were expecting will often reveal the root of many issues in web development, but a lot of developers still seem to be reluctant to employ these simple methods.
This can help when working through issues on a single page, or within an MVC framework that may be drawing on any number of modules, packages or functions. Programming often involves keeping a mental image of multiple intersecting pieces of code, services or database content that all influence one another. This delicate understanding and balance is why programming requires so much concentration and is the reason that interruptions can be so costly. While maintaining this web of knowledge about an active system is important, working through one route from start to finish and breaking the code early and often can help to narrow down and understand a particular issue.
This is, essentially, how to debug code. It’s not about diving in and starting to change things before they are understood properly, but actually encourages understanding of code and what is happening by starting at the base and understanding the output of key stages by stopping, seeing what’s going on and understanding each step individually. This helps to find bugs by not overlooking or glossing over bits of code or functionality by breaking the code down into smaller, manageable chunks that can be tested individually. Often this will allow a developer to clear and mark sections of code as working, and eventually reach the step or the point in execution that an output is wrong or a result is in some way different from that which is expected.