473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

[with] is evil

VK
In commemoration of 8th Google anniversary my present to Mr.Cornford
:-)

<http://groups.google.com/group/mozilla.dev.tech.svg/browse_frm/thread/5dabd0138008a9ec/>

(useful to read to anyone who may have with(something) {...} in her
code (unless it's a special case of Crockford-Cornford scope
management).

Sep 27 '06 #1
7 1358
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 :)
<http://groups.google.com/group/mozilla.dev.tech.svg/browse_frm/thread/5dabd0138008a9ec/>
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.
Sep 27 '06 #2
Elegie wrote:
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.
http://www.javascripttoolbox.com/bestpractices/#with

I can't think of a good reason to use 'with'. Can you?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 27 '06 #3
VK

Elegie wrote:
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.

Sep 27 '06 #4
Matt Kruse wrote:
>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.
http://www.javascripttoolbox.com/bestpractices/#with
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.
Sep 27 '06 #5
Elegie wrote:
Matt Kruse wrote:
>>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.
>http://www.javascripttoolbox.com/bestpractices/#with

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.
>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();
}
}
<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.
Sep 27 '06 #6
VK

Elegie wrote:
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.htmland 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 :-)

Sep 27 '06 #7
In article <45***********************@news.free.fr>, Elegie
<el****@invalid.comwrites

<snip>
>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
--
John Harris
Sep 29 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mac | last post by:
Under certain circumstances isinstance() seems to return incorrect value for me. I'm using Python 2.3 (latest from Debian's unstable). Here's a sample program... the multi-module nature of the...
7
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed...
14
by: enrique | last post by:
I'm trying to debug my expression that matches an alphanumeric with any number of dashes (including none), except at the ends and obviously disallowing two or more consecutive dashes. Here it...
17
by: ethan | last post by:
Hi All, How to write a macro with variable length of argument? For example, if i need a macro to check return value of printf. #define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end; ...
0
by: dnphamus13 | last post by:
I'm new to this and drowning right now. I would like to put my database online for viewing. I managed to do the filtering but i need to do PAGING as the XML doc get bigger. From what i understand...
18
by: Ed Zagmoon | last post by:
I want to write an evil AI in C++ that will be able to evolve and become a dangerous computer-mastermind, something like Skynet. How can I write such an AI in C++ and how can I make it hate...
20
by: timmg | last post by:
You know, I've had so much fun reading the thread on lookup field's subservience to the Dark One that I thought I'd provoke another, ah, polite discussion on the topic of Macros. I've always...
10
by: jodleren | last post by:
Hi I know, that there are a lot of people having problems with orkut.com, errors like "object expected" and named objects missing. When loading the site can generate some 10 errors, and still...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.