Is there any advantage in avoiding type specification redundancy?

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 the case of redundancy here: Codigo = FieldSafer.Safe<int>(dr["someCol"], 0), where I leave explicit my Safe<int> , where it could be automatically suppressed by Safe(dr... and the return ( defaulValue ) is an integer.

Is it OK to suppress type redundancy? or to facilitate understanding, is it best to make them explicit?

    
asked by anonymous 09.05.2014 / 15:14

1 answer

4

In the cases presented, since the value being passed is a literal, it is very easy to figure out which type will be inferred by C # ... so in that case, I would surely remove the type specification from the generic call.

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

In particular, I never leave the types specified when they can be inferred by the compiler, because Visual Studio lets you know what type it is by simply hovering over the method.

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

    
09.05.2014 / 16:04