Best Practices

Type-C offers a lot of features, but it's important to use them wisely. Here are some best practices to keep in mind:

Class or Struct?

The choice between a class and struct is rather straight forward. If you need to model hidden state, use a class. If the fields are independent of each other, use a struct.

For example:

The fields of the struct are independent of each other, so it makes sense to use a struct.

The len field is dependent on the data field, so it makes sense to use a class, and encapsulate the logic to update the len field for example

Classes vs Interfaces

Type-C encourages interface usage, a lot. It is recommended to program around interfaces for a vareity of reasons, but most importantly, flexibility. Hence, it is recommended to use factory pattern for classes rather than new keyword, and return an interface instead.

Example:

This pattern is heavily used in the standard library, and is recommended. It is not recommended to make constructors private without reason.


Kudos! Keep reading!