[with] is evil

E

Elegie

VK wrote:

Hi,
In commemoration of 8th Google anniversary my present to Mr.Cornford

According to the archives, Mr Cornford and you seem to enjoy quite an
interesting relationship, so I suppose that out of boredom you're
seeking a new fight :)

This thread shows that you have used a with statement, and have obtained
results you did not expect; as a result, your position is that With
statements should not be used, since they are difficult to read and also
have performance issues.

I will not comment on whether the With statement should be used; to me
it can be useful (especially in the temporary scope chain augmentation
you've been referring to), but it certainly has a particular mechanics,
that you should be familiar with if you intend to (correctly) use it.
Your pointing one aspect of this mechanics will certainly help people
unfamiliar with it to review/correct some of their code, so thank you
for sharing the issue.


<off-topic>

However, I'll comment on that "[with] is evil" title with a short story.
I happened to be, many times, the lead of programming teams. Faced with
newbies, I have realized that coding issues did not come much from their
competence (which could be leveled up) rather than their behavior to
programming.

So I worked a small introductory speech I'll share below, which will be
MY gift for Google 8th Anniversary :)

Inexperienced programmers present four syndromes, from which originate a
great deal of programming errors; a programming error being something
stupid that makes you stay working at night instead of enjoying video
games, for instance. Being aware and recognizing these syndromes will
help you identify situations where you are definitely going to blunder,
and, hopefully, help you to adopt the right behavior in order to NOT
blunder.

[0] is called "Magic". Computer science is deterministic, so each time
you'll be saying "it's evil", or "it works but I don't know why" or
"that damn stuff does not like me" or [long list] then this means you
lack knowledge on a subject, and that instead of acquiring this
knowledge you're trying to appeal to luck to have your stuff work. Don't
do that, extend your knowledge, learn to use the tools correctly, and
get back home earlier.

[1] is called "Don't touch me!". When a programmer's has spent a few
minutes writing something, he'll come to like it, and will refuse to
change/alter it in ANY way (he'd rather have dinner with his
mother-in-law). So if you notice you keep adding up codes to a core that
has nothing to do with it, if you call "patch" a new functionality, then
maybe the core wasn't that great to start with. Think carefully about
it, and never hesitate to start over, we all write stupid stuff
sometimes, and building a house on poor foundations... well, in the end,
you'll get back home earlier.

[2] is called "Just coding". When confronted with a coding task, the
programmer will start at once, will write hundreds of lines during
several hours, only to realize five hours and three coffee later that he
didn't understand anything to the task, and/or he didn't made some
analysis about how to perform the task in the best way. Then he feels
syndrome [1] and tries to miserably patch his work. Don't do that. Think
before coding; make sure you've understood what you have to do, why you
need to do that and what evolutions could be. Then build a flexible
framework. Then have a coffee, and then, only then, start coding. You'll
get back home earlier.

[3] is called "too easy, man!". When faced with a single little task, of
little "flashy" interest, the programmer will underestimate the task and
produce bad code, out of laziness and because he wants to come home
earlier. Then just before leaving, the boss will come and tell him, "Say
I have strange results... Have a good night here!". Never underestimate
tasks, and stay alert; something that looks easy might not be that easy.
You'll really get back home earlier.

So, to make a long story short : "[With] is evil" is definitely syndrome
[0], of course I understand you were making some humor VK, so don't take
it personally - I just stole your topic to tell a poor story;) Rather, I
hope this story will help readers into recognizing faulty behaviors, and
help them get back home earlier.

</offtopic>


Kind regards,
Elegie.
 
V

VK

Elegie said:
According to the archives, Mr Cornford and you seem to enjoy quite an
interesting relationship

Url, vg vf abg Fna Senapvfpb urer, vg'f gur tbbq by' p.y.w. :))
so I suppose that out of boredom you're
seeking a new fight :)

Truly not. I had some statements made before in *support* of using
[with] and I got rather intensive contre-advocating from Mr.Cornford.
As I did not get any real cases prooving his point I stayed on my point
of view. The linked case did change my mind at least in some aspects of
using [with], and as the case is rather easy to be occasionally
reproduced by others I decided to mention it at c.l.j.
I agree that the title is a bit forum-like and polemic: it may be not
clear why so strong term is used right away unless you know the "eval
is evil" slogan story in c.l.j.
 
E

Elegie


Well, the point of using a reference over "with" in your example is
certainly valid and I support it at 100%, however the condemnation of
the WithStatement seems a bit straight to me.
I can't think of a good reason to use 'with'. Can you?

The interest of the WithStatement resides precisely in that scope
alteration; the object being inserted can be anonymous, which offers you
a simple private static area.

---
with({
FOO_CONST_1 : "foo",
makeFoo:function(){alert("Foo!");}
}){

var bar=function(){
alert(FOO_CONST_1);
makeFoo();
}
}
---

Of course you can obtain the same result by using a combination of
function expressions.

---
var bar = (function(){

var
FOO_CONST_1="foo",
makeFoo=function(){alert("Foo");}

return function(){
alert(FOO_CONST_1);
makeFoo();
}

})();
---

Please note that I am not advocating the use of "with" at all costs; I
simply wish to highlight the fact that this feature is very special,
often misused then condemned, but can have a decent usage.


Regards,
Elegie.
 
R

Richard Cornford

Elegie said:
Well, the point of using a reference over "with" in your
example is certainly valid and I support it at 100%,
however the condemnation of the WithStatement seems a bit
straight to me.

I have criticised Matt's "best practice" page in the past for being short
on explanation. I tend to think that if you cannot back a 'best practice'
up with a reasoned justification it is probably unrealistic to expect
people to accept it as a 'best practice'. Injunctions without
justifications rely on an unquestioning trust in the person issuing the
injunctions, and without that are as likely to get peoples backs up as
achieve anything else.

But reasons for not using - with - are pretty well illustrated by VK's
code. When someone is writing javascript with a negligible understanding
of how javascript, and the - with - statement in particular, actually
works the apparent convenience of a particular construct can be gained at
the price of a whole additional class of errors that are hard to identify
and debug.

There is the running issue in questions to this group where people
realise that they can read from the properties of the object added to the
scope chain, and set values on its existing properties, but cannot
understand why they cannot create new properties on that object (or, why
the attempts to do so do not 'work' but also do not error).

Then there is VK's issue, where he does not understand the object he is
adding to the scope chain (cannot see which properties it is expected to
have), or cannot understand the Identifier resolution rules and how the
modification to the scope chain will influence Identifier resolution.
The interest of the WithStatement resides precisely in that
scope alteration; the object being inserted can be anonymous,
which offers you a simple private static area.

---
with({
FOO_CONST_1 : "foo",
makeFoo:function(){alert("Foo!");}
}){

var bar=function(){
alert(FOO_CONST_1);
makeFoo();
}
}
<snip>

That deserves a quick explanation of why: when the function expression is
evaluated it is assigned an internal [[Scope]] property that is identical
to the scope chain at the point when the expression is evaluated. That
is, a scope chain that has the anonymous object at the top of it.

When the function is later executed the scope chain used for its
execution is the chain defined in its [[Scope]] property with the
Variable/Activation object for its execution context added to the top of
that chain. So the anonymous object is then the second object on the
scope chain whenever the function is called.
Please note that I am not advocating the use of "with" at all
costs; I simply wish to highlight the fact that this feature
is very special, often misused then condemned, but can have
a decent usage.

Absolutely. However, by pure coincidence[1] today I did a text search of
the 60,000 lines of client-side code in the web application that I am
currently working on for "with(", to see if any with statements had been
used. There were zero occurrences. So it can have decent uses (and if
anyone is going to be augmenting scope chains with - with - I would be
the one expected to be doing so) but they are few and far between.

Richard.

[1] It was a question of automatic code size reduction, including local
Identifier shortening, which cannot easily be done inside a - with -
statement as Identifiers may be referring to object properties in that
context. The proposed code size reduction software cannot handle the -
with - case safely.
 
V

VK

Elegie said:
The interest of the WithStatement resides precisely in that scope
alteration; the object being inserted can be anonymous, which offers you
a simple private static area.

Jumping on this brunch just to avoid possible misunderstanding like
with the title: Cornford-Crockford scope management and its
with-related section is a serious stuff I excluded right away in my OP
( there are not any between-the-line "so called" or other polemics).

But truthfully if one can code CC scopes in notepad, he's most
definitely doesn't need any help with scope advises or "best practice"
advises. I personally only once afforded the first part
<http://www.crockford.com/javascript/private.html> and still floating
on the first sample at
<http://www.litotes.demon.co.uk/js_info/private_static.html> :)

That doesn't mean of course that you have to know CC or no way to
program on JavaScript (I using other methods I'm finding more
practical). Yet yes, use "with" with caution, but use it, if you know
all outcomes.

That is actually the main discutable point of some of "... is evil",
"... is bad" articles. Most of the time a few actual samples "look what
he did - and look what happened with him as a result" do much more then
the most spooky but abstract arguments.


P.S. Also if Boris Zbarsky says that something is more time/resource
consuming on Gecko script engine, we have all reasons to believe that
it is so :)
 
J

John G Harris

Rather, I hope this story will help readers into recognizing faulty
behaviors, and help them get back home earlier.

What you say is right and good, but sometimes there are other motives :-

Bad news (1) :
Some people get paid for doing overtime. Some of those need the overtime
money more than they need the feeling of a job well done. :-(

Bad news (2) :
Some managers think that rushing around and working all hours to get the
job finished is good, and going home earlier because it worked first
time is bad. Guess who's going to be promoted. :-(

John
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top