Data Types

博客介绍了编程中各类数值和数据类型的使用要点。包括避免使用“魔法数字”,检查整数除法和溢出,避免浮点数的不当运算和比较,注意字符和字符串的国际化,合理使用布尔变量、枚举类型、命名常量,以及确保数组索引在范围内等。
部署运行你感兴趣的模型镜像

Numbers in General
1. Avoid "magic numbers".
2. Use hard-coded 0s and 1s if you need to.
3. Anticipate divide-by-zero errors.
4. Make type conversions obvious.
5. Avoid mixed-type comparisons.

Integer
1. Check for integer division.
In the real world 10*(7/10)=(10*7)/10 is true, but not in the world of integer arithmetic.
2. Check for integer overflow.
3. Check for overflow in intermediate results.
int A=1000000
int B=1000000
int C=A*B/1000000
In fact, C is -727

Floating-Point Numbers
1. Avoid additions and subtractions on numbers that have greatly different magnitudes
2. Avoid equality comparisons.
3. Anticipate rounding errors.

Characters and Strings
1. Avoid magic characters and string.
2. Watch for off-by-one errors
3. Know how your language and environment support Unicode.
4. Decide on an internationalization/localization strategy early in the lifetime of a program.
5. If you know you only need to support a single alphabetic language, consider using an ISO 8859 character set.
6. Decide on a consistent conversion strategy among string types.

Boolean Variables
1. Use boolean variables to document your program.
2. Use boolean variables to simplify complicated tests.
Samples in P12

Enumerated Types
1. Use enumerated types for readable.
2. Use enumerated types for reliability.
3. Use enumerated types for modifiability.
4. Check for invalid values.

Named constants
1. Use named constants in data declarations.
2. Avoid literals, even "safe" ones.
3. Use named constants consistently.

Arrays
1. Make sure that all array indexes are within the bounds of the array.
2. Thing of arrays as sequential structures.
3. check the end points of arrays.
4. If an array is multidimensional, make sure it's subscripts are used in the correct order.
5. Watch out for index cross talk.
6. Throw in an extra element at the end of an array.

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值