There are 6 tricks like below:
- Command Query Separation
- Law of Demeter
- Death By Arguments
- Starter Pattern
- Boy Scout Rule
- Single Level of Abstraction Principle
- It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both.
- Command: change the state of a system, but do not return a value.
- Query: return a result, and do not change the state of the system.
Law of Demeter
- The Law of Demeter (LoD) or principle of least knowledge, can be succinctly
summarized in each of the following ways:
– Each unit should have only limited knowledge about other units: only units "closely" related to the current unit.
– Each unit should only talk to its friends; don't talk to strangers.
– Only talk to your immediate friends.
- The Law of Demeter (LoD) or principle of least knowledge, can be succinctly
summarized in each of the following ways:
– Each unit should have only limited knowledge about other units: only units "closely" related to the current unit.
– Each unit should only talk to its friends; don't talk to strangers.
– Only talk to your immediate friends. - Problem: when you ask the customer to pay, the customer gives you his wallet and you get a certain amount of money from his wallet!
- Solution: change the processing process of payment to customer.
- The best functions have as few arguments as possible (niladic > monadic > dyadic > triadic > polyadic).
- It also makes it all too easy to pass arguments in the wrong order with little type safety, and can reduce readability of the code.
- They are hard to read and hard to understand till you double take on the function signature every time you call it.
- It’s difficult to write all the test cases ensuring all the various combinations of arguments work properly
- Death By Booleans:
– When you pass a boolean into a function, you are declaring that you’ve written a function that does two things: one thing for the true case and another for the false case.
– Instead, you should have written two functions, one for each case.
- We usually construct our objects in one place. It could be a constructor, builder, static factory method, abstract factory or any of the other Creational Patterns.
- There are times when objects created by a framework need some additional state that we need to set. Once such example, in Android, is the Activity class. The Activity is created by the Android framework and we can only add attributes via an Intent.
- To start this TargetActivity from various entry-points across many classes and had startActivity(intent) written everywhere >> This violated DRY: Don’t Repeat Yourself.
- To avoid disappointment at run time, it’s nice to define a contract within the target class itself. Use a static starter method for this:
Boy Scout Rule
- Leave the campground cleaner than you found it.
- The Broken Window Theory
- Software rot starts with one broken window.
– One broken window, if left unrepaired for a substantial amount of time, so another window gets broken.
– Serious structural damage begins.
– In a relatively short time, the building becomes damaged beyond the owner’s desire to fix it, and the sense of abandonment becomes reality. - Don’t leave “broken windows” unrepaired. Fix each one as soon as it is discovered.
- Leave the code cleaner than you found it.
- Functions should do just one thing, and they should do it well.
- Code within a given segment/block should be at the same level of abstraction.
- If the line of codes does not directly implement the single responsibility of the
function, it is another level of abstraction. - For example, if function name is saveChangedCustomers() and takes a list of ALL customers as a parameter, and the single responsibility is to save any customers in the list that have been changed.
0 nhận xét:
Đăng nhận xét