Is this good code practice?
-
I used the following code in a programming lesson to help me with a project but the lecturer says it is hard to maintain although I made it like that for better manageability:
static List \_person = new List(); static void Main() { Dictionary methodList = new Dictionary(); methodList.Add("1", (() => \_person.ForEach(s => Console.WriteLine("{0} {1}", s.Name, s.Title)))); \_person.Add(new Person( "James","Prog" )); \_person.Add(new Person( "Claire","Illu")); Console.WriteLine("Press 1 to show all data or 2 to quit"); string input = Console.ReadLine(); if (methodList.ContainsKey(input)) methodList\[input\](); Console.Read(); }
-
I used the following code in a programming lesson to help me with a project but the lecturer says it is hard to maintain although I made it like that for better manageability:
static List \_person = new List(); static void Main() { Dictionary methodList = new Dictionary(); methodList.Add("1", (() => \_person.ForEach(s => Console.WriteLine("{0} {1}", s.Name, s.Title)))); \_person.Add(new Person( "James","Prog" )); \_person.Add(new Person( "Claire","Illu")); Console.WriteLine("Press 1 to show all data or 2 to quit"); string input = Console.ReadLine(); if (methodList.ContainsKey(input)) methodList\[input\](); Console.Read(); }
Was he talking about the lamda stuff? If so, he's right. Rookie programmers would have a learning curve to go through. The lamda stuff doesn't give you anything in terms of speed, so it's rather pointless. If it doesn't improve the speed, obfuscated code like that is nothing more than a hindrance. Of course, you'll find others here that disagree with me.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
Was he talking about the lamda stuff? If so, he's right. Rookie programmers would have a learning curve to go through. The lamda stuff doesn't give you anything in terms of speed, so it's rather pointless. If it doesn't improve the speed, obfuscated code like that is nothing more than a hindrance. Of course, you'll find others here that disagree with me.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001I can see your point however I disagree about lambda being a hindrance, the lambda expression in there is not complicated and saves me the hassle of having to make a method just to display the members which may only happen once. Although I can see that if I needed to display the details in other classes it would not be very productive. Thanks for the reply ! ;)
-
Was he talking about the lamda stuff? If so, he's right. Rookie programmers would have a learning curve to go through. The lamda stuff doesn't give you anything in terms of speed, so it's rather pointless. If it doesn't improve the speed, obfuscated code like that is nothing more than a hindrance. Of course, you'll find others here that disagree with me.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
It gives you speed when compared to overrides.. a switch is much faster though (and in this case probably also clearer) That doesn't mean I disagree, of course.
But we're not talking about overrides. We're talking about populating a dictionary from a list...
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
I used the following code in a programming lesson to help me with a project but the lecturer says it is hard to maintain although I made it like that for better manageability:
static List \_person = new List(); static void Main() { Dictionary methodList = new Dictionary(); methodList.Add("1", (() => \_person.ForEach(s => Console.WriteLine("{0} {1}", s.Name, s.Title)))); \_person.Add(new Person( "James","Prog" )); \_person.Add(new Person( "Claire","Illu")); Console.WriteLine("Press 1 to show all data or 2 to quit"); string input = Console.ReadLine(); if (methodList.ContainsKey(input)) methodList\[input\](); Console.Read(); }
Did your XML generics get swallowed? What exactly is he concerned about?
-
I can see your point however I disagree about lambda being a hindrance, the lambda expression in there is not complicated and saves me the hassle of having to make a method just to display the members which may only happen once. Although I can see that if I needed to display the details in other classes it would not be very productive. Thanks for the reply ! ;)
We're talking about maintenance, not productivity. If you want better maintainability, you shouldn't use the lamda. Adding code complexity with no tangible performance gain is bad practice, IMHO.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
But we're not talking about overrides. We're talking about populating a dictionary from a list...
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
I know, but soon the "anti switch" guerrillas will find the thread and suggest "a beautiful OO solution" :)
Don't you just hate zealots? :)
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
Did your XML generics get swallowed? What exactly is he concerned about?
Thanks for the replies so far! I think my lecturer is assuming that the program that was shown could be "grown" so I did not use switch as I assumed if the system were to grow adding more switch statements would decrease its quality? Is there a better way around this instead of encapsulating the routines into the dictionary?
-
Thanks for the replies so far! I think my lecturer is assuming that the program that was shown could be "grown" so I did not use switch as I assumed if the system were to grow adding more switch statements would decrease its quality? Is there a better way around this instead of encapsulating the routines into the dictionary?
-
That is true I initially used switches but as the project has an "unknown" amount of lookups I thought the latter approach would suit it?
-
Don't you just hate zealots? :)
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001"I know that there are people who do not love their fellow man, and I hate people like that!" -- Tom Lehrer
-
That is true I initially used switches but as the project has an "unknown" amount of lookups I thought the latter approach would suit it?
Perhaps you should have told us what the requirements were. What would he say to a system of plug-ins? :cool:
-
That is true I initially used switches but as the project has an "unknown" amount of lookups I thought the latter approach would suit it?
-
Where does that "unknown" come from? Unknown at compile time? (that would eliminate switch as option..)
It relates to an "unknown" GUI requirement as we have not been told what options we will provide the user with so options 1 and 2 are there for show.
-
I can see your point however I disagree about lambda being a hindrance, the lambda expression in there is not complicated and saves me the hassle of having to make a method just to display the members which may only happen once. Although I can see that if I needed to display the details in other classes it would not be very productive. Thanks for the reply ! ;)
A simpler anonymous method may suffice.
-
It relates to an "unknown" GUI requirement as we have not been told what options we will provide the user with so options 1 and 2 are there for show.
-
Ok thanks for all the comments, it appears that a switch is all that is needed :-D
-
I used the following code in a programming lesson to help me with a project but the lecturer says it is hard to maintain although I made it like that for better manageability:
static List \_person = new List(); static void Main() { Dictionary methodList = new Dictionary(); methodList.Add("1", (() => \_person.ForEach(s => Console.WriteLine("{0} {1}", s.Name, s.Title)))); \_person.Add(new Person( "James","Prog" )); \_person.Add(new Person( "Claire","Illu")); Console.WriteLine("Press 1 to show all data or 2 to quit"); string input = Console.ReadLine(); if (methodList.ContainsKey(input)) methodList\[input\](); Console.Read(); }
If you need that logic in multiple places, there might be some value in putting it in a dictionary structure (e.g., if you need a combination of the values and the logic for selecting which keys depends on where the dictionary is used). That way, you can pass the dictionary around. Then again, you could just wrap a switch statement in a delegate and pass that around (may not work well if there is custom logic to access several keys). In this specific case, I see no reason a switch statement should not be used. It is certainly simpler and I recommend choosing the simpler approach unless there is a real chance you will need the more complex approach later.