new
is the new goto
.
Recall why goto
is so reviled: while it is a powerful, low-level tool for flow control, people often used it in unnecessarily complicated ways that made code difficult to follow. Furthermore, the most useful and easiest to read patterns were encoded in structured programming statements (e.g. for
or while
); the ultimate effect is that the code where goto
is the appropriate way to is rather rare, if you are tempted to write goto
, you're probably doing things badly (unless you really know what you're doing).
new
is similar — it is often used to make things unnecessarily complicated and harder to read, and the most useful usage patterns can be encoded have been encoded into various classes. Furthermore, if you need to use any new usage patterns for which there aren't already standard classes, you can write your own classes that encode them!
I would even argue that new
is worse than goto
, due to the need to pair new
and delete
statements.
Like goto
, if you ever think you need to use new
, you are probably doing things badly — especially if you are doing so outside of the implementation of a class whose purpose in life is to encapsulate whatever dynamic allocations you need to do.