1

There are several times where you're coding a method which has two parameters with the same type, lets say, integers, NumberOfUnitA and NumberOfUnitB. Given that we're coding in a language that doesn't support named parameters, why is that we don't see people creating types closer to the domain logic, in this case, UnitA and UnitB, but just raw integers? It seems much more expressive to me.

And even in other situations, many times, we have to pass a bunch of flags, and they are just integers (even in cases where perfomance is not an issue), why we do this? Does it create unnecessary complications?

What are the problems of this approach of creating many types?

closed as primarily opinion-based by gnat, Dan Pichelman, GlenH7, Robert Harvey, Michael Kohne Feb 12 '14 at 13:39

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Are you talking about the approach discussed in the tiny types blog post that Jon Skeet also blogged about? – Oded Feb 2 '14 at 12:05
  • Exactly! So, it seems that some people have applied it with success already. – Julio Rodrigues Feb 2 '14 at 15:44
3

The reason I am not doing it as much as I would like to: not enough compiler support.

In Delphi I can create type aliases such as:

type
  TAmount = type Integer;
  THouseNumber = type Integer;
  TCreditAmount = type Currency;
  TDebitAmount = type Currency;

These would make method signatures so much more self dosumenting. Unfortunately, the compiler doesn't force me to use the proper alias as long as the types are assignment compatible. Only if a parameter is a var parameter (passed by reference) does the compiler enforce exact type equality.

Which means that I can forget to use the proper types and won't be alerted. Which means that the code base may slowly start not using TCreditAmount where it should and that detracts from the usefulness of declaring it in the first place.

0

This depends a lot of the language you use.

I Go which has type aliases you can easily create aliases for ints without any notable drawbacks. Other languages don't allow deriving elementar types like int or string at all, (like Dart) because they are strongly optimized for performance and dynamic method dispatch as it's used for polymorphism would decrease performance a lot (but Dart has named parameters though)

  • 1
    Many strongly typed languages allow aliasing elemental types. – Marjan Venema Feb 2 '14 at 13:09
  • I, guess so, and I hate it that I don't know more of them or that I already forgot about because I didn't use them for such a long time. – zoechi Feb 2 '14 at 13:13

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