views:

218

answers:

9

Can you refactor this ?

private void ClearUserDataFields()
{
   var textBoxes = this.Controls.OfType<TextBox>();
   foreach (var txtBoxControl in textBoxes)
   {
       txtBoxControl.Text = String.Empty;
   }
}

Isn't there anyway to do this in one LOC ?

+5  A: 

it looks nice to me, there are no duplications, no code smells (like typeof), etc

dfa
+4  A: 

Sure. I'd spell 'Fields' correctly. :)

DarkwingDuck
Fixed :)
Dumb Questioner
A: 

You could rename it to ClearUserDataFields to fix what is likely to be a typo in the name. Other than that, it looks fine to me.

Daan
+1  A: 
this.Controls.OfType<TextBox>().ToList().ForEach(
   delegate(TextBox t){ t.Text = String.Empty;}
   );

Why on Earth though?

Mark Dickinson
Why? See: http://stackoverflow.com/questions/1221527/which-technology-despite-repeated-gos-do-you-consider-as-write-once-read-never
beggs
What I meant was why do people think that minimum loc is good. It's just some damned guerilla tactic that average developers use to make themselves indispensible. Usually this is accompanied by the same people complaining that their phone always rings on their days off.
Mark Dickinson
And as I posted above, ToList() creates a new list in memory and therefore is a waste of time and resources. A standard foreach is fine.
DarkwingDuck
IEnumerable<T>.ForEach doesn't exist. That's why the ToList method is used. Yes a foreach loop would be better, to read and to run, I think we know :)
Mark Dickinson
@DarkwingDuck : you have a better idea ?(at least for a lambda expression)
Yassir
Also, I don't really buy into Linq for doing stuff to controls. Using the delegate means I could do something less trivial without making really messy Lambda.
Mark Dickinson
@Yassir A better idea is to not use a lambda at all, as others have clearly stated. Its a dumb idea. All the time I see code that calls ToList so developers can put one line of code. If you really want to do that, just call foreach without parenthesis. But it still creates unreadable code. I'd never hire a developer looking for those kinds of shortcuts.
DarkwingDuck
A: 

Well the "this" qualifier is redundant :) you can remove it

and since you have a tag lambda-expressions here you go

private void ClearUserDataFields()
    {
        Controls.OfType<TextBox>().ToList().ForEach(txtBoxControl => txtBoxControl.Text = string.Empty);
    }
Yassir
Converting to a list is an unncessary waste of time and resources. The existing enumerator is fine.
DarkwingDuck
@DarkwingDuck - IEnumerable doesn't give you a public ForEach method, which would hamper this worthy excercise ;-)
Mark Dickinson
@DarkwingDuck CAN YOU MAKE IT WORK WITHOUT CONVERTING IT TO LIST ??
Yassir
Sure, LEAVE IT AS IS. Why the hell would you convert it to a List? Why don't you convert it to a dictionary while your at it, and make all the Key's random numbers. Or perhaps serialise it to XML and query it with Linq to XML? Why wouldn't you do that? Because its a dumb idea.
DarkwingDuck
Yeah, someone had to say it :) +1
Mark Dickinson
@DarkwingDuck : yes you are right but i converted it to list because you can't call ForEach on a IEnumerable :) @Mark Dickinson : You did the same thing i did :)
Yassir
@Yassir - assuming that there was a lot of this code compression going on in a solution. There could well be a lot of local, and private class level variables. Some people like to use undescores to differentiate these, but some people also like to use notepad to do their development. I would say that whilst the this keyword is not required, it is far from redundant.
Mark Dickinson
The "this" qualifier is in no way redundant. It makes it absolutely clear that you're accessing an instance member. Yes, it's five more characters, but it's a compiler-checked aid to readability.
Greg Beech
@Yassir I still don't see why it is so important to call ForEach in this case? Its not. If you really want one line of code, see my answer at the bottom.
DarkwingDuck
+1  A: 
private void ClearUserDataFileds()
{
   foreach (var txtBoxControl in this.Controls.OfType<TextBox>())
   {
       txtBoxControl.Text = String.Empty;
   }
}

or

// think it's slower then previous one because of ToList call
private void ClearUserDataFileds()
{
   this.Controls.OfType<TextBox>().ToList().ForEach(tb => tb.Text = String.Empty);
}
Kamarey
However, I'm not sure if I'd even consider the latter in any way better than the original code due to readability issues - the original or the first example given here are far more readable (at least to me) than a lambda expression.
Amber
Agree, but the second example is still an option.
Kamarey
A: 

Oh, BTW, I love using MVVM so if this was a smart client app, I'd remove the coupling between the clearing logic and the actual controls. Instead you would be clearing your VM properties, which would be bound to the controls.

DarkwingDuck
+7  A: 

Ok because its SO important to see this in one line of code, here's your answer and it doesn't use a ToList():

foreach (var tb in Controls.OfType<TextBox>()) tb.Text = String.Empty;
DarkwingDuck
Just made me win a bet. (argue regarding readability and maintainability)
Dumb Questioner
Hope your all happy now :) +1
Mark Dickinson
+1 best solution :)
Yassir
I thought you snapped for a moment there :))
Dumb Questioner
Sorry if I seemed rude. I'm not sure what the etiquette is for balancing questions that promote bad dev, versus just answering the question. I prefer the former (obviously). :)
DarkwingDuck
+1  A: 
foreach (var control in Controls.OfType<TextBox>()) 
    control.Text = String.Empty;

ToList/lambda/ForEach are really redundant here and especially because this code have strong side effects so no point expressing imperative loop with side effect via a functional call.

Ray