How to Write Good API

 1– methods should do only one operation (only one function per method)

this enable -code reuse


-sample for testing

2– keep method small

if method have to make more than function you can encapsulate them in object

3– defensive Programming check on expected arguments to method

Ex.- If not null

– should be string

– should be array

but not validation on argument it should isolated to another method

4- use design patterns if and only if scenario,logic require that

not use design patterns just for using them

5- All types have to be interfaces

-and if you need to add function for this type you can use wrapper(delegates)

-you can wrap you function in new object (use decorator)

6- keep method name descriptive

7- you have to use factory for creating objects

you have not create object of module into another one use factory for this

8- try to provide all information for client code

9- make logging information as a informative conversation (you can report those information later )

10- Anti-if else

you have not use if else ==>use factory to select between state of object

11- null object

1- avoid return null from method

2-at some cases you should use null object which implement interface and have empty

implementation for all method

Ex: you should not pass null to command ,state machine design pattern

12- Object or Variable

1- every thing could be object as long as has it’s behavior ,attribute ,flow to avoid the god class

2- it could be data object which encapsulate data

13- The definition of Done

mean task well documented,tested


About lorddisk

WebCenter Content, WebCenter Portal, WebCenter Sites,Weblogic, Identity and Access Management (IAM),SSO,OAM,OIM,OAAM,OUD, OPAM,OID, OVD ,Oracle API Gateway ,OBIEE,OEDQ, Oracle ADF, Oracle SOA,J2EE, CackePHP ,PHP,J2SE,J2EE,Spring,Hibernate,JQuery,CSS,Java Script ,Joomla,Drupal,Worpress
This entry was posted in Design Patterns. Bookmark the permalink.