0

I'm currently reading "Code Complete" by Steve McConnell. In section 12.9 "Creating Your Own Types (Type Aliasing)", in the pre-last paragraph of "Guidelines for Creating Your Own Types" part there are such words:

For example, you can define a type INT32 and use it instead of int, or a type LONG64 instead of long. Originally, the only difference between the two types would be their capitalization. But when you moved the program to a new hardware platform, you could redefine the capitalized versions so that they could match the data types on the original hadrware.

Could you provide some examples of applying that concept when you move one hardware platform to another? I mean, it's a little bit hard to understand for me, because I haven't had any hardware experience yet. And I don't understand how to apply that concept when you move from one "platform" or "ecosystem" like Node.js to another (Java, Python, etc.). Thank you.

1 Answer 1

3

Let's take the C programming language as an example.

In C, the size of the type int depends on the platform. On some platforms / compilers, it might be 16 bits, on others it might be 32 or 64 bits or something else.

So if you write your program for a specific platform where int is 32 bits, then it might not compile and run if you recompile the same source code on a different platform where int is 16 bits.

What the quote says, is that it might be a good idea to define, for example, INT32 as an alias for int:

typedef int INT32;

And then use INT32 everywhere in your program instead of int.

If you then want to compile your code on a platform where int is 16 bits, and for example long is 32 bits, then you would have to change just that one line:

typedef long INT32;

And the program would work on the new platform.

With "platform" what is meant here is hardware and/or operating system and/or compiler, not a different language (Java vs Python etc.).

Example from my own experience: Long ago I learned programming in C on the Amiga. There were two C compilers you could get for it: Aztec C and Lattice C. On Aztec C, an int was 16 bits, while on Lattice C it was 32 bits. By using typedefs like this, you could write code that would compile the same on both compilers with minimal changes.

2
  • 1
    In fact, this idea is such a good idea that it was added to the C standard library, in the form of the [u]int{fast, least, }{8, 16, 32, 64}_t types. Dec 7, 2021 at 12:54
  • 3
    It should be added that if you program in languages like Python, Java etc., this issue does not arise, because it's the interpreter/virtual machine that's really running your code which has that problem, and they've already solved it and hidden the difficulty from you. Dec 7, 2021 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.