Combined Getter and Setter

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Combined Getter and Setter

Wayne Stewart
Hi,

With all the recent object stuff, I notice most people are using either
Cannon's excellent OBJ module or similar.

I have extended it so I have one routine do both tasks, I like this as it
means I only have one method to remember and not two.  There is obviously
some overhead copying the variable into $0 but I've never seen any impact
from this.  At least not with the variables I've used.

Does anyone else do this?

If you don't, why not?

Eg (based on Cannon's module):

C_OBJECT($AnObject_o)
C_TIME($WhenStarted_h;$WhenFinished_h)

$AnObject_o:=OBJ_Create
OBJ_Set_Time ($AnObject_o;"Start";Current time)
$WhenStarted_h:=OBJ_Get_Time ($AnObject_o;"Start")

FO_Time ($AnObject_o;"Finished";Current time)
$WhenFinished_h:=FO_Time ($AnObject_o;"Finished")

Internally FO_Time does this:

C_OBJECT($1;$Object_o)
C_TEXT($2;$Key_t)  //Can use dot notation
C_TIME($3;$Value_h;$0)

$Object_o:=$1
$Key_t:=$2

If (Count parameters=3)
  $Value_h:=$3
  OBJ_Set_Time ($Object_o;$Key_t;$Value_h)
Else
  $Value_h:=OBJ_Get_Time ($Object_o;$Key_t)
End if

$0:=$Value_h  //  Always return a value, 4D doesn't mind if you don't
receive it


Regards,

Wayne


[image: --]
Wayne Stewart
[image: http://]about.me/waynestewart
<http://about.me/waynestewart>
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

Douglas von Roeder
On Sun, Jan 8, 2017 at 4:47 PM, Wayne Stewart <[hidden email]>
wrote:

> With all the recent object stuff, I notice most people are using either
> Cannon's excellent OBJ module or similar.
>
> I have extended it so I have one routine do both tasks, I like this as it
> means I only have one method to remember and not two.  There is obviously
> some overhead copying the variable into $0 but I've never seen any impact
> from this.  At least not with the variables I've used.
>
> Does anyone else do this?
>
> If you don't, why not?
>



Ivory tower from the folks folks who write about computer sciency stuff -
"a method should have one specific defined purpose".

Practical impact - when reading through legacy code, it can be hard enough
to understand what the former developer was doing. Having to switch modes
to know if I'm working with a method or a function does not make it easier
to read the code. One of my goals in designing code is to make things as
easy as possible to understand and having a method that goes both ways is a
fail.

Practical impact - I've created thousands of setters and getters since I
started using OT Scripter and I've *never* felt that I've got "too many
methods".


--
Douglas von Roeder
949-336-2902
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

David Adams-4
In reply to this post by Wayne Stewart
> Does anyone else do this?

Hey Wayne, great question! I've always instinctively and passionately hated
combined getter setters. And I've never known why. I'd be really interested
in hearing from anyone that can articulate what they like and/or dislike
about them. Wayne, you say that you like only having one routine to
remember. I can see that. I do know that I tend to dislike the names that
such routines have because it's a bit confusing to me...

Anyone have good arguments either way? I'm open minded about changing my
thinking. Well, maybe not entirely, but at least a little ;-)
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

KirkBrooks
On Sun, Jan 8, 2017 at 5:16 PM, David Adams <[hidden email]> wrote:

> > Does anyone else do this?
>
> Hey Wayne, great question! I've always instinctively and passionately hated
> combined getter setters.

​Yeah - I agree. Combining getting and setting is way more complicated than
separating them by function. This is one of those places where the elegance
of fewer methods is not worth the increased ​complexity to make it work.

--
Kirk Brooks
San Francisco, CA
=======================
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

Arnaud de Montard

> Le 9 janv. 2017 à 05:22, Kirk Brooks <[hidden email]> a écrit :
>
> On Sun, Jan 8, 2017 at 5:16 PM, David Adams <[hidden email]> wrote:
>
>>> Does anyone else do this?
>>
>> Hey Wayne, great question! I've always instinctively and passionately hated
>> combined getter setters.
>
> ​Yeah - I agree. Combining getting and setting is way more complicated than
> separating them by function. This is one of those places where the elegance
> of fewer methods is not worth the increased ​complexity to make it work.

I have no "religion" about one method or setter+getter, it depends. My only personal hate about setters and getters is naming, more exactly the verb position.
in 4D langage:
  OBJECT Get enabled or OBJECT enabled Get?
  LISTBOX DELETE COLUMN or LISTBOX COLUMN DELETE?
in a component:
  Fs_pathGetParent or Fs_pathParentGet?
  Ob_setTime or Ob_timeGet?
During code input, I always think this way: listbox.column.delete. Never the reverse. Kind of hierarchic notation, BTW. I'd like 4D commands to respect that logic. And since predictive input, definitely the verb at second position is a mess, try for example to input "listbox get+tab" and see the list of proposal: huge!


Another thing about naming, is a method with or without $0? In 4D, commands are upper case, functions lower case. At glance, but all upper case sting my eyes. In my methods, I use the chapter prefix:
   Fs_pathParentGet --> function
   CSV_importAssistant --> command
Just a matter of taste.

--
Arnaud de Montard



**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

David Adams-4
> ​Yeah - I agree. Combining getting and setting is way more complicated
than
> separating them by function. This is one of those places where the
elegance
> of fewer methods is not worth the increased ​complexity to make it work.

...just attributed to me, but actually from Kirk. For what it's worth, I
don't hesitate to write more methods. I've got lots and lots of
methods...doesn't trouble me.
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

Jim Dorrance
I use (eg):

TBox_Bits_nGET
TBox_Bits_nSet

TBox_FormObject_Text_tGET
TBox_FormObject_Text_tSet

I see which is a function (GET)

And I see what kind of value is passed/returned (n = numeric, t = text)


--
Jim Dorrance
[hidden email]
[hidden email]
www.4d.dorrance.eu

PS: If you know of anyone that needs an experienced 4D programmer to add
energy and experience to their team, please let me know. I have
experience in many areas. Reasonable rates. Remote or Paris only.
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

KirkBrooks
In reply to this post by Arnaud de Montard
Arnaud,

On Sun, Jan 8, 2017 at 11:49 PM, Arnaud de Montard <[hidden email]> wrote:

> My only personal hate about setters and getters is naming, more exactly
> the verb position.
>
​We are perilously close to setting off a discussion about naming
conventions ... but you're right.
I seem to be settling on a general convention of

module_verb_descriptor​

​so​

​Object_get_whatever​


​I really want to the methods to sort together by 'module' - but as soon as
I say that I can think of several times I violated it.
As they say, "it depends."​

--
Kirk Brooks
San Francisco, CA
=======================
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

Richard Wright-2
In reply to this post by Wayne Stewart
I use getter/setters all the time - mainly for handling flags (boolean process variables). I do the following:


MyFlagMethod
===============
c_boolean($0)
c_boolean($1;fMyFlag)

If(count parameters >= 1)
        fMyFlag := $1
Else
        $0 := fMyFlag
End if


The reason I do this for flags is because I can put my comments that describe the usage in one place instead of spread out all over the code everywhere they are used (there are few things more frustrating than trying to track down how and why process variables are used.) The code is intuitively readable if you name the method well. For example, I may need a flag to tell me the current context, e.g., IsCalledFromInputForm. In use it looks like this:

IsCalledFromInputForm(True)
...
If (IsCalledFromInputForm)
   `do something
Endif
...
IsCalledFromInputForm(False)


And the getter/setter is so simple that having 2 methods to do the same thing ends up being more complex. It also has the advantage of guaranteeing that the boolean variable is always defined so you don’t get runtime errors.

------------------------------------------------
Richard Wright
DataDomain
[hidden email]
————————————————————————

> Date: Mon, 9 Jan 2017 11:47:01 +1100
> From: Wayne Stewart <[hidden email]>
>
> Hi,
>
> With all the recent object stuff, I notice most people are using either
> Cannon's excellent OBJ module or similar.
>
> I have extended it so I have one routine do both tasks, I like this as it
> means I only have one method to remember and not two.  There is obviously
> some overhead copying the variable into $0 but I've never seen any impact
> from this.  At least not with the variables I've used.
>
> Does anyone else do this?
>
> If you don't, why not?
>
> Eg (based on Cannon's module):
>
> C_OBJECT($AnObject_o)
> C_TIME($WhenStarted_h;$WhenFinished_h)
>
> $AnObject_o:=OBJ_Create
> OBJ_Set_Time ($AnObject_o;"Start";Current time)
> $WhenStarted_h:=OBJ_Get_Time ($AnObject_o;"Start")
>
> FO_Time ($AnObject_o;"Finished";Current time)
> $WhenFinished_h:=FO_Time ($AnObject_o;"Finished")
>
> Internally FO_Time does this:
>
> C_OBJECT($1;$Object_o)
> C_TEXT($2;$Key_t)  //Can use dot notation
> C_TIME($3;$Value_h;$0)
>
> $Object_o:=$1
> $Key_t:=$2
>
> If (Count parameters=3)
>  $Value_h:=$3
>  OBJ_Set_Time ($Object_o;$Key_t;$Value_h)
> Else
>  $Value_h:=OBJ_Get_Time ($Object_o;$Key_t)
> End if
>
> $0:=$Value_h  //  Always return a value, 4D doesn't mind if you don't
> receive it




**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

Arnaud de Montard
In reply to this post by KirkBrooks

> Le 9 janv. 2017 à 15:47, Kirk Brooks <[hidden email]> a écrit :
> I seem to be settling on a general convention of
> module_verb_descriptor​

Maybe I'm not conventional…
Many old 4D commands with verb first are really a mess to use (try "Set+tab" in code…). In recent chapters and some others, 4D began to use verb-in-second. Better but not enough IMHO. The "listbox" chapter in 4D commands is a good example to illustrate why; left is the way it is, right with verb at end (I made it fast, just to get the idea):
<http://screencast.com/t/HdxuTGRrB>
From the list on the right:
1 commands are ordered by sub chapter inside the chapter (array, cell, column, grid…)
2 getter and setter are together
3 words are ordered the way we act with objects, hierarchic; when the langage is hierarchic, event if you don't remember the command name exactly, input is like a funnel: "listbox.column+tab": the coming list is short.

--
Arnaud de Montard


**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

Douglas von Roeder
In reply to this post by Arnaud de Montard
On Sun, Jan 8, 2017 at 11:49 PM, Arnaud de Montard <[hidden email]> wrote:

> I have no "religion" about one method or setter+getter, it depends. My
> only personal hate about setters and getters is naming, more exactly the
> verb position.



That's object-action vs action-object. When I drank the accessor method
Koolaid I had to decide on a naming convention and I chose object-action
for two reasons.

One was that my most of my personal computer background was on on the Mac
which is object-action in that we highlight some text, for example, and do
something with it. In contrast, the DOS and Windows world where the
paradigm was action-object - going back to Wordstar, where you would being
a marked block of text type some text and then end a marked block of text.

The other reason was that my focus is on the data. The method
*GRID_LineSel_Set* sorts with other methods that involve lines, for
example. When using type ahead, selecting the getter means that I just hit
tab or, if I want the setter, all I have to do is hit the down arrow and
then tab. Similarly, my variables have a type suffix at the end of name,
not at the front.


--
Douglas von Roeder
949-336-2902
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

Arnaud de Montard

> Le 9 janv. 2017 à 18:05, Douglas von Roeder <[hidden email]> a écrit :
>
> That's object-action vs action-object. [...]
> Similarly, my variables have a type suffix at the end of name,
> not at the front.

At least I'll feel at home if I have to read some code of yours  :-)

--
Arnaud de Montard



**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

Bernard Escaich-3
In reply to this post by Wayne Stewart
I have published a component on Forum4D with that comment :

This component emulates 4D standard commands for objects such as OB Set or OB Get to add access to properties or sub-objects by path with dot notation.
It has been clearly inspired by OBJ_Module of Cannon Smith but differs by :
- it emulates standard commands and does not have commands by type
- it resolves paths with arrays elements
- it gives some advanced controls such as search in an array by the value of a property.
It does not intend to replace all functions of OBJ_Module.
You can download it here <http://forums.4d.fr/4DBB_Main/x_User/4018/files/18896249.zip>.
Method OBJ_Test gives several operational examples ; some functions are not yet implemented.

My answer is that I separate setter and getter ; the two commands have same syntax and you can copy the setter, change one letter and have the getter
     OBJ_Set(MyObject;"MyPath";->MyValue)
     ... do something
     OBJ_Get(MyObject;"MyPath";->MyValue)


Direct access to the post where you can download the component : http://forums.4d.fr/Post//18896257/1/ <http://forums.4d.fr/Post//18896257/1/>

Cordialement,

Bernard Escaich


> Le 9 janv. 2017 à 01:47, Wayne Stewart <[hidden email]> a écrit :
>
> Hi,
>
> With all the recent object stuff, I notice most people are using either
> Cannon's excellent OBJ module or similar.
>
> I have extended it so I have one routine do both tasks, I like this as it
> means I only have one method to remember and not two.  There is obviously
> some overhead copying the variable into $0 but I've never seen any impact
> from this.  At least not with the variables I've used.
>
> Does anyone else do this?
>
> If you don't, why not?
>
> Eg (based on Cannon's module):
>
> C_OBJECT($AnObject_o)
> C_TIME($WhenStarted_h;$WhenFinished_h)
>
> $AnObject_o:=OBJ_Create
> OBJ_Set_Time ($AnObject_o;"Start";Current time)
> $WhenStarted_h:=OBJ_Get_Time ($AnObject_o;"Start")
>
> FO_Time ($AnObject_o;"Finished";Current time)
> $WhenFinished_h:=FO_Time ($AnObject_o;"Finished")
>
> Internally FO_Time does this:
>
> C_OBJECT($1;$Object_o)
> C_TEXT($2;$Key_t)  //Can use dot notation
> C_TIME($3;$Value_h;$0)
>
> $Object_o:=$1
> $Key_t:=$2
>
> If (Count parameters=3)
>  $Value_h:=$3
>  OBJ_Set_Time ($Object_o;$Key_t;$Value_h)
> Else
>  $Value_h:=OBJ_Get_Time ($Object_o;$Key_t)
> End if
>
> $0:=$Value_h  //  Always return a value, 4D doesn't mind if you don't
> receive it
>
>
> Regards,
>
> Wayne
>
>
> [image: --]
> Wayne Stewart
> [image: http://]about.me/waynestewart
> <http://about.me/waynestewart>
> **********************************************************************
> 4D Internet Users Group (4D iNUG)
> FAQ:  http://lists.4d.com/faqnug.html
> Archive:  http://lists.4d.com/archives.html
> Options: http://lists.4d.com/mailman/options/4d_tech
> Unsub:  mailto:[hidden email]
> **********************************************************************

**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Combined Getter and Setter

Bernard Escaich-3
In reply to this post by Wayne Stewart
I have published a component on Forum4D with that comment :

This component emulates 4D standard commands for objects such as OB Set or OB Get to add access to properties or sub-objects by path with dot notation.
It has been clearly inspired by OBJ_Module of Cannon Smith but differs by :
- it emulates standard commands and does not have commands by type
- it resolves paths with arrays elements
- it gives some advanced controls such as search in an array by the value of a property.
It does not intend to replace all functions of OBJ_Module.
You can download it here <http://forums.4d.fr/4DBB_Main/x_User/4018/files/18896249.zip>.
Method OBJ_Test gives several operational examples ; some functions are not yet implemented.

For this thread, I separate setter and getter ; the two commands have same syntax and you can copy the setter, change one letter and have the getter
     OBJ_Set(MyObject;"MyPath";->MyValue)
     ... do something
     OBJ_Get(MyObject;"MyPath";->MyValue)

Direct access to the post where you can download the component : http://forums.4d.fr/Post//18896257/1/ <http://forums.4d.fr/Post//18896257/1/>

Best regards,

Bernard Escaich


> Le 9 janv. 2017 à 01:47, Wayne Stewart <[hidden email]> a écrit :
>
> Hi,
>
> With all the recent object stuff, I notice most people are using either
> Cannon's excellent OBJ module or similar.
>
> I have extended it so I have one routine do both tasks, I like this as it
> means I only have one method to remember and not two.  There is obviously
> some overhead copying the variable into $0 but I've never seen any impact
> from this.  At least not with the variables I've used.
>
> Does anyone else do this?
>
> If you don't, why not?
>
> Eg (based on Cannon's module):
>
> C_OBJECT($AnObject_o)
> C_TIME($WhenStarted_h;$WhenFinished_h)
>
> $AnObject_o:=OBJ_Create
> OBJ_Set_Time ($AnObject_o;"Start";Current time)
> $WhenStarted_h:=OBJ_Get_Time ($AnObject_o;"Start")
>
> FO_Time ($AnObject_o;"Finished";Current time)
> $WhenFinished_h:=FO_Time ($AnObject_o;"Finished")
>
> Internally FO_Time does this:
>
> C_OBJECT($1;$Object_o)
> C_TEXT($2;$Key_t)  //Can use dot notation
> C_TIME($3;$Value_h;$0)
>
> $Object_o:=$1
> $Key_t:=$2
>
> If (Count parameters=3)
>  $Value_h:=$3
>  OBJ_Set_Time ($Object_o;$Key_t;$Value_h)
> Else
>  $Value_h:=OBJ_Get_Time ($Object_o;$Key_t)
> End if
>
> $0:=$Value_h  //  Always return a value, 4D doesn't mind if you don't
> receive it
>
>
> Regards,
>
> Wayne
>
>
> [image: --]
> Wayne Stewart
> [image: http://]about.me/waynestewart
> <http://about.me/waynestewart>
> **********************************************************************
> 4D Internet Users Group (4D iNUG)
> FAQ:  http://lists.4d.com/faqnug.html
> Archive:  http://lists.4d.com/archives.html
> Options: http://lists.4d.com/mailman/options/4d_tech
> Unsub:  mailto:[hidden email]
> **********************************************************************

**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[hidden email]
**********************************************************************