10 tips for debugging and troubleshooting

Source: Internet
Author: User
Keywords Can debugger programmer write
Tags added aliyun automated testing code control credit card data debugger

Open a brand new text in a blank http://www.aliyun.com/zixun/aggregation/18444.html "> text editor without a single line of code that appears in front of a project full of possibilities and hopes However, after thousands of lines of code have been written, the entire project is overwhelmed by bugs, let alone added new features ... This is probably the biggest hit for programmers, with full enthusiasm In fact, the best software programmers of course know how to find and fix these bugs, the best way to reduce the probability of bugs through software engineering at the beginning of programming.

Few programmers can write code that does not have a bug, but the solution is always a lot more difficult. More practical and resolute determination is the key to success, so that we can write clean code to ensure the reliability of the software system.

Let's take a look at these toolkits that can suppress bugs.

1. Output statement

The first tool in code debugging is to insert a reliable, real output statement. When the number of output statements is large and not easy to manage, proper use of the recording system in the output statement is an equally good solution. Many programming languages ​​come with ready-made libraries, such as those built in Python.

The output statement is the fastest, easiest and most straightforward way a programmer can check data values ​​and types of variables. Efficient output statements can help programmers use a piece of code to track the data flow, and quickly identify the source of the bug. Although there are many advanced debugging tools, but if you want to debug a piece of code, the normal output statement should be the way the programmer first considered.

Debugger

The source code debugger uses logical reasoning in the output language method. This allows programmers to step through the code line by line while monitoring the move from variable values ​​to the underlying state of the underlying virtual machine. In addition, most programming languages ​​have multiple debuggers that provide different functions, including graphical interfaces, termination of program breakpoints, and arbitrary code execution within the execution environment.

In many cases, the debugger can be superfluous, but if properly used, the debugger is definitely an efficient tool. More debugger function see Python debugger: pdb.

3. Bug tracking system

In some of the more significant software projects, the use of bug tracking system is necessary. Without the use of bug trackers, the most typical situation is that programmers have to sort past messages or chat logs to find bugs, and even worse, programmers simply do not remember anything else, with only a bit of a bug in their images. Once this happens, the bug will inevitably be filled with the entire code programming, even worse, to identify these bugs and determine their location is difficult.

A simple text file in the project can be used as the initial bug tracking system. As the code base continues to grow, it does not take too long for a bug to produce a text file. There are many commercial and open source bug tracking software solutions are considered, the choice of which bug tracking software The first clear part is to ensure that programming projects, those non-programmers to quickly use the bug tracking system.

4. Linter

In some programming languages, Linter can perform static analysis of code to identify problem areas before the code is written and run; in some other programming languages, Linter tools are helpful for syntax checking and enhancement styles. Opening a Linter program in the editor while programming, or passing code through Linter before the code is written and run, can help programmers find and correct more errors before using the software. As a result, using Linter can help you spot precious bugs caused by incorrect syntax, typographical errors, or data types while saving valuable time.

Want to know what kind of Linter is best for your use, look at the Python Linter tool: Pyflakes.

Version control

Any major software project should not ignore the use of version control system. For example, versioning like Git, Mercurial, and SVN allows different codebase versions to be separated on a different basis.

Different control versions can be merged together, so multiple programmers can run the same codebase at the same time. Version control also plays a decisive role in debugging code, allowing programmers to roll back and modify older versions of the code, fixing errors in the codebase as much as possible before the error occurs.

Modular

Lack of architectural code is a major source of hard-to-fix bugs. As long as the code is easy to understand and works well in theory, finding and fixing a bug quickly is not uncommon for programmers. On the other hand, the more important the code is, the more likely it is to make a mistake and the more likely it is to find the error.

Oftentimes the component of software design needs to be considered a so-called code modularity, which helps programmers better understand the software system in two ways. First, modularity creates a level of abstraction that allows the system's model to be imagined without fully understanding all the details. For example, the programmer is building a business system that might consider the credit card processing module and then look at what the module has to do with the rest of the code, regardless of the details of the credit card processing module. Second, a detailed description of the module, this detailed description is not confused with the contents of other modules, as each card is only one card number is the same.

7. Automated testing

Unit testing and other types of automated testing are closely linked with the modular, can be said to be mutually reinforcing. Automated testing is a piece of code that uses special input values ​​to run the software to see if the program is running as expected.

Unit testing is mainly used to test the functionality of a single function, however, functional testing is used to examine special program performance and to examine unit-of-software as a whole in combination with unit testing. There are many test frameworks for writing test programs, and most of the popular test frameworks are derived from the JUnit library written by Kent Bent, one of the earliest backers of the "test-driven development approach." The Python Standard Class Library includes a Python version of JUnit called the unit testing framework for PyUnit or unittest.

8. teddy bear method (rubber duck debugging)

In the world of software programming, one has to mention the legends Brain Kernighan and Rob Pike. The Tweed debugging method originates from a university computer center where students can first explain the problem to mysterious bugs Teddy bear on the table to listen, and then to the teacher or teaching assistant for help. So, sometimes chatting with a bear can solve the problem. This debugging method is really useful, so swept the entire software engineering industry, just like print statements on this side, no matter how complex those tools surging, the output statement this method is still very popular today.

A similar method to Teddy Bear debugging is called rubber duck debugging, and as you explain to this rubber duck that's always silent, you find that your ideas, ideas, ideas, and actual code deviate So, you will find the bug in the code. Once a question has been fully described its details, then the solution is obvious. Do you think this method is too "stupid" or too "mentally handicapped"? Yes, it seems that people who do this seem to be a bit ill. However, I want to tell you that this method is indeed effective. Because, this is the prototype of "Code Review"!

9. Write code comments

The function of annotations is to explain the purpose of writing code at a more understandable level. Write as much as you can: how each line of code is done and how it should be done, all of which should be easy to find out after reading the code. In addition, giving reasonable names to various functions and variables can also help simplify the code implementation process. Write comments in the space below the line of code to answer why you want to use special implementation features, or how a piece of code interacts with the rest of the program, and so on.

Writing detailed comments can be said to be a step-by-step test step in software engineering, even in bug-free code. Do not worry, even if the bug is there, annotation will save you hours of troubleshooting time.

10. Write the document

Code comments are written by programmers in a straightforward manner and from a personal point of view, and writing software documents is used to describe the functionality of a software system, and users can also see these software documents. Depending on the type of software, the documentation can be used to detail the program interface, graphical interface, or workflow.

Another benefit of writing documentation is that you can demonstrate your understanding of the software system, point out the software system is not perfect or may be part of the source of the bug.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.