Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CQG / Задание1 / HDS_VCPP_Programming Rules.doc
Скачиваний:
9
Добавлен:
16.04.2013
Размер:
327.68 Кб
Скачать

Portable Code Localization

Rule 59.Allow for ANSI, MBCS, and Unicode variants in your code.

Rule 60.Put localizable strings in an included resource file.

Portability makes your classes more appealing to other developers. Your classes should allow for ANSI, MBCS, and Unicode variants so that the code is easy to port to other platforms. By using TCHARin your code, you have the flexibility of creating generic and Unicode-enabled builds with a few simple changes in the build definition.

However, TCHARis justcharwhen the code is compiled for MBCS. This means that all the usual MBCS problems are still present. Fortunately, the extended TCHAR.H include file defines various macros that work with all three environments – ANSI, MBCS, and Unicode – if you use theTCHARdata type. See“Multibyte Character Set (MBCS) Survival Guide”by Chau Vu, Seiichi Satoh, and Matt Grove (MSDN Library, Backgrounders) for more information.

If your code contains strings that need to be localized (translated) because they appear in an interface, be sure to put the strings into your resource file rather than coding them elsewhere in an application. In your header and implementation files, use the functions that refer to localizable strings by ID number rather than, for example, supplying a caption as an argument in a function. It is much easier to maintain your resources by ID than referring to resources by their values. See Example 68.

See also “Localization instructions”.

Data Abstraction

Rec. 72.Avoid the direct use of pre-defined data types in declarations.

An excellent way of transforming your world to a “vale of tears” is to directly use the pre-defined data types in declarations. If it is later necessary, due to portability problems, to change the return type of a function, it may be necessary to make change at a large number of places in the code. One way to avoid this is to use standard types of Win16 and Win32 such as, for example, UINT(seeExample 69). You can also declare a new type name using classes or typedefs to represent the types of variables used. In this way, changes can be more easily made. This may be used to give data a physical unit, such as kilogram or meter. Such code is more easily reviewed. (For example, when the code is functioning poorly, it may be noticed that a variable representing meters has been assigned to a variable representing kilograms). It should be noted that a typedef does not create a new type, only an alternative name for a type. This means that if you have declaredtypedef int something, a variable of the typesomethingmay be used anywhere that anintmay be used.

Sizes of Types

Rec. 73.Do not assume that anintand alonghave the same size.

Type Conversions

Rec. 74.Do not assume that pointers and integers have the same size.

Rec. 75.Be careful not to make type conversions from a “shorter” type to a “longer” one.

Rec. 76.Use explicit type conversions for arithmetic using signed and unsigned values.

Processor architecture often forbids data of a given size to be allocated at an arbitrary address. For example, a word must begin on an “even” address for MC680x0. If a pointer to a charis located at an “odd” address, a type conversion from thischarpointer to anintpointer will cause the program to crash when theintpointer is used. It will happen because it violates the processor’s rules for alignment of data.