KBD

Keith Devens .com

Wednesday, October 8, 2008 Flag waving
Nonsense on stilts! – Derived from Jeremy Bentham
← Class is overI hate PHP →

Daily link icon Wednesday, August 13, 2003

References and pointers

I really think I'm beginning to dislike languages that don't have an explicit concept of a reference or a pointer. Copies get made when you don't want them, but you have no control over it. Often you wind up having to stick the data you want to take a reference to in an array (since arrays often get passed by reference in these types of languages) so you can get the same effect. It has other consequences as well. All the more reason why I'm looking forward to Perl 6, since Python, even though I dig it, is one of those languages that steals pointers from you. In fact, most of the languages that we're all stuck using do this, including Python, Java, and PHP.

It seems like most the time I spend tracking down bugs in PHP is spent trying to figure out where a copy is being made. Over and frickin over again - that's not a fun way to spend your time.

← Class is overI hate PHP →

Comments XML gif

Simon Willison (http://simon.incutio.com/) wrote:

For a really good understanding of how copying / pointer type things in Python work, take a look at this and this.

∴ Simon Willison | 13-Aug-2003 10:33am est | http://simon.incutio.com/ | #2692

Keith (http://www.keithdevens.com/) wrote:

Some of that was very interesting... especially the thing about default function arguments being created at compile time and not function call time. But I don't think any of it taught me anything about Python's value/reference semantics that I didn't already know.

Keith | 13-Aug-2003 10:43am est | http://www.keithdevens.com/ | #2693

Sam Newman (http://www.magpiebrain.com) wrote:

I HATED pointers in C - well, let me correct that, I still do :-) 80% of bugs in C/C++ programs are down to 2 things in my experience - pointers and memory management. Java is fairly consistent when it comes to pointers and objects - everything is a reference, and nothing gets copied as a result of assignment. I'm not sure how it works with Python (I've only dabbled with it) or PHP (never used it), but I do remember pointers being one of the things that really annoyed me with Perl.

∴ Sam Newman | 13-Aug-2003 11:13am est | http://www.magpiebrain.com | #2694

Adam Vandenberg (http://flangy.com) wrote:

Not to sound like a language advocate, but I've never had a problem with Python copying stuff or forcing me to copy stuff. Once I internalized "Everything is an object, all objects are passed by reference, numbers strings and tuples are immutable" things made sense.

∴ Adam Vandenberg | 13-Aug-2003 12:35pm est | http://flangy.com | #2699

Keith (http://www.keithdevens.com/) wrote:

Adam, I agree. Python usually just does what you mean and you don't have to think about it. But recently I wanted to take a pointer to a string so I could append to it later, but because I couldn't take a pointer to it I had to approach it a slightly different way.

But importantly, in Python or Java, even if something doesn't do what you want or expect, it's typically clear what's going on and you don't waste much time on it. PHP, to the contrary, has wasted so much of my time in this area that it's just pure evil.

Keith | 13-Aug-2003 12:52pm est | http://www.keithdevens.com/ | #2700

Keith (http://www.keithdevens.com/) wrote:

One more thing that I forgot to mention. In languages like Java, Python, etc. it's impossible to point a function argument to a new place because they don't have the concept of a pointer. However, in all of these languages it's easy to return multiple values, so that limitation isn't so hard to get around. But this limitation also leads to kludges like passing in an array so that you can change the elements of the array and have them show up outside the function.

Keith | 18-Aug-2003 12:30pm est | http://www.keithdevens.com/ | #2747

Raphael wrote:

I totally agree with your complain. I think that there was a real error of conception in all these modern langages (Java, Python, Javascript, Perl, Ruby ??) of getting rid of pointers.
I think that's because pointers are often understood for their "technical" need, instead of their "functionnal ones". The conceptors of these new langages just said : No need to use pointers anymore, as we handle memory allocation ourselves. Ok, that was the technical part of what pointers are used for ...
Ok, but we still need pointers ! Pointers are not evil. This can be done in a very consistent way, integrated with the type system, so that there is no risk of additionnal errors (as in Ada for example)

I liked the way that, in C/C++, objects are physically grouped in a structure, and how some fields are only pointers to other structures. When you copy, link or pass an object, you can figure out what it's exactly made of, and it gives it kind of "physical borders" : The granularity of an object is what's in the struct. Pointers are references to other objects.

Now, objects in modern languages are all mixed in a messy spaghetti graph of references. There is no more granularity or atomicity of an object.
For example, in Java, when you want to serialize an object, you have to take care of what is really part of your object : Not to export the whole world.

I agree to milit for the come back of pointers. What we need is a real new language. It is pointless to develop a bunch of same languages : Python, Javascript 1.5, Ruby : Are just copies of the same concept, with slight variation of syntax / implementations.
I would like a new scripting langage :
- Garbage collection
- Strongly typed, or at least with type inference (like oCaml) :
Developping large projects with weak typing is an utopia : It makes
you loose the time that you get with scripting langages.
- Interpreted, or transparent compilation
- Powerfull complex types (arrays, lists, tuples, dictionnary, ....)
- Handy and expressive syntax (like Ruby, or python : but no indentation driven)
- Explicit pointers and references

∴ Raphael | 18-Oct-2005 5:52pm est | #8492

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 4 posts

Recent comments XML

new⇒Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Girls, please don't get breast implants

Hey everyone, 

I am new to this​blog and I have enjoyed reading all​your...

Sarah.M.: Oct 6, 9:45am

obout inc - ASP.NET controls

I like there components. I've got​it to work locally on my pc.​However I'm ...

Jeff: Oct 2, 4:43pm

Dumb substring behavior in C# (and Java)

Yes, the Substring function is not​helpful when you hit the length​problem,...

Mike Irving: Oct 2, 7:56am

Generated in about 0.127s.

(Used 8 db queries)

mobile phone