The Tao of Programming .2.20190606

Foreword

This is my personal summary of some programming ideas, from 2014 I contacted the life of the first programming language (C) start, it has guided me, and I continue to improve it. Some of them are my own conclusions drawn in practice, some of the book learned. In this sharing. In the future I will always improve it, so you can see, the blog title with a number of new content in the future, I will be directly appended to this blog, and update the blog title.

Antithesis word

add/remove       increment/decrement  open/close
begin/end        insert/delete        show/hide
create/destroy   lock/unlock          source/target
first/last       min/max              start/stop
get/put          next/previous        up/down
get/set          old/new              locked/unlocked
opened/closed    visible/invisible    source/destination 

name

  • folder

    The same function. Folders do not use the plural form, because the qualities folder already contains a plural meaning.

  • function

    Word the first letter of the first word lowercase, uppercase behind

      和获取数据有关的用get。直接从数据库查询的用find。find有从数据库表里查询的含义,
      所以在一个需要隐藏数据库的层面,不应该使用find,应该使用get
    
      数据库用的是insert/delete,其他层次我们使用add/remove
    
      由于函数的参数列表也是其抽象的一部分,所以某些含义应该以参数的型式展示,而不是描
      述进参数名字中,这样的命名策略可避免出现复杂的参数名称。例如,从数据库中查询某种
      信息,findAllByxxx就可以把Byxxx省略,因为你的参数列表已经表明了这样的用意
    
  • variable

    Typically, the first word lowercase, followed by the word capitalized. You can use an underscore as appropriate, but do not use the dash. final static constants are all uppercase, and separate words with an underscore.

  • class

    All capitalized words

Tactics

  • To make all changes to the side effects can be found at compile level; change of side effects limited to a minimal scope.

      这意味着如果你发现某些代码的变化,无法被编译器识别,你就要斟酌一下了。
    
    • You can not use BeanUtils.copyProperties (), or a similar tool. When Bean because it causes the interface changes, the side effects can not be perceived compiler.
  • Wherein the program having a pyramid structure, with the bottom of the broader abstract interfaces, the closer the top layer, the more as the interface (i.e., the fitting and service)

    • Designed for a specific service interface can not appear in the dao layer.

      Determine whether an interface designed for a specific business, the key is to determine whether a change in the interface is directly determined by the business changes.

      Interface dao layer should be no feelings of select, insert, update, delete. Once you find a business interface has a tendency to fit, such as selectForXXX, this interface will need to reconsider.

Back-end architecture

You can follow the "Ali Baba Java Development Manual" (6.1 layered application, a chapter). Note that the combination of DAO multiplexing is done in the layer manager.

About the return value

  • Dao data returned from either basic data types, either DO entity.

  • Data returned from service, either basic data types, either DTO entity.

How DAO work

Each table should have a primary DAO, DO produce around the main table, and try to avoid contingency table.

"High Performance Mysql" has this to say:

It is in any case a costly sorting operation, so from the viewpoint of performance, should be avoided as much as possible or to avoid as far as possible to sort large amounts of data to be sorted.

The first table if all the columns in the ORDER BY clause to come from the association, the association process mysql when the first table to be sorted file. All other cases, the results will be first mysql association into a temporary table, and then after all the association all over, and then sort the file.

For reference: I think a certain period of time to ponder what should go into sql DAO, you need to think about who this master table is SQL, and the information given above, is a very good thinking this problem prompts.

Some small details

  • If you use the query mapper enumeration mapping database tables in the field, should not add the database is not in the enumeration values, such as with a number to represent "all" concept (but in fact, the database does not have a status value and it corresponds). If you do, you'll find when you need to use the collection to cooperate IN query, the extra value will be your trouble.

  • Lower for the upper layer service, goal-oriented. What is the upper (the business logic) need, what the underlying (Data Access Layer) provides. Rather than lower (data access layer) What, the upper layer (the business logic) what to use.

  • dao layer does not provide the calculated attributes to provide real existence only attribute. (Although I do not see the top attribute dao provided is real or calculated, but it allows you to observe the effective separation of business logic and sql.)

  • dao layer has two parts, mapper.xml one carries the actual code, one providing an interface mapper.java. mapper.java to provide a clear list of parameters and return values, do prohibit the use Map parameters and return values.

  • NEP any problems by the user data to be guaranteed. (You have to assume that any data outside of basic data types are likely to NULL, NULL and the situation appears to make a deal)

  • Operation of the database to be polymerized to a specific interface, data query is not required. This helps control to modify the data.

Practice of the Road

  • Each function, each class and each module are engrossed in the issue

  • The classes and functions should be made small enough to eliminate the need for former members of stamps

  • Function should do one thing and do it well, only do this one thing

  • Function either do, or what to answer, but they can not have both

  • Demeter law, internal module should understand the situation it operates objects.

  • We like the private utility functions called by a public function immediately behind the public function, in line with the top-down principle, so that the program reads like a newspaper article.

  • It was named a means to help determine the length of the class, if you can not name a precise, this class probably would be too long for the life of a class. The more ambiguous the name of the class, the class is more likely to have too many responsibilities.

  • You are trying to return home to many tool drawers, each drawer with a well-defined and labeled components toolbox, or do you want a few can not just put all the stuff thrown in a drawer

  • We want to fight the system when you add or modify the characteristics of little trouble as possible shelf. In an ideal system, we add new features through the expansion of the system rather than modify existing code.

  • You are trying to return home to many tool drawers, each drawer with a well-defined and labeled components toolbox, or do you want a few can not just put all the stuff thrown in a drawer

  • We want to fight the system when you add or modify the characteristics of little trouble as possible shelf. In an ideal system, we add new features through the expansion of the system rather than modify existing code.

  • Should depend on the abstract class rather than relying on the specific details (oriented programming interface)

  • Encapsulation boundary conditions.

  • If a combination of several functions invoked, then they should be packaged as a whole, in order to maintain their integrity.

  • Data objects hidden behind the abstract, function operation data is exposed. Data structure is exposed its data, do not provide a meaningful function. Note that the class is not simply to push the external variables with the values ​​and setters, but exposure abstract interface, so that users do not understand the data will be able to achieve operational data body.

  • In good object-oriented design, each object can be very good to complete a task, but it does not try to do more.

  • According to the program developers to create characters who are divided into categories (those programmers to create new data types) and client programmers (those that use data types in their applications class consumers) are helpful.

  • When you inherit an existing type, it created a new type. This new type not only include all members of the existing type, but more importantly, it replicates the base class interface, that is, all messages can be sent to the base class objects also can be sent to the derived class object. Since the type of message by sending to the class of the class of known type, this means that the base class to derived class of the same type.

  • The derived class is regarded as its base class is called upward transition.

  • Object-oriented programming is often summarized as simply "sending messages to objects."

  • You (the client programmer) can see only two parts of the definition regarding the following: the object used to represent the concept of the problem space, and used to represent behavior in this space of information to these objects.

  • java: closure is a callable objects, which records some information, which is created from his scope

    js: Closure allows you to package some of the acts, like an object as it came to delivery, but it is still able to access the original context in the first statement.

  • I really need to upcast it? (To determine whether the use of inheritance)

    Inheritance is a (is-a) relationship, the combination is (has-a) Relationship

    For example, a security door (is-a) door, it should inherit from a door security door

    The car has a (has-a) door, then it should be a combination of relationships.

  • When designing a system, the goal should be to find or create some classes, each class has a specific purpose, and neither too large (too many functions and difficult to reuse) nor too small ( do not add other functions can not be used).

  • The code is seen as an organic, evolving life forms away culture.

  • Class too abstract a performance parameter list is too long (too many properties need to define object creation); a suitable abstract class which depends on their level may be. Argument too large is not necessarily too abstract, there may be at the bottom because it had been designed to use high-level call (the bottom of the class have been very abstract, they are high on flexibility requirements).

  • Class with a performance like too much is too long class name (job description details and specific class too). In this case you have to consider that it might be attributed to an abstract, the embodiment of several interfaces, rather than a separate class.

  • If some of the same class have some characteristics, you should consider these features it extracts, which form a parent.

  • The right principle should be preferred classes instead of interfaces. From the beginning of class, if the need becomes very clear interface, then the process of reconstruction. (Which you can avoid a lot of sense of structure at the beginning of the program interface, and endless)

  • As long as a method of operation is a class rather than an interface, then you can only use this class and its subclasses. If you want to apply to this method is not in this inheritance structure of a class, then you will come to grief up.

  • An interface says: "All classes that implement the interface looks like this." Therefore, any code that uses a particular interface knows what methods can be called the interface, but just know that.

  • We can add a new interface on top of any existing class, so this means making method receives an interface type, it is a way for any class of the method can be adapted way. This is the power of using the interface. (But do not abuse Interface)

  • Core reason of using the interface: In order to upcast plurality of base types (and thus bringing flexibility).

  • When humans found only apples when the fruit is not the abstract concept of existence value only when oranges and bananas are also found fruit before its existence it has meaning. (For the program in terms of increasing the abstract, this sentence can inspire you)

  • Any abstract should be a real need to be generated. When necessary, you should first consider the reconstruction of the interface instead of adding an extra level of indirection everywhere, and the resulting additional complexity, this additional complexity is very significant. (Do not blindly add new interfaces)

  • Maintenance codes, continuous improvement code, the code will be diligent.

  • Inner class can access all the properties of the enclosing class, it does not require any special conditions. Inner classes and used in combination is not the same.

  • Good programmers spent 90% of time to think, research and experiments to find the optimal solution. Poor programmers spent 90% of program debugging problems, blind modify the program, expect some kind of wording feasible.

  • For the same thing or different name for the same kind of action will become your worst nightmare (Do not name seriously).

  • Programs must be written for posters, only incidental to the machine to perform the task.

  • Encapsulate complex, simple interfaces. - "Luo Ji thinking"

  • This entry talking about the interface is generalized interface rather than object-oriented programming that interface (interface)

    The magic of the interface is that you need to do and just need to know what it would receive.

    Simply place the interface is that it clearly tells you what it needs, what output.

  • Word concept: to provide the interface entity refers to one kind of their abstraction outside thereof (may be another entity) for separating the internal and external communication method of operation, it can be modified without affecting the internal external entities interact with other The way.

  • Between data and user data plus one level, some benefits of doing so is the operations on the data, such as control precision, cast, etc. can focus on this level, without the need to have processed each data user again. (Discussed here, only the data and user data, and the data does not include the case where a data provider)

  • There is a class hierarchy, class bottom decoupled as possible, they are independent of the more convenient to use. (The more abstract concept of class, the more flexible it is, I want to eat fruit and I want to eat apples, obviously the former option more)

  • Private members clearly tell you, it does not matter, and others. This can effectively reduce complexity.

  • The primary mission of technical software - management complexity. Package to help you manage the complexity of the method is to let you see the complexity. Abstract of the main benefits is that it allows you to ignore irrelevant details.

  • In accordance with the principle of information hiding to think, to stimulate and promote the formation of some of the design decisions, but only in accordance with the principle of thinking has not objects.

  • "This class should be something to hide?" On all aspects of the design, you can ask what to hide to bring about good design decisions.

  • Data hidden point is, if you do not, the operation of the data will be scattered throughout the procedure, a small change may need to modify the 100 place.

  • Cohesion is a useful tool for managing complexity, because when a class code more concentrated when a center of the target, the more likely you are to remember the function code lies.

  • Asked an object that is responsible for what, like asking what the object should hide information, but this problem can bring a broader answer, so that this method is of particular value.

  • Modular goal is to make each subprogram or class looks like a "black box": you know what to go, know what came out, but you do not know what happens inside.

  • Be an effective programmer of a key is that when you develop programs of any part of the code, can ignore the rest of the program as much as possible safely, while class is an important tool to achieve this goal.

  • We say in front of a train, "plus a carriage", so more than one train carriage. (This is what we want simple)

  • SML interface demonstrated by the abstract. Keep the class interface to show the abstract is the same.

  • The various components of the system developers will make some assumptions, and these assumptions do not match between the bug is most deadly and difficult to detect the main source. (Starting with ourselves, do not let yourself develop interfaces exist any assumptions, such as a function to be called after another function)

  • The modular architecture should be, so as not to affect the output part of the business rules and procedures when replaced with a new user interface. For example, the architecture should make it easy for us to do: cut class interactive interface, insert a set of command-line classes. (MVC)

  • Well-designed object-relational the separation of concerns with each other, so that you can focus only on one thing at every moment.

  • Once you understand any other software development technology goals are not important as the complexity of managing many design considerations it becomes a straightforward.

  • When they draw complex objects when, for example, a house, they always go to layers of paint. First, draw the outline of the house, and then draw windows and doors, then draw further details, rather than a brick, a tile, a nail to paint. --Simon (1996)

  • Whenever you find yourself time to learn how to use this class by viewing the class internal implementation, you are not in an interface for programming, but rather for programming for the inside through the interface. If you come through programming interfaces, then the package is broken, and once encapsulation begin the destruction of abstraction will soon suffer. - "Code Complete"

  • Write tasteful code. Come on, do not let your code everywhere if, else, greater than or less, subtraction, multiplication and division. That point is not elegant. They are your panties, essential, but others will not want to see them.

  • Network requests into the class contradictions, class can generate multiple objects, and network requests should clearly be only one, we should not result in exactly the same situation several web server requests annoyance stop, and it will not be happy . Once you've decided to let the class go to a network request, it will appear when you use one or several objects, can not know how many will send you a network request.

  • Declare a variable before you need to identify the scope of variables, you should from its effectiveness point of view, rather than the range it is actually called.

  • Not completely understand the problem, we are not going to modify the code.

  • The larger modifications made to it without understanding the code, the lower the confidence in your work properly on it.

  • You may think you gave a class operational responsibilities of a data table, which is the single responsibility, but the reality is that it can be broken down into "CRUD" four classes, their responsibilities more "single." May God grant you the wisdom to let you tell the right size.

  • The interface does not reduce the duplication of code, but it can regulate the abstract, to reduce the complexity of the program.

  • When you design a program structure, sometimes acts consistent with upper and lower case, then you have to think about it, is only temporary coincidence, or is it redundant layer.

  • Any modification to a class already in use can not be called an extension, although you may only add a new interface for it. Add a new interface means that you have to completely understand how the class is implemented, this extension is designed, can not let you work easily one point. He said the principle of opening and closing the open for extension, but closed for modification, meaning that you can implement new features by adding some class, rather than adding new interfaces to existing classes. This means that, beginning in the design, you have to take your shelf system designed to receive the new class.

  • Code to class for the smallest unit, if you're writing a class, then you should be completely clear about this class. When you write a class, the class of all methods can not be trusted, because it during the construction phase. Scoped variables should be kept to a minimum, when there is need to expand the scope of the class because you totally clear, so you should make clear what variables in the expansion list.

  • Storage class folders explain the functionality can not carry class, such as system / attachment / lower class can not ignore the characteristics of its attachment (accessory) in the naming of the time, because when these classes are provided to customers, he can not see to a folder, programming, he can only see the class name. However, the class name can be used to explain the method.

  • The principle of opening and closing, said that after a class and put into use, it will appear in a lot of places you do not know, modifications (changes to the interface) At this point you made it, will affect many parts of the system. This solution catastrophe is on-off principle, i.e., so that the underlying system can receive the new class, thereby making modifications to the conversion interface is added to implement the new classes. Opening and closing principle also means that any class or method changes will occur in the foreseeable future, there are problems, you should re-design them to meet the principle of opening and closing.

  • "People sometimes ask me, a function of how long it would be suitable in my opinion, the length is not a problem, the key is the semantic distance between the function name and function of the body?" - "reconstruction"

  • Level of abstraction is a class, an interface is an abstract class, an abstract interface to input and output. ( "Yes" with "derived from" is more accurate, but "is" more concise) an interface so doing in relation to the level of abstraction, in other words, what is the role of an interface, depending on their level, class It may be. Interface herein refers to the structure of the function, not the interface

  • If the constructor function or parameter received due to a system error caused by an irrational argument, then the control parameter input function should use the exception handling mechanism.

  • The most important meaning is the definition of the interface specification operation, the semantics of the interface logic used to express each processing element is the most appropriate.

  • In the Java language, a different interface may be implemented with a plurality of classes, so as to constitute a tree system implementation. And each branch a different implementation, in fact, represent the same logic for different semantics Interpretation.

  • Write a class program should be finished, and then wrote a class.

  • A class refined good, depends on two aspects, first, whether the interface is consistent with the abstract; secondly, support for the expansion of adequacy. Extension here does not refer to modify class itself but rather the ability to receive information through different properties exhibit class.

  • The better scalability type, before use, the more of the tool parameters. At different levels of scalability classes which are different. Underlying class requires high scalability to improve its flexibility, and the high-level classes is required with a high degree of cost reduction.

  • Seeking common research method is a method of analogical reasoning, in the process of seeking common ground, the more occasion to observe, the more reliable the conclusions. The construction of a class is a process of seeking common ground. Therefore, the reliability of the class is to pass a test of continuous improvement to improve.

  • In the preparation of the component, it is best to consider whether to good reuse. Disposable components are closely inter-coupling does not matter, but a reusable component should define a clear exposed interface, while not making any assumptions used by the data of its outer layer.

  • Take a look at those good developers have been adhering to do. Stops practicing, doing Developers Conference to participate in practice, there may be some new things constantly get more project experience online learning courses near cattle and efforts to communicate with them participate in the development of some fun, you are interested in the project learning open source framework and library, which is necessary to try to work on my blog, to share technology to the enterprise to do the training

  • To be considered first collaboration between classes when programming (dependent) relationship, followed by the performance. For performance, the following points: 1, the problem is not necessarily your concern bottlenecks; 2, too blind optimization will reduce the efficiency of development, less effective.

  • Functional programming three principles:

    1, declarative programming

    2, pure function

    3, immutable data (to achieve more popular immutable: immutable-js)

    And declarative programming contrast imperative programming, i.e., programming instructions tell the computer how each step of the operation, such as for loops, if-else block, etc., are in general imperative programming. Programming is inseparable from the imperative programming, but imperative programming should be located in the bottom of the code, think lodash you probably understand what I mean.

    Pure function programmed with two characteristics: 1, only the output of the function related to the input, the same input produces the same output (a function not dependent on external conditions other than the argument); 2, the function does not change state in addition to the function or variable domain.

  • Program, use comments to explain the value of the use of magic, magic will result when the value increases or decreases, based on all its interfaces are abstract change, then you need to modify all uses of the comments of the value of the magic interface. This is stupid. You should use enum value instead of magic, so you do not need an abstract interface to maintain the comment, it is maintained by a single enumeration of abstraction. Sometimes, this value is not some kind of magic dictionary, but some kind of id number of the list, in which case maybe you should not use the enumeration, looking discretion.

  • It is proportional to the efficiency and code complexity. In most cases, after optimization can do.

  • Each Exception must be printed to the log once, and only once (who handled Exception, who print, pay attention, not necessarily capture process, it is also possible to throw again, do not print in this case).

  • Knowledge escape, some knowledge has not been effectively controlled within a specific range, leading all need to master this knowledge, there is no authority can stand out and lead to the definition of the knowledge. Knowledge escape a problem is programming important and widespread problem.

Guess you like

Origin blog.csdn.net/sdfgedcx/article/details/90720436
Recommended