My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
It might be petty, but those two three-letter words really get to me. They clutter my code almost as much as semicolons, Object obj = new Object() // assign a new object to the object called "obj" which is is of type Object, and angle brackets. I'm want to do something like newCodebase=rereplace(entireCodebase,"[gs]et","","all").

What if we had a new idiom where calling the method without arguments would cause it to return a value (the get), while calling it with an argument would run the setter? Its easy enough to do. What do you think, am I off my head?

Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!


Comments
Leave a comment

What about readonly attributes? Or if your get gets something completely different from your set?

Posted by Crazy Coder on May 15, 2007 at 01:52 PM UTC - 5 hrs

Well, I suppose for read only attributes you could only provide the get version (one that accepts no arguments).

And "if your get gets something completely different from your set," then I think you may as well name your variables after girlfriends, favorite foods, and video games - or just about anything that has nothing to do with what they represent. =) (Is that what you meant? I can't think of a situation where a getter is getting something different than what the equivalent setter is setting).

Posted by Sammy Larbi on May 15, 2007 at 01:56 PM UTC - 5 hrs

This exact functionality is supported by C# properties. Here's one reference, googling "C# properties" shows a bunch more:

http://www.csharp-station.com/Tutorials/Lesson10.a...

Posted by vinceb on May 15, 2007 at 02:13 PM UTC - 5 hrs

Now that we know Scorpio has missing method I can help you with three of the five characters you hate.

User.FirstName() calls missing method handler. It checks to see if any arguments. If arguments it looks for User.setFirstName() method. If it exists, it runs it. If not, if just sets variables.FirstName = whatever was passed in.

If no arguments, it looks for User.getFirstName(). If it exists, it calls it. If not, it returns variables.FirstName.

Nothing we can do in CF about the () because it supports a public properties scope. You're getting spoiled by Ruby!

Posted by Peter Bell on May 15, 2007 at 03:36 PM UTC - 5 hrs

Of course, if you want information hiding, you also need a way to either describe the list of gettable and/or settable properties and/or to have some kind of way of defining every property within the variables scope by whether publicly gettable, settable, both or neither.

Posted by Peter Bell on May 15, 2007 at 03:39 PM UTC - 5 hrs

@Vince- I recently learned that when Werner Schuster at InfoQ showed how to do the same (add properties) in Ruby (which is fairly pointless, since it has attr_accessor/writer/reader and you can have method= and such.) In that post (http://www.infoq.com/articles/properties-metaprogr...) it also mentions that the Java community is discussing it too. Thanks for the useful link as well! Eventually, I will get more into C#. I don't think I've touched it since .NET version 1.1. I've read things have changed mightily since then.

@Peter- I know(!) the possibility in Scorpio, and I can't wait for the missing method handler, because it will allow more run-time generation of code (which you know I love!) that you are talking about.

@Vince and Peter- But I was thinking on a more general level in languages where it is not currently supported, such as Java and CF.

Even when we manually write the code, could we do away with the getX() and setX() methods and have one method - X(if argument exists, set, otherwise get) - to do both (even with the parens).

Of course we /can/ do it, as it is quite easy to do in both. But can we slaughter the sacred cow as a general programming paradigm (in those languages where the get/set idiom reigns supreme), or is it too entrenched in our world? Should I forget about it because it will never catch on, and no one will understand my code if I were to pursue that path?

@Peter's 2nd post: yes, for the generation you are right, we'd need a list of gettable and settable properties (but, we still need it in the get/set idiom just to be clear to others). I would probably implement it such that by default all fields are gettable/settable and you can exclude them. Of course, overwriting the method to support your own get/set calculations rather than returning the raw variable is easy since the generation is all done via inheritance (in cfrails, anyway). And in the case of a missing method handler, the method won't be missing upon "redefining" it =)

Posted by Sam on May 15, 2007 at 07:14 PM UTC - 5 hrs

I'm not a huge fan of get/set either, I don't know why, it just annoys me :P

As Peter says with CF8's recently announced missing method handler it will make it possible to implement easily something like this in a standard way without too much extra work, which will definitely be cool.

The only real hurdle I see would be working with other devs who think differently and don't like doing things this way, perhaps for lack of clarity or some other reason?

But yes, +1 for being sick of get/set, haha :)

Posted by Justin Carter on May 15, 2007 at 07:35 PM UTC - 5 hrs

It's not built into the core language, but it's quite common for Perl modules to provide this type of getter/setter to the application programmer. There are other modules which you module can use to generate them automatically, too.

Almost every OO module (which is most of them these days) that I write has a getter/setter that gets with no arguments and sets with an argument (and usually returns the value just set).

Posted by Christopher E. Stith on May 16, 2007 at 11:27 AM UTC - 5 hrs

@Christopher - was there ever a history of get/set in Perl, or has it sort of "always" been the case that using context from number of arguments has been the way to do it? That's what I'm wanting in languages that use the get/set!

Posted by Sam on May 16, 2007 at 12:17 PM UTC - 5 hrs

Sam,
Sorry about the delay. I got busy there for a couple of months... ;-)

OO in Perl is actually a newer functionality introduced in Perl 5. There is a standard OO having to do with blessed references being an object instance. There are also a handful of other popular OO frameworks, and it's possible to roll your own. Some of these enforce encapsulation and some allow you to break it if you like.

So I guess the short answer is, "it depends". That's the story of much when talking about Perl. The language slogan is "There's More Than One Way To Do It", which is used often enough that it's familiar to most Perl programmers on site as TIMTOWTDI or TMTOWTDI, and it even has an accepted pronunciation "timtoady".

As for most modules I've seen, a separate get/set are not necessary. Some modules do use them, but it's considered more Perlish not to do so explicitly. Some Perl modules which wrap C or C++ libraries will actually replace get/set semantics with a combined getter/setter.

There are lvalue subroutines in Perl, so making your own get/set should actually be easy if someone wanted to do so.

Posted by Christopher E. Stith on Jul 25, 2007 at 07:53 PM UTC - 5 hrs

s/on site as/on sight as/;

That should make things a bit less confusing, as I wasn't meaning a particular place or web site and forgetting to mention it.

Posted by Christopher E. Stith on Jul 25, 2007 at 07:55 PM UTC - 5 hrs

@Christopher: It's quite alright, I completely understand!
Thanks for the follow-up as well, it was enlightening.

Posted by Sam on Jul 27, 2007 at 02:25 PM UTC - 5 hrs

hey, i've also found properties explanations here https://codeasy.net - it is a kind of tutorial, where you solve tasks while reading short stories) the way of explaining is rather new and fun))

Posted by gawin on Sep 01, 2017 at 09:50 AM UTC - 5 hrs

Nice one

Posted by Google on Sep 13, 2020 at 11:32 PM UTC - 5 hrs

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)
Website

Comment:

Subcribe to this comment thread
Remember my details
Google
Web CodeOdor.com

Me
Picture of me

Topics
.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and Cplusplus (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Resources
Agile Manifesto & Principles
Principles Of OOD
ColdFusion
CFUnit
Ruby
Ruby on Rails
JUnit



RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner