T
T
trurl1232013-07-20 08:50:01
Programming
trurl123, 2013-07-20 08:50:01

boolean property or function

We had a dispute at work about adding a boolean property in C#.
Suppose we have a class responsible for storing authorization parameters. Authorization can be by login or by certificate.
The dispute is whether to add a property to determine the type of authorization, or to focus in the code on the completion of the certificate field.
The class is further serialized to xml.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ruslan Lopatin, 2013-07-20
@lorus

From the point of view of code evolution, it is safer to use, for example, enumerations (enum). Not “more correct”, but “safer”, in the sense of future modifications.
Suddenly (for sure?) You will need additional authentication methods (OAuth for example)? In the case of using enums (or other analogues of the "visitor" pattern - such as case classes from Scala), the development environment will tell you where you forgot to take into account the new authentication option. I don't know about Visual Studio, but Eclipse or IDEA will give a warning in the following code:

switch (principal.getAuthMethod()) {
case LOGIN: /* Login / password auth */
    break;
case CERTIFICATE: /* Certificate auth */
    break;
}

as soon as a new authentication type (OAUTH) is added. This will let you know that you forgot to account for the new way somewhere else.
So if the IDE helps in such a matter, then why not take advantage of it?

E
EugeneOZ, 2013-07-20
@EugeneOZ

The fullness of the certificate field indicates only the certificate, it cannot speak about the type of authorization. It can only indirectly hint, and this hint, by coincidence, can be true - but you can’t rely on it 100%, especially considering the possible evolution of the code.

M
mayorovp, 2013-07-20
@mayorovp

It is ideologically more correct to use inheritance - to introduce the field of the abstract class "authorization data", and two derived classes - "authorization by login" and "authorization by certificate".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question