博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to write elegant code?
阅读量:6622 次
发布时间:2019-06-25

本文共 2185 字,大约阅读时间需要 7 分钟。

hot3.png

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. 

转载于:https://my.oschina.net/wanjubang/blog/675953

你可能感兴趣的文章
Java部署环境搭建(Linux)
查看>>
4.1 在SELinux中客体类存在的目的
查看>>
如何用iPad运行Python代码?
查看>>
PHP学习3——数组
查看>>
E-HPC支持多队列管理和自动伸缩
查看>>
各种设备的CSS3MediaQuery整理及爽歪歪写法
查看>>
CVE-2017-8464远程命令执行漏洞(震网漏洞)复现
查看>>
Java 12 将于3月19日发布,8 个最终 JEP 一览
查看>>
基础为重,Python的基础,成就月薪过万
查看>>
PHP浮点数的精确计算BCMath
查看>>
[起重机监测系统] 1、基于无线传输的桥式起重机的安全监测方案
查看>>
2014年发展计划
查看>>
QQ协议
查看>>
[Android]一个干净的架构(翻译)
查看>>
Oracle RAC安装过程中碰到的“坑”和关键点(一)
查看>>
Jmeter关联
查看>>
java的nio之:java的nio系列教程之Scatter/Gather
查看>>
linux命令之ldconfig
查看>>
Shell之sed命令
查看>>
如何让你的传输更安全——NIO模式和BIO模式实现SSL协议通信
查看>>