How can junior programmers improve their code quality

I still occasionally think of the experience of being an Android development intern in a small company in my junior year.

At that time I was still a super novice, but I was full of enthusiasm, so I learned things quickly (in fact, I remembered a lot of APIs). My self-confidence was overwhelming, but it didn't take long for me to be shattered by several code reviews by the team leader.

"Why is the constant name not capitalized?"

"There are a lot of typo (misspelled words) in the code submitted this time"

"This API is not used in this way, it is too far-fetched to write here"

"Will the problem be completely solved by using sleep for a few seconds? Sleep for a few seconds is reasonable? What if the user's mobile phone is slow?"

"Your function is almost a few hundred lines long"

"The if else of your code is like a matryoshka"

... ...


That period of internship made me grow a lot. Although I did not continue to do Android development in the end, the many experiences and coding principles I learned during that period have benefited me a lot.

After a few years of work experience after graduation, I gradually summarized some thoughts on improving code quality


First, have a cleanliness of the code

When writing code, you can’t have thoughts like “just run it” and “just function normally”, otherwise you will gradually write piles of shit, and other people or even yourself will not only be hard to maintain when reading the code. , Expand new functions.

To give a few examples:

Do not use if else frequently, or even nested, try to use polymorphism to achieve;

Give each variable/function/method/class/package a name that is as reasonable and clear as possible;

Constant names should be capitalized, and variable names should not have typos

...

In addition to these more basics, you can gradually think about the structure of the code, such as introducing some design patterns to make the code structure clearer and better maintainable-refer to my previous article

Why learn design patternsmp.weixin.qq.comicon

In short, make your code look clean, concise and maintainable.

I recommend reading "Clean Code: The Way to Clean Code" and "Refactoring: Improving the Design of Existing Code"


Second, learn the best practices of API

On this point, not only junior programmers, I even see some senior programmers are making this kind of mistakes.

It’s always exciting to just start learning new technologies or new APIs

"This API is quite interesting, try writing code next time!"

At this time, you seem to have picked up a hammer on the ground. You see everything like a nail and want to hammer it a few times.

So you might write some code that you think is cool but a bit "funny" in the eyes of the old driver. Give two examples:

Java 8 Optional

String names = Optional.ofNullable(httpServerRequest.params().get("name")).orElse(GENESIS_NAME);

Refer to my article

FreewheelLee: Java 8 Optional best practiceszhuanlan.zhihu.comicon

JavaScript array destructuring

const [age, name] = [person.age === null ? 0 : person.age, person.name === null ? '' : person.name]


Seeing these two codes, do you feel an arrow in your knees or the old driver's smile on your face?

I have made similar mistakes, so when I learn a new API, I will definitely go to Google " XXX best practices " or " XXX best practices " to understand the original design and best practices of this new API, so that I can learn to write The correct code .

In addition, reading the Effective series of books is also very beneficial, such as "Effective Java (Third Edition)"


Third, learn and adhere to some coding principles

such as

1. Open Closed Design Principle: Open to extension and closed to modification-to maintain the scalability of the code, try to avoid *** modify the original code when implementing new functions

2. Single Responsibility Principle: Single responsibility principle-each method and each class try to maintain a single responsibility

3. Dependency Injection or Inversion principle: Dependency Injection (dependency inversion)-in a class (file), do not create instances of other classes that you depend on, but pass in from outside (this is one of the core designs of Java Spring )

4. Liskov Substitution Principle: Liskov Substitution Principle-Any subclass should fully implement the function of the parent class, and the subclass implementation can be replaced at will without affecting the caller

5. Programming for Interface not implementation: Interface-oriented programming rather than implementation-oriented-design from a higher level, "interface" not only refers to Java interfaces, but also packages or even APIs

and many more

Familiar with, understand and adhere to these coding/design principles will gradually improve the quality of your code.

I have been grateful for my own code design many times when expanding new features or maintaining code

"Thanks to my original design, now it is too easy to expand this function!"

I hope you will have this experience often.



Finally, just like the title of my column "Coder's Programming Art", I hope you can also treat programming as an art, pay attention to design and details, so as to continuously improve the code quality and become a professional software engineer.


Guess you like

Origin blog.51cto.com/15064417/2569714