mercredi 25 mars 2015

To KISS or not when coding?


I don't consider myself "ninja" but I don't think I'm bad dev either. Anyway, after many years of development I tend to keep it simple more ofthen than before. For small projects there is no need to bring MVVM and all boilerplate code, OnClick() will work..


Recently one of my developers wrote this code (C# ASP.NET)



public static class PageHelper
{
public delegate TResult Func<in T1, T2, out TResult>(T1 arg1, out T2 arg2);

public static Func<string, int?, bool> NullableInt32Parser
{
get
{
return (string s, out int? int32) =>
{
int result;

if (int.TryParse(s, out result))
{
int32 = result;
return true;
}

int32 = null;
return false;
};
}
}

public static Func<string, Guid?, bool> NullableGuidParser
{
get
{
return (string s, out Guid? guid) =>
{
Guid result;

if (Guid.TryParse(s, out result))
{
guid = result;
return true;
}

guid = null;
return false;
};
}
}

public static T GetRouteParameter<T>(HttpRequest request, string name, Func<string, T, bool> stringToOutputTypeParser)
{
if (request == null || name == null) return default(T);

var valueString = request.RequestContext.RouteData.Values[name] as string;
if (valueString == null) return default(T);

T value;

return stringToOutputTypeParser(valueString, out value) ? value : default(T);
}

public static int? GetInt32QueryStringParameter(HttpRequest request, string name)
{
if (request == null || name == null) return null;

var valueObject = request.QueryString[name];
if (valueObject == null) return null;

int value;

return int.TryParse(valueObject, out value) ? (int?)value : null;
}
}


Delegates, generics, etc - all to get query string params parsed out. I ended up rewriting it like so:



public static int? GetIntQueryStringParameter(HttpRequest request, string name)
{
int value;
return int.TryParse(request.QueryString[name], out value) ? (int?)value : null;
}

public static Guid? GetGuidQueryStringParameter(HttpRequest request, string name)
{
Guid value;
return Guid.TryParse(request.QueryString[name], out value) ? (Guid?)value : null;
}


What I'm not getting is why developers trying to make it harder? To show they know delegates? To show they know generics? I know that 60% or so of developers won't even understand what's written. So, why do it?





Aucun commentaire:

Enregistrer un commentaire