Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 09, 2007 Does anyone use 'with' statement? | ||||
---|---|---|---|---|
| ||||
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. |
December 09, 2007 Re: Does anyone use 'with' statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to serg kovrov | "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. |
December 09, 2007 Re: Does anyone use 'with' statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to serg kovrov | 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.
|
December 09, 2007 Re: Does anyone use 'with' statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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
|
December 09, 2007 Re: Does anyone use 'with' statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to serg kovrov | 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.
|
December 09, 2007 Re: Does anyone use 'with' statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to serg kovrov | 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.
|
December 09, 2007 Re: Does anyone use 'with' statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to serg kovrov | 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 } } |
December 09, 2007 Re: Does anyone use 'with' statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Xinok | 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
|
December 09, 2007 Re: Does anyone use 'with' statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | 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.
|
December 09, 2007 Re: Does anyone use 'with' statement? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Xinok | 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 |
Copyright © 1999-2021 by the D Language Foundation