Excerpts from The Elements of Programming Style

本文提供了一套编程风格指南,旨在帮助开发者编写清晰、简洁且易于理解的代码。遵循这些规则不仅可以提升代码质量,还能增强代码的可维护性和可读性。包括但不限于使用清晰的变量名、避免冗余代码、合理利用函数和模块化设计等。通过应用这些原则,开发者可以更有效地沟通想法,减少错误,并使代码更容易被团队成员理解和维护。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SUMMARY OF RULES

This summary is designed to give a quick review of the points we covered inthe book. Remember as you read the rules that they were presented in connectionwith one or more examples – go back and reread the pertinent section if arule doesn't call them to mind.

To paraphrase an observation in The Elements of Style, rules ofprogramming style, like those of English, are sometimes broken, even by the bestwriters. When a rule is broken, however, you will usually find in the programsome compensating merit, attained at the cost of the violation. Unless you arecertain of doing as well, you will probably do best to follow the rules.

Write clearly - don't be too clever.

Say what you mean, simply and directly.

Use library functions.

Avoid temporary variables.

Write clearly - don't sacrifice clarity for "efficiency."

Let the machine do the dirty work.

Replace repetitive expressions by calls to a common function.

Parenthesize to avoid ambiguity.

Choose variable names that won't be confused.

Avoid the Fortran arithmetic IF.

Avoid unnecessary branches.

Use the good features of a language; avoid the bad ones.

Don't use conditional branches as a substitute for a logical expression.

Use the "telephone test" for readability.

Use DO-END and indenting to delimit groups of statements.

Use IF-ELSE to emphasize that only one of two actions is to beperformed.

Use DO and DO-WHILE to emphasize the presence of loops.

Make your programs read from top to bottom.

Use IF…ELSE IF… ELSE IF… ELSE… to implementmulti-way branches.

Use the fundamental control flow constructs.

Write first in an easy-to-understand pseudo-language; then translate intowhatever language you have to use.

Avoid THEN-IF and null ELSE.

Avoid ELSE GOTO and ELSE RETURN.

Follow each decision as closely as possible with its associated action.

Use data arrays to avoid repetitive control sequences.

Choose a data representation that makes the program simple.

Don't stop with your first draft.

Modularize. Use subroutines.

Make the coupling between modules visible.

Each module should do one thing well.

Make sure every module hides something.

Let the data structure the program.

Don't patch bad code – rewrite it.

Write and test a big program in small pieces.

Use recursive procedures for recursively-defined data structures.

Test input for validity and plausibility.

Make sure input cannot violate the limits of the program.

Terminate input by end-of-file or marker, not by count.

Identify bad input; recover if possible.

Treat end of file conditions in a uniform manner.

Make input easy to prepare and output self-explanatory.

Use uniform input formats.

Make input easy to proofread.

Use free-form input when possible.

Use self-identifying input. Allow defaults. Echo both on output.

Localize input and output in subroutines.

Make sure all variables are initialized before use.

Don't stop at one bug.

Use debugging compilers.

Initialize constants with DATA statements or INITIALattributes; initialize variables with executable code.

Watch out for off-by-one errors.

Take care to branch the right way on equality.

Avoid multiple exits from loops.

Make sure your code "does nothing" gracefully.

Test programs at their boundary values.

Program defensively.

10.0 times 0.1 is hardly ever 1.0.

Don't compare floating point numbers just for equality.

Make it right before you make it faster.

Keep it right when you make it faster.

Make it clear before you make it faster.

Don't sacrifice clarity for small gains in "efficiency."

Let your compiler do the simple optimizations.

Don't strain to re-use code; reorganize instead.

Make sure special cases are truly special.

Keep it simple to make it faster.

Don't diddle code to make it faster – find a better algorithm.

Instrument your programs. Measure before making "efficiency"changes.

Make sure comments and code agree.

Don't just echo the code with comments – make every comment count.

Don't comment bad code – rewrite it.

Use variable names that mean something.

Use statement labels that mean something.

Format a program to help the reader understand it.

Indent to show the logical structure of a program.

Document your data layouts.

Don't over-comment.

From The Elements of Programming Style by Kernighan and Plauger;McGraw-Hill 1974, 1978 ISBN 0-07-034207-5


From http://users.erols.com/blilly/programming/The_Elements_of_Programming_Style.html


The Network Simulator, Version 3 -------------------------------- Table of Contents: ------------------ 1) An overview 2) Building ns-3 3) Running ns-3 4) Getting access to the ns-3 documentation 5) Working with the development version of ns-3 Note: Much more substantial information about ns-3 can be found at http://www.nsnam.org 1) An Open Source project ------------------------- ns-3 is a free open source project aiming to build a discrete-event network simulator targeted for simulation research and education. This is a collaborative project; we hope that the missing pieces of the models we have not yet implemented will be contributed by the community in an open collaboration process. The process of contributing to the ns-3 project varies with the people involved, the amount of time they can invest and the type of model they want to work on, but the current process that the project tries to follow is described here: http://www.nsnam.org/developers/contributing-code/ This README excerpts some details from a more extensive tutorial that is maintained at: http://www.nsnam.org/documentation/latest/ 2) Building ns-3 ---------------- The code for the framework and the default models provided by ns-3 is built as a set of libraries. User simulations are expected to be written as simple programs that make use of these ns-3 libraries. To build the set of default libraries and the example programs included in this package, you need to use the tool 'waf'. Detailed information on how use waf is included in the file doc/build.txt However, the real quick and dirty way to get started is to type the command ./waf configure --enable-examples followed by ./waf in the the directory which contains this README file. The files built will be copied in the build/ directory. The current codebase is expected to build and run on the set of platforms listed in the RELEASE_NOTES file. Other platforms may or may not work: we welcome patches to improve the portability of the code to these other platforms. 3) Running ns-3 --------------- On recent Linux systems, once you have built ns-3 (with examples enabled), it should be easy to run the sample programs with the following command, such as: ./waf --run simple-global-routing That program should generate a simple-global-routing.tr text trace file and a set of simple-global-routing-xx-xx.pcap binary pcap trace files, which can be read by tcpdump -tt -r filename.pcap The program source can be found in the examples/routing directory. 4) Getting access to the ns-3 documentation ------------------------------------------- Once you have verified that your build of ns-3 works by running the simple-point-to-point example as outlined in 4) above, it is quite likely that you will want to get started on reading some ns-3 documentation. All of that documentation should always be available from the ns-3 website: http:://www.nsnam.org/documentation/. This documentation includes: - a tutorial - a reference manual - models in the ns-3 model library - a wiki for user-contributed tips: http://www.nsnam.org/wiki/ - API documentation generated using doxygen: this is a reference manual, most likely not very well suited as introductory text: http://www.nsnam.org/doxygen/index.html 5) Working with the development version of ns-3 ----------------------------------------------- If you want to download and use the development version of ns-3, you need to use the tool 'mercurial'. A quick and dirty cheat sheet is included in doc/mercurial.txt but reading through the mercurial tutorials included on the mercurial website is usually a good idea if you are not familiar with it. If you have successfully installed mercurial, you can get a copy of the development version with the following command: "hg clone http://code.nsnam.org/ns-3-dev"
03-08
<think>好的,用户让我介绍一下NS-3网络模拟器,版本3。首先,我需要理解用户的需求。他们可能是一位网络工程的学生或者研究人员,想要了解NS-3的基本信息,如何安装、运行以及获取文档。用户提供的材料已经分成了几个部分,我需要将这些内容用中文准确且有条理地转述。 首先,我应该从概述开始,说明NS-3是什么,它的开源特性以及协作开发的模式。然后,构建部分需要解释使用waf工具进行配置和编译的步骤,特别是用户提到的示例命令。运行部分要强调如何执行示例程序,并解释输出结果。文档部分需要列出可用的资源,包括教程、参考手册和API文档,同时提醒用户官方网站的重要性。最后,开发版本部分要提到使用Mercurial获取最新代码,并引导用户查看相关文档。 需要注意的是,用户特别要求使用中文回答,并且要按照系统级指令中的格式要求,比如数学表达式用$...$,独立公式用$$...$$。不过在这个问题中,可能涉及数学公式的地方不多,但需要确保LaTeX语法正确,比如变量或代码片段的正确标注。 另外,用户提供的材料中有一些具体的命令和路径,比如./waf configure --enable-examples,这些需要准确无误地转达,避免拼写错误。同时,用户可能对NS-3的用途和优势不太清楚,所以应该简要说明它是一个离散事件网络模拟器,用于研究和教育,支持社区贡献。 在结构上,应该按照用户提供的目录顺序来组织回答,保持每个部分的清晰。可能还需要补充一些连接词,使各部分内容过渡自然。例如,在构建之后,运行示例程序时,可以提到构建成功后才能运行,这样逻辑更连贯。 还有,用户提到的文档部分需要详细说明各个资源的获取方式,特别是官方网站的重要性。可能用户在实际操作中会遇到问题,所以引导他们查看更详细的教程或参考手册是必要的。此外,开发版本部分需要说明使用Mercurial工具,这对于不熟悉版本控制的用户可能需要进一步解释,但根据用户提供的材料,可以简要提及并推荐查看相关文档。 最后,检查是否覆盖了所有用户提供的要点,确保没有遗漏,比如开源项目、构建步骤、运行方法、文档获取和开发版本的使用。同时,使用口语化的中文,避免过于技术化的术语,使内容更易于理解。例如,将“discrete-event network simulator”翻译为“离散事件网络模拟器”并适当解释其用途。 总结来说,回答需要结构清晰,分点说明,准确转述用户提供的材料,并适当补充解释,帮助用户全面了解NS-3的基本信息和使用方法。</think>以下是关于网络模拟器NS-3的详细介绍,按照您提供的材料结构化说明: $$$$ ### 1. 概述 NS-3是一个**开源离散事件网络模拟器**,专注于网络仿真研究与教育。其特点包括: - 通过社区协作开发模型,鼓励用户贡献代码(贡献流程见[ns-3贡献指南](http://www.nsnam.org/developers/contributing-code/)) - 模拟结果可用于学术论文或实际网络设计验证 - 核心功能通过C++库提供,用户通过编写程序调用这些库构建仿真场景 $$$$ ### 2. 构建NS-3 #### 环境要求 - 支持平台:Linux/macOS/Windows(具体兼容性见`RELEASE_NOTES`文件) - 依赖工具:Python环境 + `waf`构建工具 #### 构建步骤 ```bash # 1. 配置项目(启用示例程序) ./waf configure --enable-examples # 2. 编译代码(生成文件保存在build/目录) ./waf ``` $$$$ ### 3. 运行示例 ```bash # 运行简单全局路由示例 ./waf --run simple-global-routing ``` - 输出文件: - `simple-global-routing.tr`:文本格式的追踪文件 - `simple-global-routing-xx-xx.pcap`:二进制抓包文件(可用`tcpdump -tt -r filename.pcap`解析) $$$$ ### 4. 文档资源 | 文档类型 | 访问方式 | |---------------------------|--------------------------------------------------------------------------| | **新手教程** | [官方教程](http://www.nsnam.org/documentation/latest/) | | **模型库说明** | 包含所有内置模型的详细说明 | | **API参考手册** | [Doxygen生成文档](http://www.nsnam.org/doxygen/index.html) | | **社区Wiki** | [用户贡献技巧](http://www.nsnam.org/wiki/) | $$$$ ### 5. 开发版本获取 1. **安装Mercurial**:需先安装版本控制工具(详见`doc/mercurial.txt`) 2. 克隆代码库: ```bash hg clone http://code.nsnam.org/ns-3-dev ``` $$$$ #### 关键注意事项 - 所有数学公式需用$...$包裹,例如网络延迟计算:$Delay = \frac{PacketSize}{Bandwidth}$ - 建议首次使用者从`examples/routing`目录的示例开始学习 - 官方维护的最新文档始终优先于本地文档参考 如需进一步了解特定模块(如TCP拥塞控制算法实现),可查阅对应模型的Doxygen文档条目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值