Is there any advantage in avoiding type specification redundancy?

Asked

Viewed 122 times

5

Given the following code:

public override Entidades.ControleAcesso.Perfil PreencherEntidade(IDataReader dr)
        {
             return  new Entidades.ControleAcesso.Perfil()
                        {
                            Codigo = FieldSafer.Safe<int>(dr["someCol"], 0),
                            Nome = FieldSafer.Safe<string>(dr["someCol"], "nothing"),
                            PerfilFuncionalidade = new List<PerfilFuncionalidade>(),
                            StatusRegistro = FieldSafer.Safe<StatusRegistroEnum>(dr["someCol"], StatusRegistroEnum.Ativo),
                        };
        }

Where Fieldsafer =

public class FieldSafer : MinhaClasseBase
    {
        private T ObjectSafe<T>(object field, T defaultValue)
        {
            return GetSafeField<T>(field, defaultValue);
        }

        public static T Safe<T>(object field, T defaultValue)
        {
            return new FieldSafer().ObjectSafe<T>(field, defaultValue);
        }
    }

I suffer in case of a redundancy here: Codigo = FieldSafer.Safe<int>(dr["someCol"], 0), where I make mine explicit Safe<int>, where it could be automatically deleted by Safe(dr... and the return (defaulValue) is a whole.

is Correct to suppress redundancy of types? or to facilitate understanding is better to make them explicit?

  • these methods do what? takes a Datareader and turns it into a Class Type ? if it can answer ?

  • Sorry for the delay, yes, these methods receive the return of the Database and turn into an object

  • I have a routine that transfer data coming from the Database read by a datareader is a list, if you want to bring an item also works ... if you think it cool I pass you as answer! @okevinlira

  • Post as Reposta @Fccdias it’s good to have another opinion to evaluate as well. Particularly there are other ways to do this, but I cannot change this structure at my client’s request ;)

1 answer

4


In the cases presented, as the value being passed is a literal, it is very easy to see which type will be inferred by C#... so in this case, I would certainly remove the specification of the type of generic call.

In other cases where there is greater complexity of the value being passed on, depending on the situation, it may be better to leave the explicit types.

Particularly, I never leave the specified types when they can be inferred by the compiler, because Visual Studio lets you know what the type is, simply passing the mouse over the method.

In terms of performance, there is no difference, because when the compiler infers the type, it is as if you had specified the type... the compiled result will be the same. I mean, the difference is just visual.

  • 1

    Man.. If you knew how ninja you were now, editing exactly what I would ask :P

  • 1

    Hehe... I forgot to answer the main part of the question: the title! But now I think it’s all right and coherent. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.