当前位置:网站首页>A good habit that makes your programming ability soar

A good habit that makes your programming ability soar

2022-06-24 12:28:00 Programmer fish skin

Share your good habits when writing code , Let your programming ability improve by leaps and bounds !

Hello everyone , I'm fish skin , Last time when it comes to , Many students do not pay attention to code quality when learning programming , While forming bad habits , Lost the opportunity to improve your programming skills .

Students who haven't read the last article , Please read first : There will be problems with the labor code ?!

At the end of the article, I raised the question :

How to intentionally improve the quality of your code ? I can't find any problems in my code , I thought I had written very well , What do I do ?

To solve this problem is actually very simple , Since I am inexperienced 、 Or is it “ Only the body in this mountain ”, I can't see where the code is not written well , Find other friends or experienced programmers to help you look at the code , Give some comments .

Although every normal programmer can't stand being called a spicy chicken when he writes code , But be open to correction , And modify it , It can really deepen our impression , The next time I write code, I will pay attention to . Slowly, , I have formed a good habit , Not only focus on your own code quality , Will “ Inheriting tradition ”, Start talking about the code written by others , So as to help more people improve .

But there is a cruel reality , Who is so kind to help me look at the code ?

When I was in College , Although I have done many projects , I've written a lot of code , But my own code is basically just for me . Even if the code of the big homework is given to the teacher , They don't even look at the code , It only depends on the running effect , This is one of the reasons why people pay less and less attention to code quality . And to be honest , I used to ask a senior student to help me look at the code , But think about it , If I don't have a very good relationship with you , Who is free to help you read your spicy chicken code for free ?

No one can ask for help , I can only ask myself .

One way is to put your own developed works online , Let users use it , To help you find something you didn't notice Bug Or functional problems , It drives you to read and improve the code . You can also expose your own code to GitHub Wait for the code hosting platform , Allow other students to read and point out the questions . however , This approach is too hard core , It takes a lot of energy to promote in the early stage , Otherwise, no one will use your project , No one came to see you GitHub!

Such as my Programming navigation The project is open source 4 More than a month. , Documentation is written with great care , Also did a little Promotion , And then I got 600+ star, It's really not easy .

Programming navigation open source project

These days I have greatly optimized the next document , And open source the back-end code ! If you are right about the front end 、React、 Back end 、Java、Spring Boot、 Tencent cloud development 、 Official account development 、 Document site generation, etc , Welcome your attention ~

Address :https://github.com/liyupi/code-nav

( You can also click the bottom of the article to read the original text directly )

Programming navigation open source project

Return to the right topic , We can't play hard core , Then I will teach you a skill that everyone can easily learn , It is also a programming habit that I have always adhered to .

Let's take a look at my code Little stubborn Well , Effectively improve the quality of your code , Programming ability has improved by leaps and bounds !

My programming habits

Like I said before , The reason why we can't find problems in the code , Largely because our programming attitude is “ optimistic ” Of , Believe in video tutorials 、 Believe in books 、 Trust the editor , As long as there's no mistake , That's right .

But when I write code , Very much “ Pessimism ” And stubborn , I will take every line of code I have written as a patient , Insist that they have a problem , It can even be said to pick a bone from an egg !

Some students said , Aren't you just talking nonsense , That's just checking the code carefully ? Or defensive programming ?

What is defensive programming , You can see my article : The sword of Damocles in software development

Don't worry , This is not the same as defensive programming , How stubborn I am , Take a chestnut and you'll see , Now please follow my train of thought ~

How stubborn I am ?

If I write such a front-end code , The function is to get the name of a dog :

//  Default dog name 
let name = ' Nameless dog ';
//  Dog 、 Dog information 、 Dog names exist 
if (dog && dog.info && dog.info.name) {
  name = dog.info.name;
}

Do you think there is something wrong with this code ?

If you think there's no problem , So good , You are optimistic .

Doesn't matter , I don't want you to think , I am stubborn , I just think there is something wrong with this code , No problem. I have to pick my own problems .

Um. , There seems to be nothing wrong with the code logic , If there is a problem, just say it , Is the amount of code a little too much ?

For example, when obtaining the dog name , In order to prevent error reporting , First determine whether the dog exists , Then judge whether the dog information exists , Finally, we can get the dog name , I'm so wordy . If you need to get a bunch of other information , The code is almost unreadable .

therefore , I began to think , How to make this code more compact , Is there a better way of writing ?

Through Baidu , I found an awesome thing , It's called Optional chain operator ?. ), Allow reading attribute values deep in the object , Instead of explicitly verifying that each reference in the chain is valid ; There is no error when the reference is empty , It will automatically short circuit the return value .

therefore , This is what the code is like :

//  Default dog name 
let name = ' Nameless dog ';
//  Dog 、 Dog information 、 Dog names exist 
if (dog?.info?.name) {
  name = dog.info.name;
}

It has been simplified a lot , But I'm not satisfied , To get a dog name, you should write 4 Line code ? There must be a problem ! Can you simplify it a little more ?

Think about it , This time I relied on my own basic knowledge , Without Baidu, I came up with a more concise way to write , Use JavaScript The ternary operator of , The code is as follows :

let name = dog?.info?.name ? dog.info.name : ' John doe ';

What about? , One line of code !

Let's give you a chance , There is still a problem with the code written like this ? Or say , Do you feel comfortable with this line of code ?

Anyway, I don't think so , I must pick a bone from an egg !

If you look carefully, , We found that dog、info、name These three words are repeated twice , What can we do to simplify it ?

Open the search engine again , This time I searched for a long time and didn't find an optimization method , Then I'll just search “JavaScript Tips ”、“JavaScript Advanced writing ” Other keywords , We finally found the answer .

Null merge operator ?? ), When the operands on the left are null perhaps undefined when , Returns its right-hand operand , Otherwise, return the left-hand operand .

Final , The code shrinks to this :

let name = dog?.info?.name ?? ' Nameless dog ';

Much more comfortable !

This is my stubbornness when writing code , Through this challenge , It helped me learn at least two JavaScript Usage of operators , amazing !

Is it worth it ?

That's what some students said : Write code like you do , Aren't you tired ? To dig into the details of the code so carefully , It won't take much time ?

The small , The pattern is smaller .

From this time of writing code , Indeed, I spent more time , Just let the code run , Whatever else you do ? But in the long run , This is the only way to learn , Find problems by thinking for yourself 、 solve the problem , The knowledge learned can be said to be deeply engraved in my mind , When writing similar code later , It will be easier 、 Standardized and efficient , The time saved is infinite !

Of course , Details also depend on the situation , If something you want to develop is urgent , It is certain to focus on the completion of functions first . But when the back is free , Be sure to optimize , Otherwise you're code “ Shishan mountain ” Contributed to their own strength .

What are the other benefits of pessimistic programming ?

Optimistic programming , We often find problems passively , Thought the code was running smoothly , But it is often mentioned by others Bug, Then forced to modify , The mentality will get worse .

And pessimistic programming , We look for problems on our own initiative , Proactively optimize code . When you think 、 Search and practice , After simplifying the originally complex code , You will be surprised to find : I went to , The original code can also be written like this ! Bull by bull !

then , You will gain a sense of accomplishment , Continue this habit , Continuous improvement .

This reminds me of pinduoduo , Early did not give users high expectations , Make them feel that this is an awesome product . Instead, it actively lowers users' expectations , Let everyone feel that this is a local product , Later, it exceeded the expectation again , And finally to success .

No problem found ?

See here , You should understand my stubbornness when writing code . But some students will say , I don't have this keen intuition , What if you can't see the problems in your code ?

I have some experience and methods , You can try :

  1. When you find it difficult to write a piece of code , There's probably a problem , You might as well search whether there are ready-made components , Or how others did it .
  2. When you repeatedly write a piece of code , There must be something wrong , You might as well abstract it into functions or components 、 Or use design patterns to optimize .
  3. When a lot of... Is written in a piece of code If Else when , There must be something wrong , Try to rearrange the logic , Reduce conditions and branches .
  4. Pay more attention to the yellow and red alert of development tools , You can usually get a lot of good optimization suggestions , Find problems early .

Let me give you another exercise , The chestnuts raised above are front-end , Let's take one example this time Java . .

The goal is to get a list of all the dog names , The code written by little Abba is as follows :

//  Get a list of all dog names 
List<Dog> dogList = new ArrayList<>();
List<String> dogNameList = new ArrayList<>();
if (dogList != null && dogList.size() > 0) {
  for (int i = 0; i < dogList.size(); i++) {
    Dog dog = dogList.get(i);
    if (dog != null) {
      dogNameList.add(dog.getName());
    }
  }
}
return dogNameList;

Is there a problem with this code ? How do you optimize it ? Let's have a try ~

tip , One line of code can achieve the same function ! Welcome to the comment area .


Well, the above is the sharing of this issue , If it helps, ask for Fabulous , I hope more people can learn .

I'm fish skin , Originality is not easy. , If you think the article is good , Hope friends give the thumbs-up Under the support , Give me some creative motivation .

I'm still developing my Programming navigation https://www.code-nav.cn ), A project to help you find programming resources , Welcome to use !

Various programming resources

How did I teach myself in college , Get Tencent 、 Byte and other big factories offer Of , You can read this article , No more confusion !

I studied computer for four years , Mutual encouragement !https://t.1yb.co/q0mS

原网站

版权声明
本文为[Programmer fish skin]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210601212821942t.html

随机推荐