1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

So... What's Wrong With "with"?!?

Discussion in 'Programming' started by deathshadow, Jan 28, 2013.

  1. #1
    You know, I've been programming for quite a long time -- coming from an Assembler/DiBol/Pascal/C background I really have to ask...

    What's the blue blazes is with all this recent 'hate' on the WITH statement? I've had several dev's get their panties in a twist over me using it, some noodle doodle idiocy about it being 'deprecated' in javascript (don't even get me STARTED about ecmascript "strict") -- when it's one of the core constructs in traditional languages for clean simple small code.

    I mean, the complaints against it are just idiotic - if you can't keep in mind that it's scope comes first maybe one shouldn't be programming in the first place! Under the hood ALL it should be doing is swapping the element's scope to the top of the list... you can't keep that straight, I can't see how you could keep anything else straight in the code.

    That in JS the alternative recommended is to waste memory on an extra var is just mind-blowing to me... Though with the whole 'objects are always passed as reference' bizarre variable handling... well, that's just the whole "every C syntax language's objects are just shoe-horned in any old way" rearing it's ugly head. Sometimes I don't know which is worse -- PHP, C++, Java or Javascript.

    Eh... maybe I just spent too much time programming before objects, and too much time with objects in Pascal, Oberon and Smalltalk... where pointers, classes and methods actually make sense.

    I mean, is there a legitimate reason for this, or did some name in the industry go "eh, I don't like it" or "it's too hard for me" and everyone did the lemming routine off the cliff?

    -- edit --

    Of course I probably wouldn't even be using it in languages like Javascript if not for this stupid "this" prefix and variables in the object not automatically being dominant in scope.
     
    deathshadow, Jan 28, 2013 IP
  2. Tom-Brown

    Tom-Brown Well-Known Member

    Messages:
    105
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Personally I believe it is for the programmer to decide on what way they go about programming. As long as it doesn't either A) over complicate the programming B) doesn't make the program intensive on certain resources and C) It isn't deprecated.

    I don't use with a lot, but the one programming language which I do use it in (Actionscript 3) I find it incredibly useful. This is mainly because I use pure AS3 and need to repeat <VAR>.<INSERT FIELD> a lot.

    This is sort of like the switch argument (where programmers would argue simply using a series of if or else if was better than using the switch statement). It's just a group of programmers trying to convince another set of programmers to do what they do but in the majority of cases your programming will not be viewed by another unless working in a programming team.

    So in short, it's more opinion unless the programming language is attempting to phase it out and is deprecated.
     
    Tom-Brown, Jan 29, 2013 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    It's redundant, there are no languages left in mainstream use that do not have a good ( or at least, an ) implementation of functions.

    with (Sym) {}
    Code (markup):
    is functionally the same as

    function (Sym){}
    Code (markup):
    They may not be exactly the same, that is beside the point. Most new languages coming along are dynamic, implemented as virtual machines executing abstract syntax trees ... and in that environment, it's pointless to implement the same functionality twice, all for the sake of 1974.

    What I find interesting is that you think it is worth comparing C++, Java, Javascript and PHP at all, in any way shape or form. They don't share the same scope, they aren't used in the same ways, it's like comparing sunset with sunrise, both a spectacle to behold, both entirely different experiences having some irrelevancies in common, and what you prefer doesn't depend on the content of the sun or shade of the sky, but you, the human being involved.
     
    krakjoe, Jan 30, 2013 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    What the devil do functions have to do with 'with'?!?!? do you even know what the WITH statement does? You know, move the scope of a record, struct or object to the top of the lookup?

    Your examples:
    ARE NOT FUNCTIONALLY THE SAME... They're not even on the same planet.

    Lets use a JavaScript object --

    
    test = function() {
      var
        alpha = 'moot',
        beta = 'zoot',
        gamma = 'woot',
        delta = 'poot';
      
      this.showThis = function() {
    		document.write(
    			'<tr><td>' +
    			this.alpha + '</td><td>' +
    			this.beta + '</td><td>' +
    			this.gamma + '</td><td>' +
    			this.delta + '</td></tr>');
      }
      
      this.showWith = function() { with (this) {
    		document.write(
    			'<tr><td>' +
    			alpha + '</td><td>' +
    			beta + '</td><td>' +
    			gamma + '</td><td>' +
    			delta + '</td></tr>');
      }}
    }
    
    Code (markup):
    You have to use THIS because even though they are in scope, they are properties... WITH opens them up to the local scope so you don't have to say 'THIS' every time. Or use a pascal RECORD / C STRUCT or objects...

    
    type
    	tTransaction = object
    		productName:string[31];
    		count:word;
    		costPerUnit,
    		totalCost:double;
    	end;
    	
    procedure testThis(name:string[31]; number:dword; cost:double);
    var
    	test:tTransaction;
    begin
    	test.productName:=name;
    	test.count:=number;
    	test.costPerUnit:=cost;
    	test.totalCost:=number*cost;
    	writeln(
    	'<tr><td>',
    		test.productName,'</td><td>',
    		test.count,'</td><td>',
    		test.costPerUnit,'</td><td>',
    		test.totalCost,'</td></tr>'
    	);
    end;
    
    procedure testWith(name:string[31]; number:dword; cost:double);
    var
    	test:tTransaction;
    begin
    	with (test) do begin
    		productName:=name;
    		count:=number;
    		costPerUnit:=cost;
    		totalCost:=number*cost;
    		writeln(
    		'<tr><td>',
    			productName,'</td><td>',
    			count,'</td><td>',
    			costPerUnit,'</td><td>',
    			totalCost,'</td></tr>'
    		);
    	end;
    end;
    
    Code (markup):
    Simply saying function (this) {} does NOT open them up to local scope. Nowhere NEAR the same thing... though at least in Pascal and C++ if we made those be objects, they'd already be local to scope.

    I'm not sure how you could even put them in the same answer... they have absolutely nothing to do with each-other.

    Except of course they aren't the same functionality, and functions predate WITH -- given that it's one of the most common structures used in Pascal, Modula, C, C++, etc...

    They are all C syntax languages -- they share for the most part the same grammatical structures and syntax. The 'major' differences between them being little more than the available function libraries, how objects were shoehorned into them (Quite often in a half-assed any-old way manner -- see C++, PHP and Javascript), and the occasional slap of paint. You learn one, well... switching between them isn't rocket science as at least the syntax is the same for 90%+ of the language constructs. SWITCH is a SWITCH, curly brackets do the same thing, pre and post increment/decrement are the same, IF, WITH, FUNCTION.... where's the differences again? Minor at best.
     
    deathshadow, Jan 30, 2013 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    You seem focused on syntax like it really matters ... I don't really think it does ....

    I think the last time I actually used it myself would have been vb, many moons ago ... with(symbol){manipulate members} is not really that different to function(object) {manipulate members} now is it ...

    If you're just focusing on syntax it's hardly surprising that you can see similarities ... not many languages have adopted a different style and broken into the mainstream and managed to survive.

    In the examples you gave the differences between them are huge, maybe someone who is focused so intensely on their syntax cannot see it and tars them all with the same brush, but they are very different animals, even their syntax when you really look at them. They share words in common, but the way one declares an object in C++ and in PHP for example are very different, and Javascript is very different to both.
     
    krakjoe, Jan 30, 2013 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    I fail to see how/where you are getting that they are ANYTHING alike... unless you are actually advocating making extra copies or pointers on the stack and adding the entire overhead of a function call to it... and even then if working on a object you'd still have to reference the object being passed... or pass each object parameter separately... kinda defeating the point.

    Just what the devil are you even on about!?! WITH and a function call have NOTHING in common. Not even CLOSE!
     
    deathshadow, Jan 30, 2013 IP
  7. seocm

    seocm Greenhorn

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    With is nice to have... if your languages support it. It certainly helps to cut down the repetitive keystroke hacking.. especially if you have really long classnames.

    However I do c# and unfortunately it doesn't exist.

    The only time ive used with is on VB, so I can see how some code purists might get antsy about "lazy" coders using with...
     
    seocm, Feb 9, 2013 IP
  8. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    818
    Best Answers:
    7
    Trophy Points:
    320
    #8
    If it does your job without breaking anything, why should anyone care HOW the job is done?
     
    mmerlinn, Feb 14, 2013 IP
  9. tyteen4a03

    tyteen4a03 Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #9
    (Please note that I am not an expert JavaScript programmer so if I made any mistakes please point out)

    From my understanding with in JavaScript is largely the same as with in Python... it manipulates an object in that scope. However Python only allows you to shortname the object (i.e with someobjectwithabizarrlylongname as o: doStuffTo(o.somevar)).

    EDIT: Just re-read your post and realized that you made a base assumption that there are no scope problems - in this case ignore what I have said below, I think with have no problems at all.


    I think the problem doesn't come from syntax - it comes from performance.

    The first problem comes from how the practice in JavaScript makes scope-defining confusing. From the Mozilla Developer Resources:
    Con: 'with' makes it hard for a human reader or JavaScript compiler to decide whether an unqualified name will be found along the scope chain, and if so, in which object. So given this example:
    
    function f(x, o) {
      with (o)
        print(x);
    }
    Code (markup):
    In this case, it can take a while to figure out whether you want x or o.x, so slows down the performance for a bit and confuse other programmers.

    Another con explained in the Mozilla Developer talks about performance issues:
    Because the interpreter would look for the variable in the object first then the outer scope, if you have a large object (say the whole jQuery - please don't do this) and is trying to get a variable of the outer scope, it would take a while for the interpreter to search the jQuery object, only to realize that it doesn't exist in that object and look in outer scopes. This lag produced by this lookup may have noticeable effects on performance, though I have not measured.
     
    Last edited: Feb 19, 2013
    tyteen4a03, Feb 19, 2013 IP