My Architecture Experience series-backend architecture-language Layer

Source: Internet
Author: User
Language Level:


  • Run-time metadata acquisition
The so-called runtime metadata acquisition means dynamically obtaining type, method, and attribute information through code during program running, and then dynamically obtaining attribute values and execution methods, reflection is called in some languages. Reflection is not necessarily efficient, but reflection is a useful technique when writing a Framework Program, and the performance overhead of reflection can be minimized by means such as caching. For example, in an Orm, all attributes are dynamically obtained based on the object class information, and then their values are obtained to generate the SQL statement to be executed in the database. Understanding reflection and mastering the use of reflection and performance optimization are important for compiling framework code.


  • Error Handling
Any backend language has its error handling mechanism ,. net and Java exception handling mechanisms are also absorbed by more languages. Although the exception mechanisms of different languages are not necessarily the same, they are also similar. The principles of error handling and exception handling are summarized as follows:

  • Do not eat the wrong results, and do not eat the caught exceptions. The so-called "eat" means that the system does not process the error results or exceptions. In this way, no one knows that this exception is very dangerous. In fact, the so-called exception is the implicit meaning that the Code cannot execute the code statement itself. For example, a method is named to create a file, in this case, the method should throw an exception when creating a file, because it cannot do what it should do. As a class library writer, it should do this, sometimes, when something goes wrong, we will be confused about the cause of the problem. As a class library user, we should handle exceptions around such methods and record such exceptions, then we can easily find the root cause through the exception information, instead of guessing. Sometimes there is no way to solve the problem by guessing, because the class library of the language is a part of dealing with the operating system. Only the class library knows what problems the operating system has, for example, you cannot create a file because of the permission or the disk is damaged. If you discard this exception information, it is hard to know the root cause. The latest UI front-end framework! Trial with prize!
  • The specific process of error handling cannot be based on the situation. In addition, it is generally not recommended to directly capture the largest exception and refine it as much as possible. Capturing all exceptions means that the exception cannot be processed. In general, logs can be recorded, thrown after repackaging, or error handling can be clearly performed.
  • Unprocessed exceptions tend to rise as the call stack goes up to the upper layer. If the Web server finds exceptions that are still not processed, the pages may be of various colors. I personally think that an error page is not necessarily a bad thing. Many people like to eat all the exceptions without an error page. This is not the fundamental solution to the problem, the fundamental solution is to find the cause of the exception and solve it from the code perspective, instead of hiding your ears. Of course, the error page should not be visible to end users. It should be replaced with a friendly page. on this page, you can neither write anything nor write an exception ID, if you think this operation is very important, you can use this exception ID to give feedback to customer service and explain the operation process to help us solve the problem, of course, the premise is that all exceptions can be recorded in the database or text files for convenient query. Many frameworks have a central portal for collecting unprocessed exceptions. Here we can summarize records of unprocessed exceptions.


  • Garbage Collection
Except C/C ++, most scripting languages and compiled languages with virtual machines are automatically garbage collection. Although the garbage collection mechanism does not require manual processing of object release, garbage collection is not omnipotent and may cause memory leakage. Of course, the memory leakage mentioned here is not the same as the memory leakage of C/C ++. In general, garbage collection determines whether an object can be recycled by referencing the reference root of the object, if an object is always referenced by the root due to improper program writing, the object may not be released, resulting in constant memory expansion. Although the object is pointed by a pointer, it is not a wild object without any pointers, but we forget this object, so we can say that there is a memory leak. Therefore, even if there is garbage collection, we should pay special attention to the use of static objects, whether it must be static, whether it can be weak references, and try to avoid using the root with a long Declaration cycle. The latest UI front-end framework! Trial with prize!


  • Multithreading
Most compilation Languages Support writing multiple threads using code. multithreading is a useful technology that allows the main thread and UI thread to stop responding because of other operations, it can be used to execute multiple tasks at the same time to speed up task execution and make full use of the processing capabilities of multi-core CPUs. Of course, it can also directly split a task into multiple tasks for parallel execution, how much CPU can be executed. Since multi-threaded programs cannot be scheduled during execution during encoding, we must be especially careful when coding the problems caused by multithreading:

  • If multiple threads access a resource at the same time, for example, accumulating a number at the same time, it is very likely that it will not meet our expectations.
  • The types provided by the class library are not always thread-safe. When using the class library, you must read the relevant documents to check whether it is thread-safe, if not, we need to use measures such as locks to ensure thread security. Otherwise, exceptions or incorrect code may occur when the class library is called.
  • Multi-threaded program debugging is also quite troublesome, because multiple threads may execute different codes interspersed, at this time, we can increase the code debugging by logging, suspending some threads, or temporarily switching to a single-threaded program.
  • When coding, we need to realize what is a thread and how much resources a thread consumes, because we do not think that multithreading can improve efficiency and that all operations are done by a bunch of threads, enabling multiple threads is not worth the candle. Although the thread costs less than the process, the cost is not so small, because each thread has a large thread stack, therefore, if you find that your program has opened thousands of threads, you may want to check whether this is reasonable.
  • Remember not to forget this thread when enabling multithreading, and do not let the thread do nothing about empty loops. For background task threads, we can record the threads opened in our program in one place. For other threads that can end, ensure that the code in the thread can end normally.
  • If the code in the thread encounters an exception, most runtime or virtual opportunities think this is a serious problem, because it may cause the status of the entire system to be inconsistent, for example, if the system involves money, this is a serious problem. In this case, it is better to terminate the process of the entire application and prevent this problem from being discovered. The wrong state may spread, if you think that the thread is only doing operations that are irrelevant to the important state, make sure that the thread does not encounter unhandled exceptions. The latest UI front-end framework! Trial with prize!


  • Code Generation
Code generation has many functions. For example, we can use code generation to reduce the amount of writing our code, or we can use code generation to implement cross-section operations such as AOP. Generally, there are two ways to generate code:

  • Dynamic generation: the code is dynamically generated when the program is running. The generated code is dynamically loaded to the running environment for dynamic execution after dynamic compilation, this method is suitable for dynamically generating code based on the Program Logic. For example, to dynamically generate a proxy class, if the proxy class interface cannot be determined in advance, we cannot generate code before compilation.
  • Static generation: it is usually generated before compilation and then compiled directly. For example, the CRUD operation code is automatically generated according to the data definition in XML, which not only avoids handwriting code, but also achieves the same efficiency as handwritten code, because this code is still fixed and not dynamically organized at runtime, it is the most efficient.

My Architecture Experience series-backend architecture-language Layer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.