FIXING BUGS AND REFACTORING CODE
Bugs in software engineering are errors, flaws, failures or faults in computer programs or systems that lead to the production of incorrect or unexpected results, or to behave in unintended ways. Debugging is the process of finding bugs/errors in software programs.
There are three types of errors: syntax, run-time and logical errors. The first two are reproduced by the programming compiler or framework for the developer, the last error is one caused by human logic.
PROCESS, TOOLS AND TECHNIQUES USED IN DEBBUGING DURING SOFTWARE DEVELOPMENT
Process
In debugging, a few steps need to be taken to effectively find solutions to an existing bug.
- First, the bug is identified and reported to the software engineer in charge who in turn verifies that the bug is true and exists.
- The bug is then analysed and the reason for the bug is resolved by making the required changes to the system.
- The final result is tested and a check is put in place to prevent future occurrence.
Tools & Techniques
1- Reproduce The Bug By Testing With Mocha, Chai or Sinon
Writing tests to reproduce a bug will help you focus on that one bug and avoid spending much time trying to reproduce the exact point at which the bug occurs.
Sinon: SinonJS provides standalone test spies, stubs and mocks. This mechanism is used to create spies, stubs and mocks for unit tests.
Mocha: is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple. Mocha tests run serially, allowing for flexible and accurate reporting while mapping uncaught exceptions to the correct test cases.
Chai: is a behaviour-driven/test-driven development assertion library for node and the browser that can be delightfully paired with any javascript testing framework.
2- Chrome Console
The Chrome console provides an interface for logging errors from the development environment to the console. In javascript, an error might be solved by “console.log”ing so as to have a better view of the root cause.
3. Debugger
The debugger helps us stop at a specific point and pause/play the code, add breakpoints at intervals and watch the code play to see where the error comes from. Debuggers can also be used in IDEs like visual studio code and Atom.
4. Understand Stack Traces
In software engineering, some bugs have stack traces while others don’t. It takes constant practice and skill to be able to debug errors from a corresponding stack trace. Note that the bug that broke the code is usually hidden in layers and layers of file traces and can be found when working patiently and steadily down the trace.
Consider the stack trace from the bug below:
Sample stack trace
To debug this error, we can start tracing each error from the file structure at each line ‘at’, down to the line that shows our own piece of code in the ‘server/app.js’ which specifies the error at line 3. If we take a look at line 3 in the app.js:
We see how a typo in the name of an npm package ‘morgan’ has caused the bug. So when the typo is corrected, the bug disappears.
How To Prevent Bugs
Test-Driven Development
One of the best ways to prevent bugs is to write tests. I will ensure I write tests for every new feature built on any system, run the tests and ensure they pass before they are coupled into the system. This will give an assurance that the application is working.
Continuous Integration Tools
Continuous integration tools serve as a watchdog for failing features and piece of code. It is so important for software engineering teams to be sure their code is working and all tests are passing. A faulty code will trigger off notification messages to the team involved in the building of the software. Examples of CI tools are Travis CI and Hound CI.
REFACTORING CODE
Code refactoring is important for preserving the code base and protecting against bugs. Recently I had to refactor a piece of my code to follow ‘DRY’ practice (Don’t Repeat Yourself). What happened?
A certain query function for one comment-controller object in my simulations project had to be repeated in four methods. The ‘payload’ is a result gotten from querying the database for certain conditions like in the figure below. At some point, the controller was so lengthy and full of repetitions.
Line 91: Find all and count all function for retrieving comments from the database.
What did I do? I created a helper function like in the figure below:
Helper function to query the database for comments
And so for every instance of the function, I imported the helper, then called the function passing it the required variable which in turn returns the desired results.
This helper function reduced the lines of code I wrote, cleaned up my controller and made it more efficient with fewer repetitions.
REFERENCES
https://www.geeksforgeeks.org/software-engineering-debugging/