Functional Longing
I am reading the Erlang book right now (post coming as soon as I finish it), but so far I have managed to grasp the idea of heavy use of recursion and functions for everything. It is very different from imperative languages, but I think I can make the shift.
Joe Functional, however, may have some trouble on the other way around. The following were found on production (but I'll not comment any further on their origin):
public static int CharCount(string strSource, string strToCount, bool IgnoreCase) { if (IgnoreCase) { return CharCount(strSource.ToLower(), strToCount.ToLower(), true); } return CharCount(strSource, strToCount, false); }
Let us ignore the inconsistent naming convention, the misleading function name are really think what this does...
public static string ToSingleSpace(string strParam) { int index = strParam.IndexOf(" "); if (index == -1) { return strParam; } return ToSingleSpace(strParam.Substring(0, index) + strParam.Substring(index + 1)); }
The pattern continue, but at least I can hazard a guess about what this does, but I wouldn't want to pipe this post through it.
public static string Reverse(string strParam) { if ((strParam.Length != 1) && (strParam.Length != 0)) { return (Reverse(strParam.Substring(1)) + strParam.Substring(0, 1)); } return strParam; }
"Reverse a string" is something that I like to ask in interviews, but I don't suppose that this implementation will be found sufficient.
Comments
CharCount was actually never called, was it? It misses the exit condition...
The other two examples remind me of my algorithm course at university, just it was C back then, in which these algorithms make at least sense (although they aren't the best implementation)
-Markus
If functional is what you seek, may I suggest F#. I too have the Erlang book and it got me going on the functional paradigm. However since you are a .NET type of guy F# may be just what you are looking for.
I actually decided on F# over Boo for my next side project. The Visual Studio integration is a killer feature. As much as I like coding in vi, I really like coding in Visual Studio.
The CharCount function doesn't even work. It will result in a StackOverflowException... except when IgnoreCase == true, and at least 1 of the string params is null, then we'll get a NullReferenceException...
Cheers,
Stu
It seems that I need to make it clear.
Those are bad functions.
OMG.
Also: what do the people on your team think when they see their bad laundry flapping on the windy streets of the interwebz like this? This can't possibly be a good experience.
I assure you, there was much ridicule about this internally as well.
But this isn't really different from a lot of the bad code samples that I post, I assure you.
Too bad that some of the best pieces are under NDA.
Did you know that you need to prime an ORA DB before it can be used? Creating temporary tables galore and running statistics on them and then dropping them seems to do the trick just fine apparently...
Did I ever mention how I do SQL Injection testing?
"Hi, My name is'; DROP DATABASE Prod; -- "
http://xkcd.com/327/
Comment preview