<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: A Square Is Not a Rectangle</title>
	<atom:link href="http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/feed/" rel="self" type="application/rss+xml" />
	<link>http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/</link>
	<description>Longer than a blog; shorter than a book</description>
	<lastBuildDate>Tue, 09 Mar 2010 18:31:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Ville</title>
		<link>http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/comment-page-1/#comment-448160</link>
		<dc:creator>Ville</dc:creator>
		<pubDate>Thu, 26 Nov 2009 08:22:19 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=469#comment-448160</guid>
		<description>bwtaylor has it right. You need to separate the actual information from the Rectangle api.

I would perhaps eg. define protected abstract class Dims in Rectangle, which has intance variables width and height and setters for them. Then Rectangle has inner private class that extends/imlements that class. Rectangle has also setters and getters for width and height, where dimensions is manipulated through Dims instance.

Now, Square inherits Rectangle and has inner private class that inherits Dims and overrides setters, where width and height is set the same.

So what is really changed in Square, is just one implementation of abstract class, that is not exposured by Rectangle-api.

That was not very good explanation, sorry, but my example works:


public class Rectangle {
    protected Dims dims;

    public Rectangle() {
        dims = new RecDims(0, 0);
    }

    public Rectangle(int width, int height) {
        dims = new RecDims(width, height);
    }
    
    public int getWidth() { return dims.width; }
    public int getHeight() { return dims.height; }
    
    public void setWidth(int wid) { dims.setWidth(wid); }
    public void setHeight(int hei) { dims.setHeight(hei); }
    
    
    private class RecDims extends Dims {
        public RecDims(int wid, int hei) {
            width = wid;
            height = hei;
        }
    }
    
    protected abstract class Dims {
        
        public int width;
        public int height;
        
        public void setWidth(int wid) { width = wid; }
        public void setHeight(int hei) { height = hei; }
    }
    
}


public class Square extends Rectangle {
    
    public Square(int dim) {
        dims = new SquDims(dim);
    }
    
    private class SquDims extends Dims {
        
        public SquDims(int dim) {
            width = dim;
            height = dim;
        }
        
        public void setWidth(int wid) { width = wid; height = wid; }
        public void setHeight(int hei) { height = hei; width = hei; }
    }
    
}</description>
		<content:encoded><![CDATA[<p>bwtaylor has it right. You need to separate the actual information from the Rectangle api.</p>
<p>I would perhaps eg. define protected abstract class Dims in Rectangle, which has intance variables width and height and setters for them. Then Rectangle has inner private class that extends/imlements that class. Rectangle has also setters and getters for width and height, where dimensions is manipulated through Dims instance.</p>
<p>Now, Square inherits Rectangle and has inner private class that inherits Dims and overrides setters, where width and height is set the same.</p>
<p>So what is really changed in Square, is just one implementation of abstract class, that is not exposured by Rectangle-api.</p>
<p>That was not very good explanation, sorry, but my example works:</p>
<p>public class Rectangle {<br />
    protected Dims dims;</p>
<p>    public Rectangle() {<br />
        dims = new RecDims(0, 0);<br />
    }</p>
<p>    public Rectangle(int width, int height) {<br />
        dims = new RecDims(width, height);<br />
    }</p>
<p>    public int getWidth() { return dims.width; }<br />
    public int getHeight() { return dims.height; }</p>
<p>    public void setWidth(int wid) { dims.setWidth(wid); }<br />
    public void setHeight(int hei) { dims.setHeight(hei); }</p>
<p>    private class RecDims extends Dims {<br />
        public RecDims(int wid, int hei) {<br />
            width = wid;<br />
            height = hei;<br />
        }<br />
    }</p>
<p>    protected abstract class Dims {</p>
<p>        public int width;<br />
        public int height;</p>
<p>        public void setWidth(int wid) { width = wid; }<br />
        public void setHeight(int hei) { height = hei; }<br />
    }</p>
<p>}</p>
<p>public class Square extends Rectangle {</p>
<p>    public Square(int dim) {<br />
        dims = new SquDims(dim);<br />
    }</p>
<p>    private class SquDims extends Dims {</p>
<p>        public SquDims(int dim) {<br />
            width = dim;<br />
            height = dim;<br />
        }</p>
<p>        public void setWidth(int wid) { width = wid; height = wid; }<br />
        public void setHeight(int hei) { height = hei; width = hei; }<br />
    }</p>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jerry Leichter</title>
		<link>http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/comment-page-1/#comment-438889</link>
		<dc:creator>Jerry Leichter</dc:creator>
		<pubDate>Thu, 01 Oct 2009 16:13:30 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=469#comment-438889</guid>
		<description>I used to use this as an interview question.  I&#039;d ask a candidate whether a square was a subclass of a rectangle, or a rectangle a subclass of a square.  Whichever answer they gave, I&#039;d point out the problems.  A good candidate will show an ability to reason about this stuff.  (There is, of course, no &quot;right&quot; answer - but all kinds of answers - like &quot;in class, we were told that squares were subclasses of rectangles&quot; - are wrong.)

Although people try to use mathematical arguments about types, types play little role in mathematics.  Neither &quot;Square&quot; nor &quot;Rectangle&quot; would be thought of as a type.  There is a set of all Rectangles, which has a subset of all Squares.  Because Square is a subset, any function whose *domain* is Rectangle is defined for all members of Square.  On the other hand, there&#039;s no particularly interesting connection between functions whose *range* is Rectangle and functions whose *range* is Square.  It may be interesting to prove that some function described as having a range of Rectangle actually only produces Squares, but that&#039;s proving something about the function, not talking about types.  Mutability isn&#039;t strictly an issue - in using English, a mathematician will certainly say things like &quot;doubling the length of the sides of a Square multiplies the area by 4&quot;, which *sounds* like the square is being modified, even to a mathematician.  Formally, though, if you pushed, we can describe the same thing functionally - we have a function from Square to Square that gives you a new square (this is itself just an informal phrase!).  A function that only doubled the horizontal dimension would go from Square to Rectangle.  No big deal.  You can recast your classes to be immutable, but as long as you have functions that return new, modified instances, you have the same problems.

LSP is a fine idea, but it only goes so far.  If I have a &quot;typeof&quot; operator that returns the dynamic type, no class is really substitutable for another, because they produce different results from &quot;typeof&quot;.  Does that bother you?  The whole notion of &quot;isa&quot; as the defining relationship for subclasses/subtypes fails for the same reason.

Programming isn&#039;t mathematics.  Subclassing and even subtyping are programming concepts, not mathematical concepts, though there&#039;s a fine mathematical theory of types developed exactly to help you understand the implications of choosing a style of subclassing/subtyping/genericity/etc.  If you&#039;re concerned about displaying geometric objects - where this whole discussion usually arises - then thinking of a Square as a &quot;kind of&quot; Rectangle which is a &quot;kind of&quot; Polygon which is a &quot;kind of&quot; Shape is useful.  Once you begin thinking about what kinds of mutators are appropriate, things get much more interesting.  Consider a graphical editor:  If I create a Square, then grab it and resize it, do I expect it to stay square, or can I stretch it one way and make it a Rectangle?  Now you&#039;re talking neither mathematics nor programming - you&#039;re talking UI design and user expectations.

                                                        -- Jerry</description>
		<content:encoded><![CDATA[<p>I used to use this as an interview question.  I&#8217;d ask a candidate whether a square was a subclass of a rectangle, or a rectangle a subclass of a square.  Whichever answer they gave, I&#8217;d point out the problems.  A good candidate will show an ability to reason about this stuff.  (There is, of course, no &#8220;right&#8221; answer &#8211; but all kinds of answers &#8211; like &#8220;in class, we were told that squares were subclasses of rectangles&#8221; &#8211; are wrong.)</p>
<p>Although people try to use mathematical arguments about types, types play little role in mathematics.  Neither &#8220;Square&#8221; nor &#8220;Rectangle&#8221; would be thought of as a type.  There is a set of all Rectangles, which has a subset of all Squares.  Because Square is a subset, any function whose *domain* is Rectangle is defined for all members of Square.  On the other hand, there&#8217;s no particularly interesting connection between functions whose *range* is Rectangle and functions whose *range* is Square.  It may be interesting to prove that some function described as having a range of Rectangle actually only produces Squares, but that&#8217;s proving something about the function, not talking about types.  Mutability isn&#8217;t strictly an issue &#8211; in using English, a mathematician will certainly say things like &#8220;doubling the length of the sides of a Square multiplies the area by 4&#8243;, which *sounds* like the square is being modified, even to a mathematician.  Formally, though, if you pushed, we can describe the same thing functionally &#8211; we have a function from Square to Square that gives you a new square (this is itself just an informal phrase!).  A function that only doubled the horizontal dimension would go from Square to Rectangle.  No big deal.  You can recast your classes to be immutable, but as long as you have functions that return new, modified instances, you have the same problems.</p>
<p>LSP is a fine idea, but it only goes so far.  If I have a &#8220;typeof&#8221; operator that returns the dynamic type, no class is really substitutable for another, because they produce different results from &#8220;typeof&#8221;.  Does that bother you?  The whole notion of &#8220;isa&#8221; as the defining relationship for subclasses/subtypes fails for the same reason.</p>
<p>Programming isn&#8217;t mathematics.  Subclassing and even subtyping are programming concepts, not mathematical concepts, though there&#8217;s a fine mathematical theory of types developed exactly to help you understand the implications of choosing a style of subclassing/subtyping/genericity/etc.  If you&#8217;re concerned about displaying geometric objects &#8211; where this whole discussion usually arises &#8211; then thinking of a Square as a &#8220;kind of&#8221; Rectangle which is a &#8220;kind of&#8221; Polygon which is a &#8220;kind of&#8221; Shape is useful.  Once you begin thinking about what kinds of mutators are appropriate, things get much more interesting.  Consider a graphical editor:  If I create a Square, then grab it and resize it, do I expect it to stay square, or can I stretch it one way and make it a Rectangle?  Now you&#8217;re talking neither mathematics nor programming &#8211; you&#8217;re talking UI design and user expectations.</p>
<p>                                                        &#8212; Jerry</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: levy</title>
		<link>http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/comment-page-1/#comment-437681</link>
		<dc:creator>levy</dc:creator>
		<pubDate>Tue, 22 Sep 2009 09:26:08 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=469#comment-437681</guid>
		<description>http://www.reddit.com/r/programming/comments/9mvq1/a_square_is_a_rectangle/</description>
		<content:encoded><![CDATA[<p><a href="http://www.reddit.com/r/programming/comments/9mvq1/a_square_is_a_rectangle/" rel="nofollow">http://www.reddit.com/r/programming/comments/9mvq1/a_square_is_a_rectangle/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: carloscs</title>
		<link>http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/comment-page-1/#comment-437394</link>
		<dc:creator>carloscs</dc:creator>
		<pubDate>Fri, 18 Sep 2009 23:36:18 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=469#comment-437394</guid>
		<description>&gt; Dave Hemming Says: 
&gt;
&gt; Am I alone in looking at this and saying Rectangle is a subclass of Square?

Not even in your dreams :)

------------------
Minor nit:

If 
-&gt; Rectangle extends Square with setHeight, setWidth
then 
-&gt; Shape extends Square, Rectangle, Circle, ... with setSize, setHeight, setWidth, setRadius, ...

------------------
Major nit:

class Square ...

class Rectangle extends Square...

Square sq = new Rectangle(10, 5);

oops....

------------------
Really, this thread leads to the conclusion that for IS-A what we really want is:

class Rectangle...

class Square restricts Rectangle...

;)</description>
		<content:encoded><![CDATA[<p>&gt; Dave Hemming Says:<br />
&gt;<br />
&gt; Am I alone in looking at this and saying Rectangle is a subclass of Square?</p>
<p>Not even in your dreams <img src='http://cafe.elharo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
Minor nit:</p>
<p>If<br />
-&gt; Rectangle extends Square with setHeight, setWidth<br />
then<br />
-&gt; Shape extends Square, Rectangle, Circle, &#8230; with setSize, setHeight, setWidth, setRadius, &#8230;</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
Major nit:</p>
<p>class Square &#8230;</p>
<p>class Rectangle extends Square&#8230;</p>
<p>Square sq = new Rectangle(10, 5);</p>
<p>oops&#8230;.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
Really, this thread leads to the conclusion that for IS-A what we really want is:</p>
<p>class Rectangle&#8230;</p>
<p>class Square restricts Rectangle&#8230;</p>
<p> <img src='http://cafe.elharo.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bwtaylor</title>
		<link>http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/comment-page-1/#comment-437386</link>
		<dc:creator>bwtaylor</dc:creator>
		<pubDate>Fri, 18 Sep 2009 21:59:22 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=469#comment-437386</guid>
		<description>Fascinating discussion.

Our intuition about rectangles and squares is as immutable objects. If you try to model this, then indeed ImmutableSquare is a subclass of ImmutableRectangle and  it should use constructors. If you want mutation, then a Square is a 1-parameter shape and a rectangle is a 2-parameter shape. These are clearly not the same. Here&#039;s a way:

 

public abstract class MutableShape;

    private Integer numberOfParameters;

    private double[] parameters;
    
    MutableShape(Integer numberOfParameters, double... parameters);

        if (numberOfParameters &gt;= 0) {

            this.numberOfParameters = numberOfParameters;

            setParameters(parameters);

        }

        else throw new IllegalArgumentException(&quot;numberOfParameters must be non-negative&quot;);

    }
    
    void setParameters(double... parameters) {

        if (parameters.length == numberOfParameters)

            this.parameters = parameters;

        else throw new IllegalArgumentException(&quot;wrong number of parameters&quot;);

    }

    public double[] getParameters() { return parameters; }
    
    public abstract double getArea();
   
}


----------------------------

    public class Square extends MutableShape {

        public Square(double size) {

            super(1, size);

        }

        public void setSize(double size) { getParameters()[0] = size; }

        public double getSize() { return getParameters()[0]; }        

        public double getArea() {

            double size = getSize();

            return size * size;

        }       

    }

----------------------------

    public class Rectangle extends MutableShape {

        public Rectangle(double height, double width) {

            super(2, height, width);

        }

        public double getHeight() { return getParameters()[0]; }

        public double getWidth() { return getParameters()[1]; }

        public void setHeight(double height) { getParameters()[0] = height; }

        public void setWidth(double width) { getParameters()[1] = width; }

        public double getArea() {

            return getHeight() * getWidth();

        }

        public boolean isSquare() { return getHeight() == getWidth(); }

        public Square toSquare() {

            return isSquare() ? new Square(getHeight()) : null;

        }

</description>
		<content:encoded><![CDATA[<p>Fascinating discussion.</p>
<p>Our intuition about rectangles and squares is as immutable objects. If you try to model this, then indeed ImmutableSquare is a subclass of ImmutableRectangle and  it should use constructors. If you want mutation, then a Square is a 1-parameter shape and a rectangle is a 2-parameter shape. These are clearly not the same. Here&#8217;s a way:</p>
<p>public abstract class MutableShape;</p>
<p>    private Integer numberOfParameters;</p>
<p>    private double[] parameters;</p>
<p>    MutableShape(Integer numberOfParameters, double&#8230; parameters);</p>
<p>        if (numberOfParameters &gt;= 0) {</p>
<p>            this.numberOfParameters = numberOfParameters;</p>
<p>            setParameters(parameters);</p>
<p>        }</p>
<p>        else throw new IllegalArgumentException(&#8220;numberOfParameters must be non-negative&#8221;);</p>
<p>    }</p>
<p>    void setParameters(double&#8230; parameters) {</p>
<p>        if (parameters.length == numberOfParameters)</p>
<p>            this.parameters = parameters;</p>
<p>        else throw new IllegalArgumentException(&#8220;wrong number of parameters&#8221;);</p>
<p>    }</p>
<p>    public double[] getParameters() { return parameters; }</p>
<p>    public abstract double getArea();</p>
<p>}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>    public class Square extends MutableShape {</p>
<p>        public Square(double size) {</p>
<p>            super(1, size);</p>
<p>        }</p>
<p>        public void setSize(double size) { getParameters()[0] = size; }</p>
<p>        public double getSize() { return getParameters()[0]; }        </p>
<p>        public double getArea() {</p>
<p>            double size = getSize();</p>
<p>            return size * size;</p>
<p>        }       </p>
<p>    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>    public class Rectangle extends MutableShape {</p>
<p>        public Rectangle(double height, double width) {</p>
<p>            super(2, height, width);</p>
<p>        }</p>
<p>        public double getHeight() { return getParameters()[0]; }</p>
<p>        public double getWidth() { return getParameters()[1]; }</p>
<p>        public void setHeight(double height) { getParameters()[0] = height; }</p>
<p>        public void setWidth(double width) { getParameters()[1] = width; }</p>
<p>        public double getArea() {</p>
<p>            return getHeight() * getWidth();</p>
<p>        }</p>
<p>        public boolean isSquare() { return getHeight() == getWidth(); }</p>
<p>        public Square toSquare() {</p>
<p>            return isSquare() ? new Square(getHeight()) : null;</p>
<p>        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dstibbe</title>
		<link>http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/comment-page-1/#comment-437336</link>
		<dc:creator>dstibbe</dc:creator>
		<pubDate>Fri, 18 Sep 2009 07:39:00 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=469#comment-437336</guid>
		<description>Mmh... as I kept thinking an other idea occurred. What I just posted above this post is useless :(

I definitely have to go with Johann&#039;s train of thought. 
This would result in the following :

* all classes immutable. You cannot modify an object, merely create a new one.

*class Rectangle with methods
constructor: Rectangle( width, height)
Rectangle scale( percentage );
Rectangle reshapeWidth( width )
Rectangle reshapeHeigth( height) 

*class Square extends Rectangle with methods:
constructor: Square(width) 
constructor: Square( Rectangle ) 
Square scale( percentage );
Rectangle reshapeWidth( width) 
Rectangle reshapeHeight( height )
boolean isSquare( Rectangle )

-if you want to create a square with a new specific height, use the constructor.

-If you modify the width/height of an existing Square or Rectangle a new Rectangle object will be the result. 

-When you scale a Square a new Square will result.  When you scale a Rectangle a new Rectangle will result.</description>
		<content:encoded><![CDATA[<p>Mmh&#8230; as I kept thinking an other idea occurred. What I just posted above this post is useless <img src='http://cafe.elharo.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>I definitely have to go with Johann&#8217;s train of thought.<br />
This would result in the following :</p>
<p>* all classes immutable. You cannot modify an object, merely create a new one.</p>
<p>*class Rectangle with methods<br />
constructor: Rectangle( width, height)<br />
Rectangle scale( percentage );<br />
Rectangle reshapeWidth( width )<br />
Rectangle reshapeHeigth( height) </p>
<p>*class Square extends Rectangle with methods:<br />
constructor: Square(width)<br />
constructor: Square( Rectangle )<br />
Square scale( percentage );<br />
Rectangle reshapeWidth( width)<br />
Rectangle reshapeHeight( height )<br />
boolean isSquare( Rectangle )</p>
<p>-if you want to create a square with a new specific height, use the constructor.</p>
<p>-If you modify the width/height of an existing Square or Rectangle a new Rectangle object will be the result. </p>
<p>-When you scale a Square a new Square will result.  When you scale a Rectangle a new Rectangle will result.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dstibbe</title>
		<link>http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/comment-page-1/#comment-437334</link>
		<dc:creator>dstibbe</dc:creator>
		<pubDate>Fri, 18 Sep 2009 07:21:36 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=469#comment-437334</guid>
		<description>Then how about the following. In lin of Dave and Johann&#039;s remarks:

How about calling the method &#039;scale&#039; ?

You take the class Square and merely give it the method scale. 

The class Rectangle then extends Square and adds the operations scalewidth and scaleHeight.</description>
		<content:encoded><![CDATA[<p>Then how about the following. In lin of Dave and Johann&#8217;s remarks:</p>
<p>How about calling the method &#8217;scale&#8217; ?</p>
<p>You take the class Square and merely give it the method scale. </p>
<p>The class Rectangle then extends Square and adds the operations scalewidth and scaleHeight.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johann Thomas</title>
		<link>http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/comment-page-1/#comment-437243</link>
		<dc:creator>Johann Thomas</dc:creator>
		<pubDate>Thu, 17 Sep 2009 14:02:54 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=469#comment-437243</guid>
		<description>... Still, when I first encountered this analogy as a thought exercise, I could not spot the failure lightly. It is the model that is violated, by adding the convenience methods setHeight and setWidth. It is an implementation detail and the contract is not declared. We just think, we know, what setHeight does. The comments regarding &quot;resizing the shape&quot; and &quot;immutable, only constructor&quot; are half way to finding the problem. 
 SetHeight seems correct on a rectangle, but we get a completely different object - it just happens to remain a rectangle, that is the reason we believe to be safe, but
 1. It violates the encapsulation 
 2. and the immutability - changing the dimensions really replaces the old object with something new, that is were immutability is the safe way to go.
 3. setHeight is a naive name, better use reshapeHeight. mySquare.reshapeHeight will result naturally in a myRectangle, which can be reshaped again by myRectangle.reshapeWidth and 
  
    m2ndSquare = Square.isSquare(myRectangle) ? new Square(myRectangle) : null;
 </description>
		<content:encoded><![CDATA[<p>&#8230; Still, when I first encountered this analogy as a thought exercise, I could not spot the failure lightly. It is the model that is violated, by adding the convenience methods setHeight and setWidth. It is an implementation detail and the contract is not declared. We just think, we know, what setHeight does. The comments regarding &#8220;resizing the shape&#8221; and &#8220;immutable, only constructor&#8221; are half way to finding the problem.<br />
 SetHeight seems correct on a rectangle, but we get a completely different object &#8211; it just happens to remain a rectangle, that is the reason we believe to be safe, but<br />
 1. It violates the encapsulation<br />
 2. and the immutability &#8211; changing the dimensions really replaces the old object with something new, that is were immutability is the safe way to go.<br />
 3. setHeight is a naive name, better use reshapeHeight. mySquare.reshapeHeight will result naturally in a myRectangle, which can be reshaped again by myRectangle.reshapeWidth and </p>
<p>    m2ndSquare = Square.isSquare(myRectangle) ? new Square(myRectangle) : null;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johann Thomas</title>
		<link>http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/comment-page-1/#comment-437240</link>
		<dc:creator>Johann Thomas</dc:creator>
		<pubDate>Thu, 17 Sep 2009 13:43:28 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=469#comment-437240</guid>
		<description>A lot of interesting ideas and remarks above...

mathematically speaking:
 - a rectangle is a (convex) polygon with 4 orthogonal edges
 - a square adds another constraint: all sides same length
 - both are immutable
 - a geometrical transformation transforms each point one-by-one
 - is is known, what transformations results are what kind of a shape

But:
 - you will not resize a shape in mathematics, by resizing its length but by resizing its points, for rectangles the vertexes and redraw the edges
 - setting the height or width of a rectangle is not a simple operation, it is a transformation on an infinite number of points

So:
 - you simplified the mathematical rectangle to something named the same way, but have implemented the transformations wrong. Setting the height on a square will make it a different polygon, it will be a common rectangle.
 - setting height, than width on a rectangle simply is a set of consecutive operations/transformations, which do not preserve the shape, but happen to result in the same shape after doing both with carefully chosen new values. 
 - what about choosing stretchHeight as the name for setHeight?</description>
		<content:encoded><![CDATA[<p>A lot of interesting ideas and remarks above&#8230;</p>
<p>mathematically speaking:<br />
 &#8211; a rectangle is a (convex) polygon with 4 orthogonal edges<br />
 &#8211; a square adds another constraint: all sides same length<br />
 &#8211; both are immutable<br />
 &#8211; a geometrical transformation transforms each point one-by-one<br />
 &#8211; is is known, what transformations results are what kind of a shape</p>
<p>But:<br />
 &#8211; you will not resize a shape in mathematics, by resizing its length but by resizing its points, for rectangles the vertexes and redraw the edges<br />
 &#8211; setting the height or width of a rectangle is not a simple operation, it is a transformation on an infinite number of points</p>
<p>So:<br />
 &#8211; you simplified the mathematical rectangle to something named the same way, but have implemented the transformations wrong. Setting the height on a square will make it a different polygon, it will be a common rectangle.<br />
 &#8211; setting height, than width on a rectangle simply is a set of consecutive operations/transformations, which do not preserve the shape, but happen to result in the same shape after doing both with carefully chosen new values.<br />
 &#8211; what about choosing stretchHeight as the name for setHeight?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Augusto</title>
		<link>http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/comment-page-1/#comment-437199</link>
		<dc:creator>Augusto</dc:creator>
		<pubDate>Thu, 17 Sep 2009 02:42:30 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=469#comment-437199</guid>
		<description>C&#039;mon guys, we really have to debug some of the sample code? I&#039;m sure you all get the idea no?

I don&#039;t see a problem with overriding the setHeight() and setWidth() rectangle arguments to make a square, if you wanted to make such a type. The immutable solution is nice, but limiting.</description>
		<content:encoded><![CDATA[<p>C&#8217;mon guys, we really have to debug some of the sample code? I&#8217;m sure you all get the idea no?</p>
<p>I don&#8217;t see a problem with overriding the setHeight() and setWidth() rectangle arguments to make a square, if you wanted to make such a type. The immutable solution is nice, but limiting.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
