I'm new to the functional programming concepts in C#, but I have some experience with higher order functions via Haskell, Scala, Python and Ruby. I'm currently going through an old .NET 2.0 codebase with a LOT of duplication in it. Most of it is simple "copy/paste" with a few variables changed. I'm trying to condense as much of this as possible.
A chunk of code within a routine I'm working on now goes a bit like this, for example:
if (PostProcess.UsesFirstPass)
{
w.WriteLine(V1 + " = 0" + " !! SET FOO");
w.WriteLine(V2 + " = 0" + " !! SET BAR");
w.WriteLine(V3 + " = 0" + " !! SET BAZ");
if (PostProcess.IsSpecial){
w.WriteLine(V3 + " = 20" + " !! INCREASE BAZ");
}
w.WriteLine(V4 + " = 0" + " !! SET QUUX");
}
if (PostProcess.UsesSecondPass)
{
w.WriteLine(V5 + " = 0" + " !! SET FOO");
w.WriteLine(V6 + " = 0" + " !! SET BAR");
w.WriteLine(V7 + " = 0" + " !! SET BAZ");
if (PostProcess.IsSpecial){
w.WriteLine(V7 + " = 20" + " !! INCREASE BAZ");
}
w.WriteLine(V8 + " = 0" + " !! SET QUUX");
}
if (PostProcess.UsesFinalPass)
{
w.WriteLine(V9 + " = 0" + " !! SET FOO");
w.WriteLine(V10 + " = 0" + " !! SET BAR");
w.WriteLine(V11 + " = 0" + " !! SET BAZ");
if (PostProcess.IsSpecial){
w.WriteLine(V11 + " = 20" + " !! INCREASE BAZ");
}
w.WriteLine(V12 + " = 0" + " !! SET QUUX");
}
Where any of the "V"s you see in there is a string variable defined earlier. It's part of a fairly large method. I could write another method called, say, WriteVariables()
and pass in the required info and call it three times, but it only ever applies to this section of method. I think this would be a place where writing an anonymous inner function would make sense.
I did it to test it out, and it seems to work fine. Now I have a single delegate that looks something like:
private delegate void WriteCutVars(string V1,
string V2, string V3, string V4, CuttingParameters cutParam);
// snip //
WriteCutVars WriteVars = (Va, Vb, Vc, Vd) => {
w.WriteLine(Va + " = 0" + " !! SET FOO");
w.WriteLine(Vb + " = 0" + " !! SET BAR");
w.WriteLine(Vc + " = 0" + " !! SET BAZ");
if (PostProcess.IsSpecial){
w.WriteLine(Vc + " = 20" + " !! INCREASE BAZ");
}
w.WriteLine(Vd + " = 0" + " !! SET QUUX");
}
if (Process.UsesFirstPass) WriteVars(V1, V2, V3, V4);
if (Process.UsesSecondPass) WriteVars(V5, V6, V7, V8);
if (Process.UsesFinalPass) WriteVars(V9, V10, V11, V12);
My main questions:
- Is this a proper use of inner functions?
- Is this acceptable when I'm the only CS graduate on a team of mostly mechanical engineers?