I don't know if any of you have such experience? A certain code operation, which was rarely written before, suddenly used but can’t remember how to write it; a certain tool library has not been used for a long time, and now I want to use it and even forget the library name; a certain command line, I have used , and now I have forgotten what parameters it has... I have encountered this experience many times anyway. Programmers have been working for a long time, and it is inevitable that they occasionally need to write some difficult codes ( low-frequency codes ). So programmers always ridicule themselves that they are programming for Baidu/CSDN/GITHUB.
To remind everyone of the fear of being dominated by low-frequency code, let's throw a concrete example: Can you write code to read and write files in Java? , To be honest, although I am a Java programmer, I still have to check the relevant information for me to write this. Although the frequency of this kind of problem is very low, it still takes a while to repeat the toss once encountered. Therefore, my later choice is to record all these code snippets , and if I encounter them again later, I can copy and paste them directly. Compared with re-checking the information online, it is not a little faster. Every time a similar problem is encountered, it is conservatively estimated that ten minutes or half an hour can be saved.
Another thing I would say is that documenting code snippets is not just to save time later, it can also indirectly help you build your body of knowledge in certain directions. Think about it, if every time you encounter a new problem and get a code snippet after solving it, you want to record it and make it recallable later, you have to add some additional information, The process of supplementing information pushes you back to get more content. Over time, you will accumulate more and more knowledge. For example, I will definitely add some code comments or additional code samples when I record code snippets myself.
There are many ways to record code, such as Github Gist , but this is more suitable for putting some open source code fragments, not suitable for private use. You can also directly use cloud notes to save code snippets, as long as you do a good job of filing, labeling, and writing descriptions for easy retrieval. There are also dedicated code snippet software, such as SnippetsLab (paid software) that I am using. Compared with ordinary cloud notes, SnippetsLab code is much more convenient to archive and retrieve. Personally, I still think it is very useful. . The picture below is some ffmpeg operations I recorded when processing video before. If ffmpeg is used again, these contents will definitely come in handy.
Seeing this, you must also want to build your own code snippet library immediately. The main question is which code should be stored. My suggestion is to save all the code snippets that you are not familiar with but will definitely use in the future . Taking my own code base as an example, I will store the following types of code snippets.
Use of some uncommon commands
I have saved a lot of awk-related commands myself, and the saved content can even produce a complete awk tutorial. The new generation of programmers may not use awk anymore, or even don't know what awk is. If you are interested, you can read an article I wrote before. The Linux Three Musketeers: Awk Getting Started Guide . Although I have stopped doing operation and maintenance for a long time, I still occasionally use awk to process some text data (application logs). To find out, I just used grep and awk commands to sift it out from the log. It took less than 5 minutes for more than 1,000 devices.
Use of certain tools
Some class libraries have been used before, and now they are used, but suddenly I can't remember how to use them. This experience should be very common for programmers in any language and any position. For example, the guava library commonly used by our Java programmers, the apache-common library, and some frequently used packages in Jdk... , node, python, go... Programmers should also have similar confusions, after all, they can import at will.
some special syntax
Although I have been writing Java for many years, I am not yet proficient in using a lot of Java's syntactic sugar. To give a simple example, such as groupingBy in Java lambda expression, I still can't write it myself. After all, I have not systematically learned lambda expression, nor have I used it often. I will record this directly, and copy it directly next time I encounter it.
Some special configuration methods
Do you know how nginx configures https? Do you know how jvm configures GC-related things? Do you know how xxx can be started from the command line? ...I don't know, but I have it in my codebase and I can give you the answer in 20 seconds.
Some complete code demo
Although I am a full-time Java programmer, I occasionally need to write python and shell scripts to achieve some functions, because I am not professional, and even some grammars have to be checked now. Fortunately, there are not many scenes using other languages. Simply save the complete code Demo directly, and change it based on it next time.
The above are the major categories of codes that I currently store in the codebase. There are also some code snippets that I would have saved before, but I will not save them now, because with the increase of my working hours, some things are already familiar to me. In the future, these Useless code is also gradually cleaned up. Finally, there are a few points that need to be paid attention to:
- The code snippet is supplemented with additional information
The purpose of recording code slices is to quickly reuse them later, or to quickly recall certain information. First of all, it is required that the code piece can be quickly retrieved, write the code description, label it, and classify it well to ensure that the code can be found quickly, otherwise it is meaningless. Secondly, you have to be able to understand the code after you find it, so some complex code is recommended to add detailed comments, references, or more forms of examples. - Pay attention to information security
I know that some people will save account passwords in the code chips. Don't put such code chips with sensitive information on the public Internet, and don't share them at will, otherwise, if they are leaked, the gains will outweigh the losses.
That's all for today's article, please like it if you like it, and follow it if it's useful. I'm XINDOO, a programmer who uses efficiency to earn paddling time.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。