当前位置:网站首页>What are some tricks that novice programmers don't know?

What are some tricks that novice programmers don't know?

2022-06-25 16:04:00 Seven step programming

hello, Hello everyone , I am a Jackpop, Master graduated from Harbin Institute of technology , I worked in Huawei 、 Ali and other big factories work , If you are interested in further education 、 employment 、 There are doubts about technology improvement , Might as well make a friend :

I am a Jackpop, Let's make a friend !

Whether you are a beginner , He is also an expert in software development , There is no way to avoid errors in the code .

There are errors in the code that we all develop , because , No one can thoroughly understand what happens in the coding process .

therefore , It takes time to develop the project , stay Debug The process also requires a lot of energy .

We can only study ourselves , Study our tools , Study our bug, Find a solution , Help us more efficiently reduce the problems in the development process bug.

How to deal with it bug, I think it can be divided into 3 Stages :

  • Pre commissioning : Try to avoid in the development process bug appear
  • debugging : Find out bug、 distinguish bug、 Repair bug、 remove bug
  • After commissioning : The estimate is unknown 、 Does not appear in the bug

below , Let's go through each stage in detail .

Pre commissioning

Like computer scientists Edsger W. Dijkstra That's what it says :

“ If debugging is the process of eliminating errors , Then programming must be the process of introducing errors .”

If we programmatically introduce bug, That means we need to guide ourselves to reduce the introduction of bug The number of . I call this process of guiding myself “ Pre commissioning ”(Prebugging).

I am here Google On the search "Debug" The definition of , The definition I saw in the Oxford Dictionary made me think .

“Debug The process of identifying and eliminating errors from computer hardware or software .”

This definition caused me to think , Because I believe that many software developers take the initiative to debug . They improve their tools and themselves , In order to reduce the bug Number .

We can do this in some ways .

  • Write program specifications
  • Know the tools you often use
  • Learn to type accurately
  • Familiarize yourself with error messages and possible solutions
  • Always make sure you have stable settings for most of the tools you use

Although this definition is acceptable for debugging , But it downplays the need for software developers to reduce bug Everything else that comes into being . So let's look at these things now .

Know the tools you often use

It's important to know all the tools you often use , Because it helps you reduce the amount of bug.

There is no way to avoid bug The birth of , But if you have a sound basic knowledge of the tools you use , You can avoid some bug.

for example , quite a lot JavaScript The user can't remember what a return is . And some people can't remember the difference between array methods .

If you're not JavaScript user , Just pick a built-in method or function from the language you use , Then ask yourself :

What parameters are required for this method ? What will this method return ? What happens if an invalid parameter is supplied ?

Ask yourself these questions often , You can learn a lot .

This is how to keep yourself up to date with the basics of the tools you often use , Especially if you don't have much time to actively read .

Plan before coding

Programming seems like a trial and error job , You need to keep trying , Until it's right .

Many junior software developers do not really understand the program they are developing , Some of them search the Internet for error messages before , I didn't try to understand the error message .

Now? , Everyone seems to think that programming is always related to “ Code , Code , Code , Search for , debugging ” of . ‌‌

But it is necessary to really understand what you are doing , So that you can quickly list :

  • What we expect as inputs and the structure and characteristics of these inputs
  • How do we expect to handle these inputs
  • What we ultimately expect to return
  • If the expected input is not given , What needs to be done

In short , Input to a function or program , Planning processes and outputs can not only help you reduce errors , It can also help you write effective test cases .

Familiarize yourself with common error messages

If you are already familiar with a mistake or bug, It's often easy to fix .

therefore , Take time to study some common mistakes , It is important to learn how to fix these mistakes . Now let's talk about some common mistakes .

  1. Grammar mistakes

Every programming language has its own rules , Developers are responsible for violating these rules .

Programming languages are very strict with rules , As long as these rules are violated , They throw errors .

for example , Imagine , You omit the parentheses of a function or method :

function {}

It throws an error .

Be familiar with the error messages of grammatical errors , And how to fix it , It will give you more advantages in debugging .

I personally noticed , Most grammatical errors always mention some key words , Help you find the wrong part of the code .

let school = { 
name: "Harvard", 
location: "Heaven On Earth", admit: function() { return "weeew! You are admitted" } 
} 
console.log(school.names); // undefined

Back to "undefined" Tell us , The object or property we want to access is not available . If we are keenly aware of error messages , We can find out the problem .

The more code you write , The more you can avoid grammar mistakes . You can also simply use the code editor 、 Static inspection tool , Can highlight grammar errors . Using these tools can help you a lot .

  1. Logic / Semantic error

Logical errors are a thorny problem , Because there is nothing wrong with them looking at the mountains – But you still don't get the expected results .

for example , A simple way to confirm this error is to check the following code in the browser console .

prompt("enter number") + 3;

You may want to output a number , But it will return a string .

In short , You will not get the desired results .

Plan before coding , Understand the basics of the programming language you use , It can help you deal with logical errors – The premise is that you should understand the procedural requirements given to you .

  1. Compile error

Your program may not compile , Because you may have violated some of the rules the compiler expects you to follow .

therefore , The program you are writing may not compile .

for example , Write a string without using the usual quotation marks , Causes a compilation error , Because strings must use quotation marks . therefore , The code will not compile .

const name = Ayobami

This is similar to a syntax error , The more code you write , The better you can handle compilation errors .

You can improve your productivity by compiling or testing your code often , Reduce these errors .

  1. Resource error

Sometimes , Your program may exceed its memory limit or run out of available resources .

This may cause your application to stop serving or fail .

The following code is a practical example of code that causes a resource error .

function factorial(num) {
  var result = 1;
  for(var i = num; i > 0; i--){
    result = num * factorial(num-1);
  }
  return result;
}
​
factorial(5);
factorial(10);
factorial(20);
factorial(0);

Due to stack space ( That is, the memory allocated by the browser to the function call chain ) Used up , Function crashes or slows down the browser .

under these circumstances , The error is a resource error , Because it is due to running out of allocated memory ( resources ) And what happened .

  1. Interface error

occasionally , The program we designed API It is used in a certain way , But the way users use the program is different , It will cause errors .

This error is called an interface error .

for example , Suppose the method expects a string , But instead of calling it, we use a number . If the developer doesn't take this into account , It will lead to mistakes .

Most things in software follow standards . If the standards you define are not followed , You need to provide error messages or guidelines for your users , To help them figure out that they are using the application incorrectly .

under these circumstances , Record your API A lot of help .

Make sure your settings and tools match

It's important to have a tool that works for you .

Sometimes , Your operating system may not be compatible with your applications – Maybe it needs a newer version of the operating system , Or it needs some software .

for example , If the computer lacks some Microsoft VC Runtime ,WampServer It may not be possible in Windows Normal operation on the operating system .

Similar things can happen in Linux and macOS On .

You just need to make sure that your settings are appropriate for your development work .

Be certain about the functionality of your program

“ In Mathematics 、 In computer science and physics , Deterministic system refers to the system that does not involve randomness in the development of the future state of the system . therefore , A deterministic model will always produce the same output from a given starting condition or initial state ”.

that , The problem is , How do we make a deterministic program ?

You must determine the types of data that are acceptable in your program , Reject any non-conforming data .

In short , You need to accept the expected data , Reject data that does not meet expectations , Or inform the user of the expected data .

Don't use if you don't understand

Reduce bug One of the best ways to generate is to use only the methods you know 、 Methods and classes .

If you have to use any method or style that you don't understand , Study it before you do it , Make sure you have a deep understanding of it .

Whenever you use something you don't know , It's easy to introduce unnecessary bug.

Learn to type accurately

Typing accuracy is underestimated , Because programming is more about thinking than typing . But accuracy in typing may help you reduce some grammatical errors 、 Wrong type or typo .

Many programming errors are caused by simple typing errors .

The ability to type accurately can reduce the bug.

Observe other developers while debugging

Another interesting way to improve your debugging skills is , When other developers debug , Watch them , This helps to see different debugging methods .

There will always be some tools or methods we don't know or use to debug , Observing other people's debugging gives us the opportunity to discover tools or methods that we may not know .

Or even if you know the different ways , You may not know why or how to use them .

Observing others can influence us to reexamine these methods and tools , May eventually improve our debugging skills .

debugging

Debugging is the core of programming , Because it takes up the most time in coding .

Commissioning mainly involves three stages :

  • Find the error
  • Analyze and understand the cause of the error
  • Repair or remove bug

Find the error

Find out bug First, you need to understand the error messages you see .

Beyond all doubt , An error message is a bug Guide to .

If you understand the error message , You can accurately track the wrong location .

But some mistakes can be tedious , Because they may not have clear error information and unknown .

As I said before , Logic / Semantic errors just don't get the results we want .

In order to find bug, You need :

  • Define your expectations
  • Check the results you get
  • Compare your expectations with actual results , Look at the difference
  • You can use the debugger or other useful tools to quickly find these errors

then , You can examine different parts of the code based on your assumptions , And perform trial and error to find the error .

Analyze and understand the cause of the error

Find one bug after , You need to figure out why the code has such errors .

Doing so will help you build an efficient system .

contrary , Many developers will only search on Google , And directly from StackOverflow The answer .

This is not a problem in some cases , But it's best to know bug Why and why the solution works .

understand bug The reason is to repair bug Or delete bug An important step on the road .

Repair or remove bug

After finding and understanding bug After the reason for , We're going to fix this bug.

sometimes , When you understand bug After what is , You will find a solution without pressure .

however , sometimes , We understand that no matter how hard we try , Can't find any solution .

Instead of wasting time , Instead, Google for error messages or whatever you think is appropriate .

You can also ask another person , Because other people tend to have different views . They are neutral , This neutrality does help fix some bug.

therefore , Google it !

Fix a troubling bug It always brings great excitement .

But don't get too excited , Because fixing a bug It might cause another one bug.

therefore , First make sure you don't introduce another problem to the program .

This is why automated testing is important .

After commissioning

After debugging, you need to predict what might happen in the program you have written bug.

It refers to all the mechanisms you might use , To ensure that the unknown bug It can be easily tracked or managed before endangering the system or the company .

The question now is how do you do this ? Um. , Use an error tracking system .

You should have an error tracking system in production , So you can easily find mistakes , Because they appear after your application is pushed to production .

There are many error trackers , They just need to search the Internet . But here are a few you can see .

There are so many error trackers , You just need to choose the one that suits you best .

Conclusion

Debugging is an important skill that all software developers must develop .

It is the core of coding , If you do well , It can make you a better developer .

To be an excellent debugging expert , You must learn as many debugging methods as possible , Many methods have been discussed in this article .

Now it's time to be a good software developer , Debugging can help you along the way .

Now? , You just need to put everything into practice , You can be a good debugger , Your software development skills are different .

\

原网站

版权声明
本文为[Seven step programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251534358794.html