Naming variables with fixed point units [closed]

What should I name a variable that has units with a fixed point?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>int herpLimitLo_psig2 = 6000; // 60.00 psig
int derpLimitLoPsigwithtwodigits;
int herpLimitHiPsigfixedtwo;
int herpLimitHi_psig_timesOneHundred;
</code>
<code>int herpLimitLo_psig2 = 6000; // 60.00 psig int derpLimitLoPsigwithtwodigits; int herpLimitHiPsigfixedtwo; int herpLimitHi_psig_timesOneHundred; </code>
int herpLimitLo_psig2 = 6000; // 60.00 psig
int derpLimitLoPsigwithtwodigits;
int herpLimitHiPsigfixedtwo;
int herpLimitHi_psig_timesOneHundred;

Apparently I suck at naming things.

Just herpLimitLo is enough.

Variable names are expensive; you can’t make them too long or your code will be unreadable (after all, excessive repetition of excessively redundant information excessively causes excessive exceeding of the human brain’s excessiveness processing systems if used excessively), but you do want them to be as descriptive as possible. This means that you should name them after their meaning; things like expected value range or value semantics can be expressed through other language features in almost every programming language.

For example, in C, you can use typedef; even if the variable’s “real” type is still int, writing it as fixed_2 herpLimitLo suggests what it is. In a language with more sophisticated type systems, whether dynamic or static, you can define an actual type for your base-2 fixed-point numbers. Depending on the language, you can then even overload common numeric operations to work transparently with such fixed-point numbers.

Some languages will even refuse to compile your code if you do stupid things like trying to directly assign a float to a fixed_2, provided you do in fact define them as different types – no naming convention in the world can do that for you.

2

what about naming the variable according to its meaning in the current context? You should avoid naming variables after their type.

The only important information you should be able to retrieve easily is the interpretation factor. In most cases, there will be only one, so you can define a constant and use it everywhere you need it (and avoid putting it in the variable names).

If you are using a lot of different precisions in your computations, you should use a specific type for fixed point computations.

At least in debug mode, this type should be able to give you the precision of a given variable, and prevent you to do stuff like adding two numbers with different precisions (at least in debug mode). And so you are also free to give your variable a short meaningful name.

example in python:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>speed = FixedPoint(precShift=5, val=64) # --> 2 m/sec
duration = FixedPoint(precshift=2, val=5) # --> 2.5 sec
length = speed * duration # precShift=7, value=320
</code>
<code>speed = FixedPoint(precShift=5, val=64) # --> 2 m/sec duration = FixedPoint(precshift=2, val=5) # --> 2.5 sec length = speed * duration # precShift=7, value=320 </code>
speed = FixedPoint(precShift=5, val=64) # --> 2 m/sec
duration = FixedPoint(precshift=2, val=5) # --> 2.5 sec
length = speed * duration # precShift=7, value=320

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật