osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A question on modification of a list via a function invocation


On Wednesday, September 6, 2017 at 4:03:40 PM UTC+5:30, ROGER GRAYDON CHRISTMAN wrote:
> On 5 Sep 2017 14:28:44,  (Dennis Lee Bier) wrote: 
> > On 5 Sep 2017 17:57:18 GMT, <a onclick="addTo('ram')"

> 
> >>  But what does "a C++ reference" refer to?
> >>
> 
> > Per Stroustrup (The C++ Programming Language 4th Ed, page 189)
> 
> > """
> > * ...> * A reference always refers to the object to which it was initialized.
> > * ...
> 
> > A reference is an alternative name for an object, an alias. ...
> > """
> 
> > {Hmmm, and I see that the syntax can be used outside of parameter
> > declaration -- which is the only place I'd seen it previously... either
> > this is a change from earlier standards, or my classes just didn't feel the
> > need to expose a non-parameter reference -- since, based upon the above
> > book, you can not declare a bare reference "variable"; it MUST be
> > initialized with a real object.}
> 
> I think I can say something about this, having been a teacherof the classes you refer to.   I intentionally avoided reference variables.
> IMO, the 'good' use for declaring a new reference variable (i.e. not parameter)would be when (1) the object to which you refer to is time-consuming to access(2) you plan to refer to this object more then once, and don't want to repeatthat time-consuming process, and (3) you really want a reference, and not a copy.
> The first two years of programming courses really do not have a purposethat meets all three, so can "didn't feel the need" is probably applicable.
> I intentionally avoided them because reference variables simply compoundthe problem of aliasing, so unless you really limit your reference variableto a very tight sandbox, you could be causing more headache than you save.
> I do admit to occasionally defining a method that returned a reference,such as one that overloads the [] operator.   But even so, I would generallybe reluctant to giving an outside client a direct access to my database'sinternal structures.  (Thank you Python for separating __getitem__ and __setitem__)
> Python doesn't eliminate aliasing, of course, since most assignment operationscreate aliases.  But at least it's nice to know that aliasing immutable valuesis harmless.   Hence my unit on building recursive data structures entirelyout of tuples.

The realization that immutability is a significant virtue is now beginning 
to percolate mainstream programming
Ive seen it in recent C# books as a definite recommendation? Something like
- Use value types
- Use getters but no setters

And you have a good design

Python makes this hard by giving less status to immutable types than mutable ones
- set comprehensions exist not frozenset comprehensions
- Likewise tuples and lists