4
There are situations in which we have fields that represent a type of response, whether boolean responses sim
and não
, S
and N
or with multiple responses like the status of something. For example the status of a course certificate analysis provided by some institution:
- Certificate yes
S
- Certificate not
N
- Certificate in analysis
A
Considering the above context, the use of constant can be adopted to facilitate the reading of the code.
Now see a real example of the class Instituicao
which has response type fields in which it is using constants for them:
public class Instituicao
{
public static string DeletadoSim { get; } = "S";
public static string DeletadoNao { get; } = "N";
public static string AtivaSim { get; } = "S";
public static string AtivaNao { get; } = "N";
public static string CertificadoDisponivel { get; } = "S";
public static string CertificadoNaoDisponivel { get; } = "N";
public static string CertificadoEmAnalise { get; } = "A";
public int Id { get; set; }
public string RazaoSocial { get; set; }
public string UrlSite { get; set; }
public string Ativa { get; set; } = AtivaSim;
public string TemCertificadoGratuito { get; set; }
public string Deletado { get; set; } = DeletadoNao;
public DateTime DataCadastro { get; set; }
public List<Curso> Cursos { get; set; }
}
Therefore, the scenario that was illustrated above left me with doubts regarding the appointment and creation of constants.
Doubts
- In cases like this, should we use constants? Is there another alternative? If yes, which?
- In the case of constants, how could I name them in order to avoid prolixity, redundancy or anything that does not leave the code clean for response type fields?
How so constants ? type public const int C1 = 5;
– Amadeu Antunes
@Amadeuantunes think I could put an example of use to make it easy to understand.
– gato
Every programmer is familiar with the boolean type, for two mutually exclusive status the use of boolean is enough. The enumeration is used to designate more than two status (not necessarily mutually exclusive with the use of the [Flags] attribute). The entire underlying type derives from the early programming of microcontrollers, when one had little memory and a low number of instructions per second, so one could compare integers in a single instruction. Today with Ghz and TB memory machines this is no longer a problem.
– user178974
This type of routing can be done with a dictionary without greatly affecting its performance.
– user178974