+1 on returning NULL. Again, this is part of the *HUGE* exposed API :) --strk;
On Fri, Mar 31, 2006 at 05:01:52AM +0200, Mateusz Å?oskot wrote: > Hi, > > Please, forgive me that I'm annoying with all those recent questions but > I really would like to understand GEOS internals. > > > Here is getCoordinate: > > const Coordinate* LineString::getCoordinate() const > { > // should use auto_ptr here or return NULL or throw an exception ! > // --strk; > if (isEmpty()) > return(new Coordinate()); > > return &(points->getAt(0)); > } > > as you see, there is Sandro's comment about what could be best. > I think it's time to answer this question :-) > > For comparison and reference, here is JTS' version of this function: > > public Coordinate getCoordinate() > { > if (isEmpty()) return null; > return points.getCoordinate(0); > } > > Here are my comments to current solution: > 1. Returning newly allocated Coordinate is confusing and will cause > problems that user won't free the memory > > 2. This brokes the logic because how empty linestring can return any > Coordinate? This does not make any sense. > > > Here are my comments to possible solution: > > 1. auto_ptr will not solve the problem because it won't fix broken logic > will > > 2. My suggestion is to return *null pointer* > > As you can see above, JTS version returns null reference. > > What is Java null reference (a pointer to nothing)? > It is a reference that does not refer to any instance of any type. > > What we can do with Java null reference? > null reference can be safely compared with other references (null or not > null), null reference can be safely passed to the function, returned > from function, null reference can be safely assigned to the existing > reference etc. > What we can not do with null reference is dereferencing, e.g.: > > Type x = null; > x.Fire(); // BUM! > > Now, how to transit this Javism to C++. It's very simple - just use null > pointer. > > What is null pointer in C++? > Null pointer is a *valid* pointer value that doesnât point to anything. > Just as null reference in Java. > > Similarly, with null pointers in C++ we can do (almost) the same as what > we can do with Java null reference. > > The only thin that can not be done with C++ null pointer is > dereferencing. Dereferencing null pointer causes undefined behavior > This rule applies to every pointer that does not point to point to anything. > > What I'd like to repeat is that "null pointer is a *valid* pointer", > but not invalid. Invalid pointer is another category and *any* use of > invalid pointer causes Undefined Behaviour, even comparing, etc. > > So, I vote for returning null pointer. > > 3. Throwing an exception is a bad idea > > Exceptions should be used for unexpected situations but when user is > asking object for coordinate he is aware that the object may or may not > include any coordinates. So, there is no unexpected behaviour here. > > > BTW, I'm really sorry if my explanation seems to be like in books of > seriece "XXX for dummies". This is completely not my intetion! > Simply, I usualy try to explain my point as clear as possible. > > > Cheers > -- > Mateusz Åoskot > http://mateusz.loskot.net > _______________________________________________ > geos-devel mailing list > geos-devel@geos.refractions.net > http://geos.refractions.net/mailman/listinfo/geos-devel -- /"\ ASCII Ribbon Campaign \ / Respect for low technology. X Keep e-mail messages readable by any computer system. / \ Keep it ASCII. _______________________________________________ geos-devel mailing list geos-devel@geos.refractions.net http://geos.refractions.net/mailman/listinfo/geos-devel