|
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
|
|
|
|
|
A simpler anonymous method may suffice.
|
|
|
|
|
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 know, but soon the "anti switch" guerrillas will find the thread and suggest "a beautiful OO solution" ![Smile | :)](https://www.codeproject.com/script/Forums/Images/smiley_smile.gif)
|
|
|
|
|
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
|
|
|
|
|
PIEBALDconsult wrote: Tom Lehrer
One of my heroes!
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
You rang?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
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?
|
|
|
|
|
More switches? Why not just add more cases to a big switch?
|
|
|
|
|
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 | :cool:](https://codeproject.freetls.fastly.net/script/Forums/Images/smiley_cool.gif)
|
|
|
|
|
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.
|
|
|
|
|
Well if it's just that you could just go back and add some cases.. right?
|
|
|
|
|
Ok thanks for all the comments, it appears that a switch is all that is needed ![Big Grin | :-D](https://codeproject.freetls.fastly.net/script/Forums/Images/smiley_biggrin.gif)
|
|
|
|
|
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.
|
|
|
|
|
You might want to move some of this code out of your main method and into other methods.
For e.g.
thebuzzwright wrote: methodList.Add("1", (() =>
_person.ForEach(s => Console.WriteLine("{0} {1}", s.Name, s.Title))));
might go into a readInput() method.
thebuzzwright wrote: person.Add(new Person( "James","Prog" )); _person.Add(new Person( "Claire","Illu"));
could go into loadData() or something like that.
thebuzzwright wrote: string input = Console.ReadLine(); if (methodList.ContainsKey(input)) methodList[input]();
could go into a method called userResponse().
If any of these methods are modified later, these logical groups might make it easier for others to understand what is done where in the program.
modified on Saturday, May 1, 2010 12:39 AM
|
|
|
|
|
I see the point your Lecturer is trying to convey. You are at a learning stage. Hence there will always be possibilities that the code you write will need to be improved/modified/tweaked.
If this is the final code piece and there cannot be any more changes to be made, this code is fine.
However if there is a possibility that someone else may be using your code (as in the case of Open Source/ or in a Production Environment), this code may cause misunderstandings.
Consider this, what happens if you join a company and are given this piece of code and are asked to make some changes. How comfortable would you be? (You understand this code. But any other such code example?)
|
|
|
|
|
As a switch zealot (only kidding), I don't see anything wrong with you using the dictionary - as long as you were doing it for one particular case (you aren't in this case so a switch would be a better choice). If your application was using Dependency Injection with plugins then it could make sense to use the dictionary because you may have more and more plugins so your code would constantly need to be modified.
In this case, it looks like the lecturer is complaining about the lambda. I like Expressions in their place but there comes a time when you need to step back and ask yourself if your code is clean and easy to read.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Thanks for the replies its quite a mixed view from what im getting so far, from what I understand I should use a switch but if it were to be like a component "plug in and play" style I would use the approach I am using so far.
Thanks again ! ![Big Grin | :-D](https://codeproject.freetls.fastly.net/script/Forums/Images/smiley_biggrin.gif)
|
|
|
|
|
There are two concerns really, and perhaps your lecturer didn't clearly state which he's concerned about (or both?):
0) Switch vs. Dictionary of delegates
1) Regular Method vs. Anonymous Method vs. Anonymous Method with lambda
As written, I would go with the switch, as that would also eliminate both concerns.
However, when you consider growth, the Dictionary may be the way to go. But before you get there you should make your code more modular (as suggested by Abhinav S).
In a command-line-oriented application I'm working on I have an OperationMap class that is a Dictionary of Dictionaries of delegates. The delegates all refer to "regular" methods in the API.
Here's most of the Run method of my command line interpreter:
Command cmd = null ;
do
{
try
{
cmd = GetCommand ( interactive ) ;
OperationMap [ cmd.Operation ] [ cmd.Entity ]
(
this.api
,
cmd
) ;
}
catch ( System.Exception err )
{
System.Console.WriteLine ( err ) ;
}
}
while ( cmd.Operation != Operation.Exit ) ;
Notice how things like getting the command from the user is done by another method rather than muddying up this method. Modularity, separation of responsibility, encapsulation, etc. are things you should master before trying to use anonymous methods and lambdas.
Also, start thinking of your applications in layers. Generally, your interface should call methods of your API, your API should call methods in your business layer, your business layer should call methods in your data access layer, etc.
In the "real world", rather than using anonymous methods and lambdas, your Dictionary will more likely contain references to methods in the API.
As an excercise, write a class that implements a collection of your Person class and offers a DumpAllToConsole() method, then add a delegate to DumpAllToConsole to your Dictionary.
Oh, and the Person class should probably have an override of ToString that formats itself properly, and maybe a FullName property.
|
|
|
|
|
That is an awesome answer thank you for that !
I was not completely sure of what the exercise meant but I gave it a try:
class Person
{
private string _forName;
private string _surName;
private string _title;
public Person(string forname, string surname, string title)
{
ForName = forname;
SurName = surname;
Title = title;
}
public string ForName
{
get { return _forName; }
set { _forName = value; }
}
public string SurName
{
get { return _surName; }
set { _surName = value; }
}
public string Title
{
get { return _title; }
set { _title = value; }
}
public override string ToString()
{
string fullName = string.Format("{0} {1} {2}", ForName, SurName,Title);
return fullName; ;
}
}
class PersonManager
{
private List<Person> _personList;
public PersonManager()
{
_personList = new List<Person>(10);
_personList.Add(new Person("James", "x", "Prog"));
_personList.Add(new Person("Claire", "y", "Illu"));
}
public void DumpAllToConsole()
{
foreach (Person p in _personList)
{
Console.WriteLine(p.ToString());
}
}
}
class Program
{
delegate void DictionaryDel();
static DictionaryDel _invokeAction;
static Dictionary<PersonManager, DictionaryDel> _action;
static PersonManager _personManager;
static void Main()
{
_action = new Dictionary<PersonManager, DictionaryDel>();
_personManager = new PersonManager();
_invokeAction += _personManager.DumpAllToConsole;
_action.Add(_personManager, _invokeAction);
_action[_personManager]();
Console.Read();
}
}
?
|
|
|
|