Coding tricks

  1. Self-creation factory interface
class X{
 public static X CreateSelf(...){
}
}

  1. leverage the power of constructor
    avoid of below kind of codes :
var p = new Person();
p.Name = p1.Name;
p.Age = p1.Age + 12;

  1. push down the behavior where changes can happen. control the level of inheritance (less than 4).
  2. move up the duplicate defination in the abstraction tree and push down the implementation.
  3. sealed class where nessacery, avoid the abstraction leak where possible.
  4. provide pair services in class.
  5. lazy or preload , yes ,it depends .but make sure you have already think about it.
  6. code readability has nothing to do with object-oriented or functional ,the style of code really matters, readability is all about how fast code can be read then understand .
  7. stop the purpose of making code short or use something new, how to centralize the behaviors, abstract the model&process is where beatiful code are needed.
  8. Attribute is a nice feature in c# where allow meta-data attched into method/property/parameter/class where reflection is not a big deal .
  9. class design is much more than single responsibility ,for example :control abstraction on same level.
  10. hard code is not about seeing any string or constant,but system is hard to make change where need to be abstract away, find out the part are needed to be changed and remain unchanged in system is important.
  11. use bit operation where possible to speed up .
  12. avoid static class ,store procedure, etc those implementations are hard to write unit test.
  13. avoid use ‘new’ keyword to hide base class members
  14. remember to use the power of Liskov
  15. avoid a ‘service layer’ that does nothing but only a call to the layer below.
  16. domain classes should have behavior or property.
  17. tell, but not ask.
  18. least knowledge between classes.
  19. always design for business, and there is no perfect design ever.
  20. property should be allowed to be set in any order.
  21. defensive programming in every method.
  22. catch all exceptions in service layer .customize the type of exception where possible.
  23. make sure log are detail and enough.
  24. properly handle exception in another thread(task, async) .
  25. make it virtual if not sure it will be override or no.
  26. evaluate the linq result ASAP.
  27. make sure all ‘if’ has a ‘else’ even within ‘else’ does nothing.
  28. Avoid method that take a bool flag. usally this method doing more than one thing.
  29. use _ for irrelevant lambda parameters
  30. Use async only for I/O bound Operation, Use multi-thread only for CPU bound Operation.

Author: lanliang

Programmer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s