没有合适的资源?快使用搜索试试~ 我知道了~
erlang 编程规范
5星 · 超过95%的资源 需积分: 9 21 下载量 153 浏览量
2012-05-24
23:09:40
上传
评论
收藏 95KB PDF 举报
温馨提示
试读
27页
很有用的写erlang程序的程序规范。 1.1 Program Development Using Erlang - Programming Rules and Conventions This paper lists some aspects which should be taken into consideration when specifying and programming software systems using Erlang/OTP. It does not attempt to give a complete description of general specification and design activities which are independent of the use of Erlang.
资源推荐
资源详情
资源评论
Program Development Using Erlang -
Programming Rules and Conventions
Copyright © 1997-2011 Ericsson AB. All Rights Reserved.
Program Development Using Erlang - Programming Rules and Conventions 0.9
May 23 2011
Copyright © 1997-2011 Ericsson AB. All Rights Reserved.
The contents of this file are subject to the Erlang Public License, Version 1.1, (the "License"); you may not use
this file except in compliance with the License. You should have received a copy of the Erlang Public License
along with this software. If not, it can be retrieved online at http://www.erlang.org/. Software distributed under the
License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations under the License. Ericsson AB. All Rights
Reserved..
May 23 2011
Ericsson AB. All Rights Reserved.: Program Development Using Erlang - Programming Rules and
Conventions | 1
1.1 Program Development Using Erlang - Programming Rules and Conventions
2 | Ericsson AB. All Rights Reserved.: Program Development Using Erlang - Programming Rules and
Conventions
1 User's Guide
1.1 Program Development Using Erlang - Programming Rules and
Conventions
This paper lists some aspects which should be taken into consideration when specifying and programming software
systems using Erlang/OTP. It does not attempt to give a complete description of general specification and design
activities which are independent of the use of Erlang.
1.1.1 Structure and Erlang Terminology
Erlang systems are divided into modules. Modules are composed of functions and attributes. Functions are either only
visible inside a module or they are exported i.e. they can also be called by other functions in other modules. Attributes
begin with "-" and are placed in the beginning of a module.
The work in a system designed using Erlang is done by processes. A process is a job which can use functions in many
modules. Processes communicate with each other by sending messages. Processes receive messages which are sent to
them, a process can decide which messages it is prepared to receive. Other messages are queued until the receiving
process is prepared to receive them.
A process can supervise the existence of another process by setting up a bi-directional link to it. When a process
terminates, it automatically sends exit signals to the process to which it is linked. The default behavior of a process
receiving an exit signal is to terminate and to propagate the signal to its linked processes. A process can change this
default behavior by trapping exits, this causes all exit signals sent to a process to be turned into messages.
A pure function is a function that returns the same value given the same arguments regardless of the context of the
call of the function. This is what we normally expect from a mathematical function. A function that is not pure is
said to have side effects.
Side effects typically occur if a function a) sends a message b) receives a message c) calls exit d) calls any BIF which
changes a processes environment or mode of operation (e.g. get/1, put/2, erase/1, process_flag/2 etc).
Warning:
This document contains examples of bad code.
1.1.2 SW Engineering Principles
Structure the code into applications
The Erlang/OTP distribution is structured in so called Erlang applications. An Erlang application is not really part of the
Erlang language but it is part of the OTP design principles and there are many tools and API's that expect applications.
Applications have a predefined file structure both for source code, documentation and for the binaries used in runtime.
See also Applications in the OTP Design Principles.
1.1 Program Development Using Erlang - Programming Rules and Conventions
Ericsson AB. All Rights Reserved.: Program Development Using Erlang - Programming Rules and
Conventions | 3
Export as few functions as possible from a module
Modules are the basic code structuring entity in Erlang. A module can contain a large number of functions but only
functions which are included in the export list of the module can be called from outside the module.
Seen from the outside the complexity of a module depends upon the number of functions which are exported from the
module. A module which exports one or two functions is usually easier to understand than a module which exports
dozens of functions.
Modules where the ratio of exported/non-exported functions is low are desirable in that a user of the module only
needs to understand the functionality of the functions which are exported from the module.
In addition, the writer or maintainer of the code in the module can change the internal structure of the module in any
appropriate manner provided the external interface remains unchanged.
Try to reduce intermodule dependencies
A module which calls functions in many different modules will be more difficult to maintain than a module which
only calls functions in a few different modules.
This is because each time we make a change to a module interface, we have to check all places in the code where
this module is called. Reducing the interdependencies between modules simplifies the problem of maintaining these
modules.
We can simplify the system structure by reducing the number of different modules which are called from a given
module.
Note also that it is desirable that the inter-module calling dependencies form a tree and not a cyclic graph.
Example:
Figure 1.1: These are good dependencies.
Figure 1.2: But this is a bad example.
Put commonly used code together into library applications
Commonly used code should be placed into libraries. The libraries should be collections of related functions. Great
effort should be made in ensuring that libraries contain functions of the same type. Thus a library such as lists
containing only functions for manipulating lists is a good choice, whereas a library, lists_and_maths containing a
combination of functions for manipulating lists and for mathematics is a very bad choice.
The best library functions have no side effects. Libraries with functions with side effects limit the re-usability.
Isolate "tricky" or "dirty" code into separate modules
Often a problem can be solved by using a mixture of clean and dirty code. Separate the clean and dirty code into
separate modules.
剩余26页未读,继续阅读
资源评论
- shanso2015-05-01比较难找,我打算翻译后部分吸引做为我们团队的规范
cp_cz
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功