H
H
herfleisch2011-06-14 11:36:15
C++ / C#
herfleisch, 2011-06-14 11:36:15

Should I use typedef?

I am only interested in the usefulness of using typedef in terms of further code support. Is it worth writing

typedef int mytype;
mytype newVariable;

instead of simple
int newVariable;
On the one hand, the name of the new type will tell the developer about its purpose, it will be clear that the minutes type is minutes, not hours or something else. On the other hand, information about the parent type is hidden from the developer: range of values, format, etc.
Grady Booch says to use typedef. Robert Martin doesn't even stutter about it. What is your opinion?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
S
susl, 2011-06-14
@susl

use by default. it's better if only because today it's an int, tomorrow it can be a double, and the day after tomorrow si::minutes the main thing is that it's a minute, and not how it is implemented, right?
typedef int minutes;
minutes x;

int x;

M
mark_ablov, 2011-06-14
@mark_ablov

I use typedef only for shorthand.
For example:
typedef std::pair <std::vector <somens::sometype>, anotherns::antohertype> myPair;

V
VBart, 2011-06-14
@VBart

Without a typedef, you most likely won't be able to write a portable application.

B
Bright, 2011-06-14
@Bright

On the one hand, the name of the new type will tell the developer about its purpose, it will be clear that the minutes type is minutes, not hours or anything else. On the other hand, information about the parent type is hidden from the developer: range of values, format, etc.

In your example, everything is clear. Minutes is an unsigned integer type, the range of valid values ​​is from 0 to 59.
IMHO, typedef should be used if it helps to make the code more readable. Above, for example,pointed out such a case.

M
Maxim, 2011-06-14
@Speakus

should be used, if only because:
minutes TravelTime;
much better read than
int TravelTime;

X
xanep, 2011-06-14
@xanep

Everything depends on the specific case. Your question is too abstract. This code says absolutely nothing about what mytype is and how it will be used. The same is true for the "minutes" type. Without understanding how and where it will be used, it is impossible to answer. In the general case, to work with time, it is more convenient to use special classes that can represent time not only in minutes, but also in months-days-hours. As for me, it makes sense to use typedef in such cases: 1. Shorten long types (in C ++ 0x you can use auto for this) 2. For a possible choice between precision and speed / used_memory (between float and double, for example)
typedef int mytype;
mytype newVariable;

Well, for code portability between architectures with different bitness, there are already types size_t, uintptr_t, ptrdiff_t. No need to reinvent the wheel.
>should be used, if only because:
>minutes TravelTime;
>much better readable than
>int TravelTime;
This is from the evil one. Behind the imaginary readability, it is not at all clear whether I can write

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question