<?xml version="1.0" encoding="UTF-8"?> 
<feed xmlns="http://www.w3.org/2005/Atom">
	<title>Are getters and setters object oriented? - Arkanis</title>
	<subtitle>A blog about random stuff, but mostly programming.</subtitle>
	<id>http://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented.xml</id>
	<link href="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/comments.xml" rel="self" />
	<link href="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/#comments" rel="alternate" />
	<updated>2025-07-24T18:32:02+00:00</updated>
	<entry>
		<title>Comment by Stephan</title>
		<author>
			<name>Stephan</name>
		</author>
		<id>http://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented#comment-2011-02-02-17-05-41-stephan</id>
		<link rel="alternate" href="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/#comment-2011-02-02-17-05-41-stephan" />
		<updated>2011-02-02T17:05:41+00:00</updated>
		<content type="html" xml:base="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/">
&lt;p&gt;I programmed in VisualBasic 6.0 some years ago and I vaguely remember some kind of &amp;quot;Property Get&amp;quot; or &amp;quot;Property Set&amp;quot; stuff. Nice to see it in the new .Net syntax.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Hm, but is &amp;quot;property overloading&amp;quot; something different from wrapping setters and getters into an API to hide their existence, very similar to the way .NET properties work?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;No, it&#039;s exactly that. The whole point is that you can add &amp;quot;property specific&amp;quot; code later on without changing the code that uses the property. Java doesn&#039;t allow that and therefore the get/set Method convention was established. Unfortunately this convention got the reputation of being &amp;quot;good object orientation practice&amp;quot; and therefore people used it even in languages that do not have that problem. I saw that when Extbase was demonstrated on the WebDay 2010 &lt;a href=&quot;http://events.mi.hdm-stuttgart.de/2010-12-10-web-day-2010#extensions-mit-php-extbase-und-fluid&quot;&gt;1&lt;/a&gt;. Because of that I decided to write a bit about it hoping that a few people think about that before the use getter and setter methods in PHP.&lt;/p&gt;
&lt;p&gt;In retrospective I should have chosen another title for the post. &amp;quot;Are getters and setters object oriented?&amp;quot; is aimed at the people that actually believe that getter and setter methods are an integral part of object orientation. That exposure of state actually breaks encapsulation and therefore a good deal of object orientation is an even higher-level problem that would be very difficult to understand for those people.&lt;/p&gt; 
		</content>
	</entry>
	<entry>
		<title>Comment by Andreas Stiegler</title>
		<author>
			<name>Andreas Stiegler</name>
		</author>
		<id>http://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented#comment-2011-01-31-20-02-43-andreas-stiegler</id>
		<link rel="alternate" href="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/#comment-2011-01-31-20-02-43-andreas-stiegler" />
		<updated>2011-01-31T20:02:43+00:00</updated>
		<content type="html" xml:base="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/">
&lt;p&gt;Hm, but is &amp;quot;property overloading&amp;quot; something different from wrapping setters and getters into an API to hide their existence, very similar to the way .NET properties work? This would allow having an internal &lt;code&gt;_Speed&lt;/code&gt; as vector2, reading or writing it as float via the get and set part of a property, but accessing it just like a public field, just like in your D example above. The syntax is even quite similar. Be warned, VB incoming!&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Public Class Car
    Private _Speed As Vector2
    Public Property Speed As Double
        Get
            Return Math.Sqrt(_Speed.X * _Speed.X + _Speed.Y * _Speed.Y)
        End Get
        Set(ByVal value As Double)
            If value &amp;lt; Epsilon Then value = Epsilon
            Dim speedThroughLength = value / Speed
            _Speed.X *= speedThroughLength
            _Speed.Y *= speedThroughLength
        End Set
    End Property
End Class&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and somewhere else&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Dim mercedes As New Car
mercedes.Speed = 7
Console.WriteLine(mercedes.Speed)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I might be spoiled a bit from game development, but i threw the holy grail of OO out of the window a while ago and never found it again.&lt;br /&gt;
Performance, performance, performance&lt;/p&gt; 
		</content>
	</entry>
	<entry>
		<title>Comment by Stephan</title>
		<author>
			<name>Stephan</name>
		</author>
		<id>http://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented#comment-2011-01-01-17-07-40-stephan</id>
		<link rel="alternate" href="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/#comment-2011-01-01-17-07-40-stephan" />
		<updated>2011-01-01T17:07:40+00:00</updated>
		<content type="html" xml:base="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/">
&lt;p&gt;Well said. 🙂&lt;/p&gt; 
		</content>
	</entry>
	<entry>
		<title>Comment by Nils Jonsson</title>
		<author>
			<name>Nils Jonsson</name>
		</author>
		<id>http://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented#comment-2011-01-01-07-25-33-nils-jonsson</id>
		<link rel="alternate" href="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/#comment-2011-01-01-07-25-33-nils-jonsson" />
		<updated>2011-01-01T07:25:33+00:00</updated>
		<content type="html" xml:base="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/">
&lt;p&gt;Real question is whether methods that expose state are OO. Tell, don&#039;t ask, and you&#039;ll be working with objects instead of glorified structs.&lt;/p&gt; 
		</content>
	</entry>
	<entry>
		<title>Comment by Stephan</title>
		<author>
			<name>Stephan</name>
		</author>
		<id>http://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented#comment-2010-12-26-17-17-20-stephan</id>
		<link rel="alternate" href="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/#comment-2010-12-26-17-17-20-stephan" />
		<updated>2010-12-26T17:17:20+00:00</updated>
		<content type="html" xml:base="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/">
&lt;p&gt;I&#039;m not sure I understand your point. The syntax for properties in C# looks somewhat strange to me but I have never really programmed in .Net so I can&#039;t judge if it&#039;s consistent with the language. I don&#039;t understand where .Net adds awareness for properties because the user of an object can use it the same way as normal fields. Otherwise it would break encapsulation (or that what&#039;s left of it with getters and setters).&lt;/p&gt;
&lt;p&gt;I can see advantages in documentation but here Python, Ruby and D offer the same way to extract that information. In PHP it&#039;s a bit more complex since there are no established coding conventions for property overloading I know of.&lt;/p&gt;
&lt;p&gt;Regarding the awareness that exceptions are thrown I&#039;m a bit surprised. I don&#039;t know what programmers are supposed to think while programming .Net but it sounds strange to me that someone would actually expect a specific part of the code not being able to throw an exception. Maybe I&#039;m spoiled by Ruby in this regard, don&#039;t know. The only thing I can think of in this direction is that try/catch blocks are a bit clumsy and therefore only used when absolutely necessary. The scope guard statement &lt;a href=&quot;http://digitalmars.com/d/2.0/exception-safe.html&quot;&gt;1&lt;/a&gt; of the D programming language would surely help there. But exception handling is a whole different topic.&lt;/p&gt;
&lt;p&gt;I&#039;m pretty sure I misunderstood your point, so if you have a view minutes I would appreciate a deeper explanation.&lt;/p&gt; 
		</content>
	</entry>
	<entry>
		<title>Comment by Valentin</title>
		<author>
			<name>Valentin</name>
		</author>
		<id>http://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented#comment-2010-12-23-12-06-56-valentin</id>
		<link rel="alternate" href="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/#comment-2010-12-23-12-06-56-valentin" />
		<updated>2010-12-23T12:06:56+00:00</updated>
		<content type="html" xml:base="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/">
&lt;p&gt;I partially agree. Because a good thing about getter and setter-Methods is that they are in fact methods. This raises some awareness, since programmers know that methods might throw IllegalArgumentExceptions or something like that. A property however has a passive role in general which may lead to a more careless use of the property.&lt;/p&gt;
&lt;p&gt;I personally find a nice way to handle getters- and setters is the .NET-Approach. In .NET they distinguisch between fields and properties. While Properties can be used like simple fields it is perfectly clear that they might contain validation code and throw exceptions. This raises awareness on the programmers side but still makes them usable in a straightforward way.&lt;/p&gt; 
		</content>
	</entry>
	<entry>
		<title>Comment by Stephan</title>
		<author>
			<name>Stephan</name>
		</author>
		<id>http://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented#comment-2010-12-21-11-17-17-stephan</id>
		<link rel="alternate" href="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/#comment-2010-12-21-11-17-17-stephan" />
		<updated>2010-12-21T11:17:17+00:00</updated>
		<content type="html" xml:base="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/">
&lt;p&gt;Ruby solved this problem in a very consistent way: there simply are no externally accessible properties (in Ruby instance variables). What happens is that the attr_accessor() function defines getters and setters for all passed instance variable names. So in Ruby the indirection is always present but usually hidden by the shortcuts attr_reader(), attr_writer() and attr_accessor(). 🙂&lt;/p&gt;
&lt;p&gt;The reason why I didn&#039;t mention Ruby was that I never saw Ruby code that used getters and setters (methods named &lt;code&gt;get_xyz&lt;/code&gt; or something like that). Everyone uses the &lt;code&gt;attr_*&lt;/code&gt; functions. It seems Ruby programmers simply do not have this problem thanks to the strong coding conventions of Ruby.&lt;/p&gt; 
		</content>
	</entry>
	<entry>
		<title>Comment by S2</title>
		<author>
			<name>S2</name>
		</author>
		<id>http://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented#comment-2010-12-20-20-47-50-s2</id>
		<link rel="alternate" href="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/#comment-2010-12-20-20-47-50-s2" />
		<updated>2010-12-20T20:47:50+00:00</updated>
		<content type="html" xml:base="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/">
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; #Don&#039;t forget ruby! 🙂
?&amp;gt; #
?&amp;gt; class Car
&amp;gt;&amp;gt;   attr_accessor :speed
&amp;gt;&amp;gt; end;
?&amp;gt; c = Car.new
=&amp;gt; #&amp;lt;Car:0xb77bd538&amp;gt;
&amp;gt;&amp;gt; c.speed = 10
=&amp;gt; 10
&amp;gt;&amp;gt; c.speed
=&amp;gt; 10
&amp;gt;&amp;gt; class Car
&amp;gt;&amp;gt;   def speed=(s)
&amp;gt;&amp;gt;     @speed=s
&amp;gt;&amp;gt;   end
&amp;gt;&amp;gt; 
?&amp;gt;   def speed
&amp;gt;&amp;gt;     @speed
&amp;gt;&amp;gt;   end
&amp;gt;&amp;gt; end
=&amp;gt; nil
&amp;gt;&amp;gt; c = Car.new
=&amp;gt; #&amp;lt;Car:0xb779d10c&amp;gt;
&amp;gt;&amp;gt; c.speed = 10
=&amp;gt; 10
&amp;gt;&amp;gt; c.speed
=&amp;gt; 10
&amp;gt;&amp;gt;&lt;/code&gt;&lt;/pre&gt; 
		</content>
	</entry>
	<entry>
		<title>Comment by Me</title>
		<author>
			<name>Me</name>
		</author>
		<id>http://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented#comment-2010-12-20-03-32-49-me</id>
		<link rel="alternate" href="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/#comment-2010-12-20-03-32-49-me" />
		<updated>2010-12-20T03:32:49+00:00</updated>
		<content type="html" xml:base="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/">
&lt;p&gt;This is useful. Have never thought about this.&lt;br /&gt;
So python, PHP and D dont need setters and getters.&lt;/p&gt; 
		</content>
	</entry>
	<entry>
		<title>Comment by Stephan</title>
		<author>
			<name>Stephan</name>
		</author>
		<id>http://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented#comment-2010-12-20-03-29-02-stephan</id>
		<link rel="alternate" href="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/#comment-2010-12-20-03-29-02-stephan" />
		<updated>2010-12-20T03:29:02+00:00</updated>
		<content type="html" xml:base="https://arkanis.de/weblog/2010-12-19-are-getters-and-setters-object-oriented/">
&lt;p&gt;I fixed the typo Erick, thanks. 🙂&lt;/p&gt;
&lt;p&gt;The JavaScript proxies looks very promising. It&#039;s interesting they didn&#039;t made everything interceptable (e.g. identity operator, instanceof, typeof, …). Should make debugging easier. I used a similar construct in Ruby to inject internationalized objects into Rails 1.x (SimpleLocalization plugin) and debugging these proxies was… interesting.&lt;/p&gt;
&lt;p&gt;The more explicit approach of ActionScript 3.0 looks good. Actually in D2 it is advised to add the @property attribute to methods that are meant to be called as properties. It&#039;s not yet required though.&lt;/p&gt;
&lt;p&gt;The Python approach looks very interesting, too. Actually defining the property with two handlers… almost a bit JavaScript like. Good to know, wouldn&#039;t have expected that. 🙂&lt;/p&gt; 
		</content>
	</entry>
</feed>