With - End with block in C#

J

Jon Skeet [C# MVP]

ozbear said:
"With" is an abomination and should be cast into the pits of hell.

It creates maintenance nightmares and no professional would ever
use it in any language that supports it.

I'm sure you're holding back... why don't you tell us what you really
think? ;)
 
S

Steve Jorgensen

This is a good point, but you have to ask yourself a couple of questions:

1. Which methodology is more explicit? Overall, explicit code is less buggy
and easier to read.

I disagree. Code that says more in fewer words is easier to read. A with
block can be abused, but a reasonably short With block in ol' VB is a very
good thing. It removes duplication which removes a chance of a
hard-to-find typo, particularly when modifying code later.
2. Which methodology is easier to maintain? I find most people that heavily
use "with" in Visual Basic are pretty much being lazy.

Most? I don't know. Almost any good feature can be abused by someone
being lazy. Again, I'm not saying With should be added to C#, but I'd
argue it was more of a good thing than a bad one in VB.
With the OleDb command params, I generally use one of the following
methodologies:

1. Create a code generator based on the stored procedure. A poor man's
version can be created with SQL Query Analyzer and Excel (or Access builder,
et al)

2. Create a generic layer that matches sproc params with a list of some type
(dictionary, hashtable, et al) so I do not have to create the params
programatically.

3. Use the Microsoft.ApplicationBlock.Data project, which gives some neat
overloads to set up params (and caches parameters).

I find, more and more, that I am coding code generators that build the
repetitive code for me. Sure, there are times I have to hand code a few
params, but the coding is simple, so I am not too worried. With saves me
about three keystrokes each line in my coding standard, so it is not a big
deal. If you use less explicit code techniques, you may see a greater
keystroke savings.

All sound like good suggestions to me.
 
S

Shawn B.

Its amazing how most C# programmers (in their humble opinion) view the
"with" as degrading or at least, not adding to, the readibility of code but
most VB programmers think it does. Therefore it doesn't technically work
either way, it's all in the perception of those who want/like/accept the
feature and those who don't want/like/accept the feature, but not a
technical one. It's a philisophical one.


Thanks,
Shawn
 
S

Shawn B.

Why? I"ve never had a problem doing so. It's a great conveniece and have
never in 7 years working with VB suffered a single lost second of
productivity as a result of its presence. I never "rely" on it, but I do
take advantage of the features a tool offers as they are there for a reason.
I've never once in my memory suffered from not being able to read code that
used a "with".

I hear no end to the arguments of C# developers saying how difficult it is
to read. In my experience, I agree, I have worked with many C# and Java
developers who struggle to no end. My take? Adapt and move on. Most VB
programmers don't struggle as much as I've personally witnessed non VB
programmers struggle with it. I'm not making a blanket statement. This is
just an observation based on experience, is all.

Me? I'm primarily an Assembly and C++ programmer, who gets paid well to do
VB and C#. I enjoy all the features of all the languages and often pick the
best tool for the job when given the choice. When not given the choice, I
use the features available.


Thanks,
Shawn
 
S

Shawn B.

It won't because it's not the same language. That never stopped a language
designer from "borrowing" features of other languages. "With" is not unique
to VB. It also exists in JScript and JScript.NET. The syntax of it in
JScript can be improved, though. Nonetheless, that only proves its not a
VB only feature.


Thanks,
Shawn
 
S

Shawn B.

The C# syntax should be closer to:

with( a.parameters ) {
.add...
.add..
}


Thanks,
Shawn
 
S

Shawn B.

See inline:

Cowboy (Gregory A. Beamer) said:
This is a good point, but you have to ask yourself a couple of questions:

1. Which methodology is more explicit? Overall, explicit code is less buggy
and easier to read.
2. Which methodology is easier to maintain? I find most people that heavily
use "with" in Visual Basic are pretty much being lazy.
And if they are, so? Work smarter, not harder. It helps get the project
done faster. I find that most people who use it use it because it's there
and because it requires less typing. That's certainly why I use it. But
overall, I'm more productive compared to many of my peers (not because of
the with statement) but the with statement helps. I don't over use it, I
use it when it makes sense.
 
S

Shawn B.

See inline:

ozbear said:
"With" is an abomination and should be cast into the pits of hell.

It creates maintenance nightmares and no professional would ever
use it in any language that supports it.

I've never in 7 years experienced one second of lost time due to
"deciphering" a with statement code block. Yes, I'm a professional, and I
do use it. I've never seen a VB programmer struggle or increase maintenance
issues due to the presence of a "With" block.

The only people I've ever witnessed struggle with it are C# and more so,
Java programmers who just can't seem to grasp it. Of course, they would
struggle with it. Of course, if it was a feature of the language they would
better understand it. If they were maintaining a VB app, there will be
increased maintenance because they are maintaining code in a language they
don't speak well.

I seem to find more C# programmers complain about how it decreases
readibility and increases maintainibility but I don't see too many VB
programmers (who use the feature) say the same thing. Sure it can be
abused, as can any number of features in C#. That doesn't make C# evil.

There are places in C# where I would greatly welcome it. I create custom
web controls, and in the Render methods, I have scores of lines of code that
repeat the same "HtmlTextWriter..." that could easily save typing by using a
with. I actually did start the project over in VB for reasons of having to
type too much in C# and, even though I'm very profficient in C#, I get the
job done quicker in VB because I have to type less, my hands hurt less from
all the massive typing, and so on. Actually, I think it's the better
intellisense features of VB but I degress. I did not make that choice
because of the lack of a "with" but the point being, I'm going to type less
when I have more to do. Arguing with me that I'm "lazy" and less
"professional" as a result wouldn't hold well if we were programming
together in the real world.

But we all have our opinions. In the opinion of many C# programmers, "with"
is to be abhorred. In the opinion of VB programmers, "with" is a welcome
feature. "With" also exists in JScript, by the way, but its syntax could be
improved.


Flame me for that if you wish. But I do use the language that's going to
help me get where I want to go the quickest. For other projects, I've used
C# because it makes more sense to do so. I enjoy both languages and get
paid to do so.


Thanks,
Shawn
 
D

Daniel O'Connell

Shawn B. said:
See inline:



I've never in 7 years experienced one second of lost time due to
"deciphering" a with statement code block. Yes, I'm a professional, and I
do use it. I've never seen a VB programmer struggle or increase maintenance
issues due to the presence of a "With" block.

The only people I've ever witnessed struggle with it are C# and more so,
Java programmers who just can't seem to grasp it. Of course, they would
struggle with it. Of course, if it was a feature of the language they would
better understand it. If they were maintaining a VB app, there will be
increased maintenance because they are maintaining code in a language they
don't speak well.

I seem to find more C# programmers complain about how it decreases
readibility and increases maintainibility but I don't see too many VB
programmers (who use the feature) say the same thing. Sure it can be
abused, as can any number of features in C#. That doesn't make C# evil.

There are places in C# where I would greatly welcome it. I create custom
web controls, and in the Render methods, I have scores of lines of code that
repeat the same "HtmlTextWriter..." that could easily save typing by using a
with. I actually did start the project over in VB for reasons of having to
type too much in C# and, even though I'm very profficient in C#, I get the
job done quicker in VB because I have to type less, my hands hurt less from
all the massive typing, and so on. Actually, I think it's the better
intellisense features of VB but I degress. I did not make that choice
because of the lack of a "with" but the point being, I'm going to type less
when I have more to do. Arguing with me that I'm "lazy" and less
"professional" as a result wouldn't hold well if we were programming
together in the real world.

But we all have our opinions. In the opinion of many C# programmers, "with"
is to be abhorred. In the opinion of VB programmers, "with" is a welcome
feature. "With" also exists in JScript, by the way, but its syntax could be
improved.


Flame me for that if you wish. But I do use the language that's going to
help me get where I want to go the quickest. For other projects, I've used
C# because it makes more sense to do so. I enjoy both languages and get
paid to do so.

I used With in VB and don't regret it because I used it carefully, in only
some small circumstances. But I don't particularly care for it when coders
start writing long (or worse, nested) with statements to the point I cannot
determine what exactly is going on on a single screen

I simply don't think its something that C# needs, its not *THAT* important,
other features would be more valuable
 
A

Alvin Bruney

Out of curiousity. What do you do that requires assembler?

I have a mental block to that language. It reminds me of D's and E's in
compsci. Forced to learn it to master the debugger, but still hate its guts.
 
S

Shawn B.

I program windows apps as a hobby in x86 asm. On another note, I program
SNES (16-bit 65c02) in assembly and the original 8-bit NES in asm. But I
enjoy (as a hobby) win32 programming in asm. Nothing I do requires it. But
I do it because I enjoy making large business applications that consume
about 30MB in C++/or VB, but 300k in asm.


Thanks,
Shawn
 
J

Jon Skeet [C# MVP]

And if they are, so? Work smarter, not harder. It helps get the project
done faster. I find that most people who use it use it because it's there
and because it requires less typing. That's certainly why I use it. But
overall, I'm more productive compared to many of my peers (not because of
the with statement) but the with statement helps. I don't over use it, I
use it when it makes sense.

While I agree with laziness being generally a good thing in programmers
(look at Larry Wall's suggestions for a good Perl programmer), I don't
think that "it requires less typing" is *ever* a good enough reason in
itself for doing anything with code. Readability has to come absolutely
first, IMO.

Admittedly I'm not *quite* hard-working enough to always write braces
after if blocks:

if (x)
{
doSomething();
}

but I think in general it's a good idea.

On the other hand, I *do* include white space for the sake of
readability even though it means more typing. I *do* format my code for
readability even though it means more work. I *do* comment my code even
though it means more work. I *do* use Convert.ToString(...) instead of
""+... even though it's slightly longer.

Eventually, the time it takes to type the code is likely to be nearly
insignificant in the course of the lifetime of the code - I'll usually
spend more time thinking about it, debugging it to get it working in
the first place, and maintaining it later on. Anything that helps with
*those* tasks is worth much more than something that gives a short term
typing benefit at the cost of readability - and the nature of "with"
just reduces readability, IMO. It wouldn't be quite so bad if you could
only have one level of "with"-ness (i.e. no nested "with"s) but I still
don't like it in general. I'd still rather give a variable a meaningful
name in one line and use that name in subsequent lines to make it
explicit what's going on.
 
M

mikeb

Shawn said:
Its amazing how most C# programmers (in their humble opinion) view the
"with" as degrading or at least, not adding to, the readibility of code but
most VB programmers think it does. Therefore it doesn't technically work
either way, it's all in the perception of those who want/like/accept the
feature and those who don't want/like/accept the feature, but not a
technical one. It's a philisophical one.

However, as the "Ask a Language Designer" article
(http://www.gotdotnet.com/team/csharp/learn/columns/ask.aspx#with)
points out, this snippet of VB code:

With MyDataStructure.GetButton(44)
.Text = "Hello"
.BackColor = Color.Blue
End With

is equivalent to:

Dim b as Button = MyDataStructure.GetButton(44)
b.Text = "Hello"
b.BackColor = Color.Blue

which you can do in C# like so:

Button b = MyDataStructure.GetButton(44);
b.Text = "Hello"
b.BackColor = Color.Blue


So C# does really have the basic functionality of a "With" construct, it
just has a different syntax. I agree with you that it's a philosophical
(or probably more likely a religious) difference. I don't miss the
"With" construct, which is easy for me, since I've never really been a
VB programmer. But, if MS added it to C#, I wouldn't think the world
had ended.

For me, I think it boils down to:

if you really, really need With/End With, use VB.NET

if you want With/End With functionality in C#, create a
short-lived, short-named object and deal with that.


.... snipped old thread ...
 

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