There is a famous saying, “Talk is cheap,Show me the code.” Both new and experienced people want to improve the quality of their code, or at least make it comfortable for others to see. My previous work experience did not attach much importance to the ability of personal coding, including correctness, aesthetics and efficiency. Someone who has been working for years can still write code that ugly, and it would be embarrassing to say so, so it’s imperative to improve your coding.

So what aspects can we start to improve their ability?

Books recommended

“Refactoring”

Martin Fowler’s book is one of the most classic books in software development. It is now in its second edition.

This is a book worth repeated reading, with the practice of work to learn.

Reframing has been clearly defined, in both noun and verb forms.

Refactoring, n: An adjustment to the internal structure of software to improve its comprehensibility and reduce its modification costs without changing its observable behavior. Refactoring (verb) : The use of a series of refactoring techniques to adjust the structure of software without changing its observable behavior.

Refactoring improves the design of existing code

“The Clean Code”

Clean Code is a classic book by Uncle Bob, and it is highly recommended that you read it.

Uncle Bob has condensed his understanding of clean code in this book, which is a great gift for future generations.

Clean Code Learning Record

Uncle Bob

Head First Design Patterns

Big Talk Design Mode

Design patterns are used to carry complex business logic, making written code concise and easily extensible. In short, you need to understand what changes in your business and what doesn’t. The things that change are complex business logic, and you need to think about how to accommodate it in a design pattern that scales well when it changes. If you have studied design patterns and are familiar with how each design pattern is used, you will be much more comfortable with it.

If you don’t have time to read these two books on Design patterns, you can refer to the following learning site: Design Patterns Learning

Or Head First design pattern learning record

“Effective Java”

Effective Java is a classic in the Java field, as influential as Ideas for Java Programming.

For a Java engineer, the ability to learn how to learn and how to learn quickly is key to growth as new technologies emerge and iterate. The way, thinking and ability to learn can be a huge boost to one’s growth.

Effective Java Learning

other

  • Alibaba Java Development Manual: github.com/alibaba/p3c
  • Google Java Programming Style Guide: www.hawstein.com/posts…

Excellent source code recommendation

Because of personal as a background developer, mainly learning to use the Java language, so the recommended here is Java related source code learning, but the avenue to simple, thousands of languages, the core will not change.

Although there are many projects with high star numbers on Github, most of them are highly functional and involve advanced technology points, which are highly respected by everyone. I will not list such projects here. The source code recommended here is the Java ecosystem framework source code.

JDK

Why read the JDK source code?

First, JDK source code is the basis of all other source code, read the JDK source code to see other source code will achieve twice the result with half the effort.

Two, JDK source code contains a lot of data structure knowledge, is a good data structure to learn, such as linked list, queue, hash table, red black tree, jump table, bucket, heap, double-ended queue and so on.

Third, JDK source code contains a large number of design patterns, is a good way to learn design patterns, such as adapter mode, template method mode, decorator mode, iterator mode, agent mode, factory mode, command mode, state mode and so on.

Third, the JDK source code contains a lot of high-level Java knowledge, such as weak references, Unsafe, CAS, locking principles, pseudo-sharing, etc., which is difficult to learn without looking at the source code.

JDK source code

  1. java.langBasic Packaging classes under packages (Integer, Long, Double, FloatEtc.), and string-related classes (String, StringBuffer, StringBuilderEtc.), common classes (Object, Exception, Thread, ThreadLocal, etc.).
  2. java.lang.refReference classes under the package (WeakReference, SoftReferenceEtc.)
  3. java.lang.annotationRelated classes for annotations under the package
  4. java.lang.reflectRelated classes for reflection under packages
  5. java.utilUnder the package are some utility classes, mainly composed of various container and collection classes (Map, Set, ListEtc.)
  6. java.util.concurrentFor concurrent packages, mainly atomic classes, locks, and concurrent utility classes
  7. java.iojava.nioYou can look at it in combination
  8. java.timeThis class contains time-dependent classes, which you can learn from in Java 8
  9. java.netPackage for network communication related classes, you can read the followingSocketHTTPClientThe relevant code

So much source code, do not expect to see all at once. If you want to understand multithreading, you should focus on JUC, if you want to understand IO, you should focus on NIO, if you want to understand constant pools, you should focus on ClassFileParser. When looking at modules, remember that interfaces are bigger than everything, or functions are bigger than everything. Don’t try to understand all the details. Find a few key functions, understand what functions do (for example, you should analyze function names and parameter names carefully), and then work your way down. When looking at the Java class library, we should pay more attention to whether the class is abstract, whether to use template methods, and pay more attention to the modifiers before the function, which generally indicates who the function is used for. Paying attention to these details, rather than going through the logic stupidly, can teach you a lot about design. It’s also worth noting where there are compromises to be made for the sake of previous designs, because a library that’s been around for so many years isn’t always perfect.

The JDK source code must look at the Java concurrency related source code, Doug Lea’s concurrency source code is beautiful, line by line is the best, very worth reading.

tool

Look at the class library first idea, multi-purpose Structure, multi-automatic generation of UML diagram. First understand the relationship between interfaces and classes, and then analyze functions.

Finally in the process of learning source code, don’t mind reading other people’s blogs, don’t mind handwritten notes.

spring-framework

The Spring Framework is an open source Java/Java EE full-stack application Framework distributed under the Apache license and ported to the.NET platform. The framework is based on the code in the Expert One-on-One Java EE Design and Development (ISBN 0-7645-4385-7), originally developed by Rod Johnson and Juergen Hoeller, among others. The Spring Framework provides an easy way to develop without the numerous property files and helper classes that can clutter the underlying code.

Spring is an open source framework at the design level, which solves the problem of loose coupling between the business logic layer and other layers, so it puts the thought of interface oriented programming throughout the entire system application. Spring MVC, Spring Boot and Spring Cloud derived on this basis are more and more widely used in modern enterprises. Both design ideas, code specifications, or design patterns, interface design, class loading, are very good source code.

Personal learning experience is as follows: first go to watch the video, probably familiar with the use of Spring, and then to learn the source code, here you can read the “Spring source in-depth analysis”, in addition to reading, remember to open IDEA to view the corresponding source code, if you can debug to see the specific call logic that would be better.

Google Guava

Google Guava is an open source version of Google’s internal Java development tools library. It is used by many Java projects within Google. It provides some functionality not provided by the JDK, as well as enhancements to existing JDK functionality. Among them: Collections, Caching, Primitives Support, Concurrency Libraries, Common Annotations, Strings Processing, Math, I/O, EventBus, and more.

practice

Through the above two ways of learning, we must put into practice what we absorb, input must have output, otherwise input is meaningless.

Here we need to emphasize the benefits of Code Review. There are three advantages of simple sorting:

Learn from each other and grow together

No matter what team you are on, there are differences in technical ability and experience among team members.

Through Code Review, experienced engineers can provide reasonable optimization suggestions to inexperienced engineers for the same function implementation. Inexperienced engineers can quickly learn best practices in technology use by reading good code. If everyone is equally skilled, it may be refreshing each other’s ideas.

2. Knowledge sharing

Code Review is not only about how the Code is written, but also about the business behind it. In this process, read the logic of the Code according to the description of the submitter to see if the Code implementation conforms to the description.

Through Review, team members are no longer limited to a certain part of the project, so as to avoid the embarrassing situation that only one person knows the project. In special cases, you can also maintain the relevant code on behalf of others in a timely manner, especially in the absence of relevant business function documentation.

3, unified coding style, improve quality

Code quality generally falls into the following levels:

It works — it passes tests — it’s easy to read — it’s easy to maintain.

At a minimum, make your Code easy to read.

In order to make it easy to read, it is not enough to just rely on Code Review. First of all, we need to observe the style of the whole project and write codes with the same style according to the existing specifications. Of course, everyone’s style may not be exactly the same, but we should ensure that the overall style is similar, so that it will not be uncomfortable to maintain.

conclusion

Writing code today is like writing in school. Being able to code well is like being able to write well. If you look back at your handwriting every once in a while and can’t bear to look directly at it, you can improve yourself by copying your handwriting and eventually acquire good handwriting.

Practice more words will become a habit, and then writing becomes a subconscious habit. Writing code is not that easy. You need to think about patterns before you write it, how to design it, and how to optimize it after you write it.

Follow the learning method above and slowly improve your coding habits until you can write like a god.