www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Does anyone use 'with' statement?

reply serg kovrov <sergk mailinator.com> writes:
Hi,

Recently I have studied a (proprietary language) code. The language 
features 'with' statement in the same sense that D does. To understand 
what this code sample really do I had to remove all 'with's and 
fix/compile code many times before it start working as original.

I don't know why never tried to use it in my own code before, but now I 
definitely will NOT use it ever. Even if it could save me some typing. I 
found 'with' far more evil than even 'goto' (which I don't use as well).

-- serg.
Dec 08 2007
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"serg kovrov" <sergk mailinator.com> wrote in message 
news:fjfchm$u14$1 digitalmars.com...
 Hi,

 Recently I have studied a (proprietary language) code. The language 
 features 'with' statement in the same sense that D does. To understand 
 what this code sample really do I had to remove all 'with's and 
 fix/compile code many times before it start working as original.

 I don't know why never tried to use it in my own code before, but now I 
 definitely will NOT use it ever. Even if it could save me some typing. I 
 found 'with' far more evil than even 'goto' (which I don't use as well).

 -- serg.
It could probably be overused, but it certainly has its uses. I don't use it often, but when I do it usually makes for more elegant and less crufty-looking code. Especially with UI stuff: with(myButton = new Button()) { text = "OK"; position = Point(150, 300); size = Size(75, 40); onClick ~= &okHandler; } Much nicer-looking than the alternatives: myButton = new Button(); myButton.text = "OK"; myButton.position = Point(150, 300); myButton.size = Size(75, 40); myButton.onClick ~= &okHandler; or: myButton = new Button(); myButton.text("OK") .position(Point(150, 300)) .size(Size(75, 40)) .onClick ~= &okHandler; I'd say using more than one level of a 'with' at a time is probably bad form.
Dec 08 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Jarrett Billingsley wrote:
 "serg kovrov" <sergk mailinator.com> wrote in message 
 news:fjfchm$u14$1 digitalmars.com...
 Hi,

 Recently I have studied a (proprietary language) code. The language 
 features 'with' statement in the same sense that D does. To understand 
 what this code sample really do I had to remove all 'with's and 
 fix/compile code many times before it start working as original.

 I don't know why never tried to use it in my own code before, but now I 
 definitely will NOT use it ever. Even if it could save me some typing. I 
 found 'with' far more evil than even 'goto' (which I don't use as well).

 -- serg.
It could probably be overused, but it certainly has its uses. I don't use it often, but when I do it usually makes for more elegant and less crufty-looking code. Especially with UI stuff: with(myButton = new Button()) { text = "OK"; position = Point(150, 300); size = Size(75, 40); onClick ~= &okHandler; } Much nicer-looking than the alternatives: myButton = new Button(); myButton.text = "OK"; myButton.position = Point(150, 300); myButton.size = Size(75, 40); myButton.onClick ~= &okHandler; or: myButton = new Button(); myButton.text("OK") .position(Point(150, 300)) .size(Size(75, 40)) .onClick ~= &okHandler; I'd say using more than one level of a 'with' at a time is probably bad form.
I agree you need to use it with discipline. My rules of thumb are to only use it when 1) the block of code is short 2) basically every line in the block references the 'with' object, bonus points if they're all either LHS or RHS references. 3) the methods being referenced are fairly unambiguous (i.e. not names like 'value' which might be defined by lots of different classes) These are rules of thumb, not hard and fast. The basic question is "will I be able to figure out what this is doing if I come back to this code next year". Which is really what I try to ask myself for all code I write, so 'with' isn't really special in that sense. --bb
Dec 08 2007
parent serg kovrov <sergk mailinator.com> writes:
Bill Baxter wrote:
 These are rules of thumb, not hard and fast.  The basic question is 
 "will I be able to figure out what this is doing if I come back to this 
 code next year".  Which is really what I try to ask myself for all code 
 I write, so 'with' isn't really special in that sense.
My (original) question rather is, will I (average Joe) understand your code (using 'with') without refactoring it? -- serg.
Dec 09 2007
prev sibling next sibling parent Clay Smith <clayasaurus gmail.com> writes:
serg kovrov wrote:
 Hi,
 
 Recently I have studied a (proprietary language) code. The language 
 features 'with' statement in the same sense that D does. To understand 
 what this code sample really do I had to remove all 'with's and 
 fix/compile code many times before it start working as original.
 
 I don't know why never tried to use it in my own code before, but now I 
 definitely will NOT use it ever. Even if it could save me some typing. I 
 found 'with' far more evil than even 'goto' (which I don't use as well).
 
 -- serg.
I don't use it.
Dec 08 2007
prev sibling next sibling parent janderson <askme me.com> writes:
serg kovrov wrote:
 Hi,
 
 Recently I have studied a (proprietary language) code. The language 
 features 'with' statement in the same sense that D does. To understand 
 what this code sample really do I had to remove all 'with's and 
 fix/compile code many times before it start working as original.
 
 I don't know why never tried to use it in my own code before, but now I 
 definitely will NOT use it ever. Even if it could save me some typing. I 
 found 'with' far more evil than even 'goto' (which I don't use as well).
 
 -- serg.
I find the with statement useful in the same sense as auto for generic programming. Name clashes can be a problem however there are always trade offs in programming.
Dec 08 2007
prev sibling next sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
serg kovrov wrote:
 Hi,
 
 Recently I have studied a (proprietary language) code. The language 
 features 'with' statement in the same sense that D does. To understand 
 what this code sample really do I had to remove all 'with's and 
 fix/compile code many times before it start working as original.
 
 I don't know why never tried to use it in my own code before, but now I 
 definitely will NOT use it ever. Even if it could save me some typing. I 
 found 'with' far more evil than even 'goto' (which I don't use as well).
 
 -- serg.
I agree the explicit way is more readable, but I don't have strong feelings against the with statement if the object it's with is referenced in every statement in the block. Goto, used correctly, increase readability, IMO. Having ported some C++ code that used quite a bit of goto over to Java, and the temporary boolean variables used seem less readable to me and often force code to split up into many places.
Dec 09 2007
prev sibling next sibling parent reply Xinok <xnknet gmail.com> writes:
I can see where with statements can make code harder to read. I think 
it's important to make it more explicit, so others reading your code 
don't have to guess where a symbol is coming from.

We could overload the global operator to do just that. It's purpose 
changes when it's used in the with statement.

class N{
	int a, b, c;
}

void main(){
	int a, b, c;
	N obj = new n;
	with(obj){
		a = 35; // main.a
		.a = 60; // obj.a
	}
}
Dec 09 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Xinok wrote:
 I can see where with statements can make code harder to read. I think 
 it's important to make it more explicit, so others reading your code 
 don't have to guess where a symbol is coming from.
 
 We could overload the global operator to do just that. It's purpose 
 changes when it's used in the with statement.
 
 class N{
     int a, b, c;
 }
 
 void main(){
     int a, b, c;
     N obj = new n;
     with(obj){
         a = 35; // main.a
         .a = 60; // obj.a
     }
 }
I'd like to see it be a shadowing error if you try to use 'a' inside with(obj) when 'a' is both a member of obj and the enclosing scope, as above. And I think '.a' is already used elsewhere to mean the 'a' from the _outer_ scope. Maybe I'm imagining that though... I've never actually used it, but I was thinking I read that was D's version of ::a from c++. Note that if you need to differentiate you can also still refer explicitly to "obj.a" inside the with(obj) block. "obj" doesn't cease to exist as a symbol. --bb
Dec 09 2007
parent Xinok <xnknet gmail.com> writes:
Bill Baxter wrote:
 Xinok wrote:
 I can see where with statements can make code harder to read. I think 
 it's important to make it more explicit, so others reading your code 
 don't have to guess where a symbol is coming from.

 We could overload the global operator to do just that. It's purpose 
 changes when it's used in the with statement.

 class N{
     int a, b, c;
 }

 void main(){
     int a, b, c;
     N obj = new n;
     with(obj){
         a = 35; // main.a
         .a = 60; // obj.a
     }
 }
I'd like to see it be a shadowing error if you try to use 'a' inside with(obj) when 'a' is both a member of obj and the enclosing scope, as above. And I think '.a' is already used elsewhere to mean the 'a' from the _outer_ scope. Maybe I'm imagining that though... I've never actually used it, but I was thinking I read that was D's version of ::a from c++. Note that if you need to differentiate you can also still refer explicitly to "obj.a" inside the with(obj) block. "obj" doesn't cease to exist as a symbol. --bb
Yes, '.a' is, I think it's called, the global operator. I was suggesting that we could overload this operator and change it's meaning inside of with statments. What you suggested, "shadowing error", could help avoid a few bugs. However, I don't think it will make the code any easier to read. The problem still exists that you may not know where a symbol is coming from.
Dec 09 2007
prev sibling parent Matti Niemenmaa <see_signature for.real.address> writes:
Xinok wrote:
 class N{
     int a, b, c;
 }
 
 void main(){
     int a, b, c;
     N obj = new n;
     with(obj){
         a = 35; // main.a
         .a = 60; // obj.a
     }
 }
What about when there's an a at global scope: class N { int a, b, c; } int a; void main() { int a, b, c; N obj = new N; with (obj) { a = 35; // main.a .a = 60; // obj.a or global a? } } -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Dec 09 2007
prev sibling next sibling parent Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
serg kovrov wrote:
 Recently I have studied a (proprietary language) code. The language 
 features 'with' statement in the same sense that D does. To understand 
 what this code sample really do I had to remove all 'with's and 
 fix/compile code many times before it start working as original.
 
 I don't know why never tried to use it in my own code before, but now I 
 definitely will NOT use it ever. Even if it could save me some typing. I 
 found 'with' far more evil than even 'goto' (which I don't use as well).
I sometimes use it to bring in a certain template instantiation for multi-member templates, like: with(EuclidianSpace!(3,float)) { Vector a,b; // ... } -- Oskar
Dec 09 2007
prev sibling parent serg kovrov <sergk mailinator.com> writes:
Well, I perfectly understand the intent. My first thoughts regarding 
'with' was it could be used for isolating/grouping of complex objects 
initialization. for example:

with (window = new Window())
{
     Create();
     Move(12, 34);
     Attach(children);
     Show(true);
     Etc();
}

But as everyone agreed so far, 'with' requires discipline. And my 
experience taught me that when it comes to others code, discipline is 
not the thing you can count on.

Speaking of other 'disciplined' software development aspects such as 
manual memory management, manual references management, goto's, etc., it 
could be mitigated by unit and system testing. But still, testing do not 
guaranteed program correctness. And in case of readability I don't see 
means to enforce (or even motivate) 'correctness' just yet.


-- serg.
Dec 09 2007