Scope and Coding Style

  • Thread starter Andrew Robinson
  • Start date
A

Andrew Robinson

I have never done this or seen this but thought it might be useful. What do
you think of it? This is a very simplified example but frequenly when
reusing an object, I find myself spending a lot of time insuring that I
don't carry values over from the previous use. This insures that each of
these objects are in their own scope. (I am not looking for a 'using'
statement here since my object doesn't implement IDispose.) I guess a good
alternative would be be to wrap each of these entities in a different sub
method.

public void SavePhone() {
{
PhoneEntity phone = new PhoneEntity();
phone.Number = "123";
phone.Save();
}

{
PhoneEntity phone = new PhoneEntity();
phone.Number = "234";
phone.Save();
}

{
PhoneEntity phone = new PhoneEntity();
phone.Number = "345";
phone.Save();
}

{
PhoneEntity phone = new PhoneEntity();
phone.Number = "456";
phone.Save();
}
}


let the opinions fly!
 
P

Paul E Collins

Andrew Robinson said:
This is a very simplified example but frequenly when reusing
an object, I find myself spending a lot of time insuring that I
don't carry values over from the previous use. [...]
[using separate {} blocks to enforce "sub-local" scope]
let the opinions fly!

I don't think your example is a good example because each block
already does the equivalent of overwriting the previous value with a
new one. You're saying that you might forget to do this; however, you
might equally forget to put the { } braces around one of the blocks,
or make any number of other errors. Ultimately, there's no structure
that will save you from your own carelessness.

I am vaguely reminded of the C comparison idiom where you write (5==a)
rather than (a==5), because (a=5) would do an accidental assignment
whereas (5=a) would be a compile error. (This isn't an issue in C#
because conditions have to be Boolean values.) However, that's really
just a typo, whereas what you mention involves missing out a whole
step in the process, whether it is calling a Clear method or assigning
to a variable.
I guess a good alternative would be be to wrap each of these
entities in a different sub method.

Definitely for your example! If you duplicate the same few lines over
and over, like you've done here, then you're risking a different
problem: suppose the rules for saving a PhoneEntity change, such that
you also have to set the AreaCode property. If you have a method, you
can just change the code in one place. If not, you're going to have to
hunt down all of them and change it in a hundred places. This is very
error-prone and rather annoying.

If you're talking about a case with less repetitive actions, then post
a better example :)

Eq.
 
L

Linda Liu [MSFT]

Hi Andrew,

Sorry that I may not understand your question exactly.
This is a very simplified example but frequenly when reusing an object, I
find myself spending a lot of time insuring that I don't carry values over
from the previous use.

I think you could define different variables for different usage, or define
a variable first and then every time you'd like to use a new instance, you
create a new instance and assign the new instance to the variable .

For example:

PhoneEntity phone1 = new PhoneEntity();
phone1.Number = "123";
phone1.Save();

PhoneEntity phone2 = new PhoneEntity();
phone2.Number = "234";
phone2.Save();

Or

PhoneEntity phone = null;

phone = new PhoneEntity();
phone.Number = "123";
phone.Save();

phone = new PhoneEntity();
phone.Number = "234";
phone.Save();

Hope this helps.

If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Andrew,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top