First of all , i think the only way to master one skill is learning and practise repeatly.
1. be more specificity
if you are used to writing code in one function, this's especially for you. the code looks as if working well, but it may broke down on one terrible midnight.Do one thing with one mothod. Believe it or not, your code will be traced and debuged easiler by following this rule.
(trace code here)
2.provide metadata
when you'r not providing what data you can proccess and result the caller may get , it just like promise of world peace.Futhermore, when your program broke down, you'll get a unexpected error, right?
so please make sure what you can process and what if failed
(code here)
3.get things done by composing small unit(function for example)
In order to complish one task, we may take many steps. The functor is the step, what you should do is just building block.
the code may look like that:
(code here)
if you'r famliar with linux pipe, you'll be feeling right at home.
read more : (sum-up of the temporary work) link
4. putting the ret into bottle.
Now we talk about the error handling. we traced error by rule 1, but what should i talk to the caller when the program broke down. that's a problem. We decide to put the ret into bottle. what the functor returned is the instance of the container.
For example I create containerA as a bottle that only has one property named _value. Functor will return {_value: xxxx} as success, {_value: 'error, opps'} as failure. cool?
5. chain
put all the methods together, and make the data flow through the methods. it's cool , cause you can compose all this methods arbitrarily, seem like magic .
6.try to keep params unary
when you try to do this, you'll find it's easy to pass options. for example:
// goodfunction registry({ name, password, email = '', sex = 'male'})call: registry({password: 'abcdef', name: 'karl', sex: 'female'})// badfunction registry( name, password, email = '', sex = 'male')call: registry('karl', 'abcedf', undefined, 'female')
when you keep params unary, calling the method could be easier, and the sequence is not important by this way.