Open a brand new text in a blank text editor without a line of code. It is a project full of infinite possibilities and hope. However, after thousands of lines of code are written, the entire project is overwhelmed by bugs, let alone new features... this may be the biggest blow to programmers, pouring a pot of cold water on the enthusiasm. In fact, the best software programmers certainly know how to discover and fix these bugs and reduce the probability of bug occurrence through the best method of software engineering at the beginning of programming.
Almost no programmer can write code with no bugs, but the solution is always much more difficult. Multi-practice and determination are the key to success, so that you can write clean code and ensure the reliability of the software system.
Let's take a look at these toolboxes that can suppress bugs.
1. Output statement
The primary tool for code debugging is to insert reliable and authentic output statements. When the number of output statements is large and difficult to manage, the record system is used properly in the output statements. This is an equivalent solution. Many programming languages are equipped with ready-made class libraries, such as record libraries built in Python.
The output statement is the fastest, simplest, and most direct way for programmers to check data values and variable types. Efficient output statements help programmers track data streams through a piece of code and quickly identify the bug source. Although there are many advanced debugging tools, if you want to debug a piece of code, this common method of output statements should be the first method that programmers consider.
2. Debugger
The source code debugger uses logical reasoning in the output language method. This allows programmers to execute code one by one row and monitor every action from the variable value to the entire state of the underlying virtual machine. In addition, most programming languages have multiple debuggers that provide different functions, including graphical interfaces, breakpoint settings for terminating programs, and implementation of arbitrary code in the execution environment.
In many cases, the debugger can be said to be useless, but if it is used properly, the debugger is definitely an efficient tool. For more debugger functions, see Python Debugger: pdb.
3. Bug Tracking System
In some major software projects, it is necessary to use the bug tracking system. If the bug tracker is not used, the most typical situation is that the programmer has to sort out previous emails or chat records to find bugs. Even worse, the programmer simply does not remember other things, there is only one bug document in the impression. Once this happens, the bug will inevitably be filled with the whole code programming. More seriously, it is difficult to identify these bugs and determine their locations.
A simple text file can be used as the initial bug Tracking System in the project. As the code library continues to grow, it does not take too long for a bug to derive a text file. There are a lot of commercial and open-source bug tracking software solutions that can be considered, the first thing to be clear about which bug tracking software to choose is to ensure that in programming projects, non-programmers can quickly use this bug tracking system.
4. Linter
In some programming languages, Linter can perform static code analysis to identify the problem area before writing and running the Code. In some other programming languages, linter tools are helpful for syntax checks and enhanced styles. Open a Linter program in the editor during programming, or pass the Code through Linter before code writing and running. This helps programmers to discover and correct more errors before using the software. Therefore, using Linter can help you find the source of bugs caused by syntax errors, typing errors, or data type errors while saving valuable time.
If you want to know what kind of Linter is best for you, check out the Python Linter tool: Pyflakes.
5. Version Control
Version control systems should not be ignored in any major software engineering project. For example, version control such as Git, Mercurial, and SVN allows different code library versions to be separated on different bases.
Different control versions can be merged. Therefore, multiple programmers can run the same code library at the same time. Version Control also plays an important role in code troubleshooting, allowing programmers to roll back and modify code of earlier versions, and try to fix errors in the code library before they occur.
6. Modularization
Code without architecture is the main source of bug fixing. As long as the code is easy to understand and theoretically works, it is not difficult for programmers to find and quickly fix bugs. On the other hand, the more important the Code has an error, the more likely it will be, the more difficult it is to find this error.
The so-called code modularization often needs to be considered when designing software components. Code modularization can help programmers better understand the software system in two ways. First, modularization can create a certain level of abstraction and imagine the system model without fully understanding all the details. For example, if a programmer is building a business system, he may consider the credit card processing module, and then observe the connection between this module and other code. He does not need to consider all the details of the credit card processing module. Second, the detailed description of the module. This detailed description will not be confused with other modules, just as each card has only one card number.
7. Automated Testing
Unit testing and other types of automated testing are closely related to modularization, which can be said to be a complementary component. Automated testing is a piece of code that uses special input values to run the software, in order to detect whether the program runs properly as expected.
Unit Testing is mainly used to detect the functionality of a single function. However, function testing is used to check the performance of special programs and to check the overall part of the software system in conjunction with unit testing. There are many testing frameworks that can be used to write test programs, and most of the popular testing frameworks are derived from the JUnit class library compiled by Kent Bent, kent Bent is one of the earliest supporters of "test-driven development methods. The Python standard library includes a Python version of JUnit, called PyUnit or unittest.
8. Teddy Bear method (debugging for Rubber Duck)
In the software programming field, we have to mention the legends Brain Kernighan and Rob Pike. The teddy bear debugging method originated from a university computer center. Here, when a student encounters a mysterious bug, he can explain the problem to the teddy bear on the table before asking for help from the teacher or TA. So sometimes you can solve the problem by chatting with the bear. This debugging method is so useful that it is popular in the software engineering industry, like printing statements, no matter how complicated the tools are, the output statement method is still very popular today.
A similar method to the teddy bear debugging method is called the Rubber Duck debugging method. When you explain to the Rubber Duck that remains silent, you will find that your thoughts, opinions, ideas, and actual code are deviated, so you will find bugs in the code. Once a problem is fully described, the solution is obvious. Do you think this method is too "stupid" or "mentally retarded? Yes, it seems that the brain of the person who will do this seems to be a bit faulty. However, what I want to tell you is that this method is indeed valid. This is the prototype of "Code Review!
9. write code comments
The function of annotation is to explain the purpose of writing the code at a more understandable level, and write as much as possible: What is each line of code and how to complete it, these questions should be easily answered after the code is read through. In addition, obtaining a proper name for each function and variable also helps simplify the code implementation process. Enter comments in the blank space below the code line to answer why special implementation functions are required, or how a piece of code interacts with the rest of the program.
Writing detailed comments can be said to be a step-by-step reliable verification step in software engineering, even in code without bugs. In this way, even if a bug occurs, you don't have to worry about it. comments will help you save the troubleshooting time for several hours.
10. Write documents
Code comments are written by programmers in a simple way and in their personal opinion. Software documents are used to describe the functionality of the software system. You can also see these software documents. Depending on the software type, the document can be used to detail the program interface, Gui, or workflow.
Another benefit of writing a document is to demonstrate your understanding of the software system, and point out that the software system is incomplete or may be the source of a bug.
Original article: Onextrapixel
Link: http://www.cnblogs.com/oooweb/p/10-useful-tips-for-debugging.html