Metaprogramming facilities

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Metaprogramming facilities

Victor RENE
In the TinyBlog mini-project of the MOOC, we start by creating the model.
However, since I basically finished the exercises, I wanted to go one step further and
ask you guys, what is the correct way _not_ to write all gettters / setters for the fields.
 
TBPost >> title
    ^ title
TBPost >> title: anObject
    title := anObject
TBPost >> text
    ^ text
TBPost >> text: anObject
    text := anObject
etc...
 
Do you do it with macros / templates / something else ?
I am having a bit of fun (although I would love that they unlock weeks 2 to 7 for me). Thanks in advance.

Victor RENE
Software engineer,
Game designer, Writer


tel: +33 6 26 83 61 76
 
Reply | Threaded
Open this post in threaded view
|

Re: Metaprogramming facilities

Guillaume Larcheveque
Select the class, right click on it:

Images intégrées 1

2016-05-02 17:35 GMT+02:00 Victor RENE <[hidden email]>:
In the TinyBlog mini-project of the MOOC, we start by creating the model.
However, since I basically finished the exercises, I wanted to go one step further and
ask you guys, what is the correct way _not_ to write all gettters / setters for the fields.
 
TBPost >> title
    ^ title
TBPost >> title: anObject
    title := anObject
TBPost >> text
    ^ text
TBPost >> text: anObject
    text := anObject
etc...
 
Do you do it with macros / templates / something else ?
I am having a bit of fun (although I would love that they unlock weeks 2 to 7 for me). Thanks in advance.

Victor RENE
Software engineer,
Game designer, Writer


tel: <a href="tel:%2B33%206%2026%2083%2061%2076" value="+33626836176" target="_blank">+33 6 26 83 61 76
 



--
Guillaume Larcheveque

Reply | Threaded
Open this post in threaded view
|

Re: Metaprogramming facilities

cedreek
In reply to this post by Victor RENE
Hi Victor,

I use the refactoring facilities offered by the contextual menu when a class is selected.


You should explore and try this submenus :)

Cheers,

Cédrik
Reply | Threaded
Open this post in threaded view
|

Re: Metaprogramming facilities

cedreek
In reply to this post by Victor RENE
Or even quicker with the shortcut  CMD+H+A

Cheers,
Cédrik
Reply | Threaded
Open this post in threaded view
|

Re: Metaprogramming facilities

stepharo
In reply to this post by Victor RENE

Code not tested but ....


#('title' 'text')

    do: [:each |

           | body |

            body := String new streamContents:

                    [ :s |

                    s nextPutAll: each ; cr; tab;

                        nextPutAll:  '^ ';

                        nextPutAll: each ; cr].

            TBPost compile: body classified: 'accessing'

]


Le 2/5/16 à 17:35, Victor RENE a écrit :
In the TinyBlog mini-project of the MOOC, we start by creating the model.
However, since I basically finished the exercises, I wanted to go one step further and
ask you guys, what is the correct way _not_ to write all gettters / setters for the fields.
 
TBPost >> title
    ^ title
TBPost >> title: anObject
    title := anObject
TBPost >> text
    ^ text
TBPost >> text: anObject
    text := anObject
etc...
 
Do you do it with macros / templates / something else ?
I am having a bit of fun (although I would love that they unlock weeks 2 to 7 for me). Thanks in advance.

Victor RENE
Software engineer,
Game designer, Writer


tel: +33 6 26 83 61 76
 

Reply | Threaded
Open this post in threaded view
|

Re: Metaprogramming facilities

Peter Uhnak
1) write them by hand

annoying typing required

2) use Class refactorings (right click on class -> refactoring -> class refactoring -> generate accessors; or ctrl+h+a)

this will ask you to choose for all attributes… good initially, but really stupid if you are providing e.g. just getters (because then you would need to cancel genreation for all the attributes)

3) use attribute refactorings (right click on class -> refactoring -> inst var refactoring -> accessors -> select attribute)

good for individual attributes

4) write the code compilation (ideally as part of some tool)

MyClass compile: 'attr ^ attr' classified: 'accessing'.
MyClass compile: 'attr: anObject attr := anObject' classified: 'accessing'.

Much more typing, but good if you have it as part of some tool (so you can quickly script a gui that would generate it just by clicking on a checkbox or something)

5) use refactoring (this is what the System Browser uses internally)

(RBCreateAccessorsForVariableRefactoring
variable: #attr class: MyClass classVariable: false) execute

6) use TDD (test driven development)

the Debugger will offer you to create the method and you can modify it as you work through (I don't know if TDD is already part of the broadcasted MOOC courses)


Typing by hand is annoying, but it's literally two words for getter and four for setter.
This mail is probably more words than all the accessors I've created this year… so typing it by hand is no big deal.
More importantly the autogenerated code is a bit stupid

TBPost >> title: anObject
    title := anObject 

this is stupid, so even for generated one I select the argument, press ctrl+t (or right click + suggestions) and select 'rename' ... and rename it into something meaningful like

TBPost >> title: aString
    title := aString


But otherwise I regularly use all 1, 2, 4, 5, 6…

I never use 3) it's too annoying and distracting because I have to read the labels… I can type in the getter/setter without breaking focus on what I am doing… but it just gave me an idea... :)

Peter

On Tue, May 3, 2016 at 9:05 PM, stepharo <[hidden email]> wrote:

Code not tested but ....


#('title' 'text')

    do: [:each |

           | body |

            body := String new streamContents:

                    [ :s |

                    s nextPutAll: each ; cr; tab;

                        nextPutAll:  '^ ';

                        nextPutAll: each ; cr].

            TBPost compile: body classified: 'accessing'

]


Le 2/5/16 à 17:35, Victor RENE a écrit :
In the TinyBlog mini-project of the MOOC, we start by creating the model.
However, since I basically finished the exercises, I wanted to go one step further and
ask you guys, what is the correct way _not_ to write all gettters / setters for the fields.
 
TBPost >> title
    ^ title
TBPost >> title: anObject
    title := anObject
TBPost >> text
    ^ text
TBPost >> text: anObject
    text := anObject
etc...
 
Do you do it with macros / templates / something else ?
I am having a bit of fun (although I would love that they unlock weeks 2 to 7 for me). Thanks in advance.

Victor RENE
Software engineer,
Game designer, Writer


tel: +33 6 26 83 61 76
 


Reply | Threaded
Open this post in threaded view
|

Re: Metaprogramming facilities

Peter Uhnak
More importantly the autogenerated code is a bit stupid

TBPost >> title: anObject
    title := anObject 

this is stupid, so even for generated one I select the argument, press ctrl+t (or right click + suggestions) and select 'rename' ... and rename it into something meaningful like

TBPost >> title: aString
    title := aString

This (usually) doesn't apply to 6 (TDD), because the generated correctly names it based on the real value that is being passed to it.