Tuesday, January 29, 2008

Readers-/Authors field one liner

While doing som googling today I discovered that NotesDocument.ReplaceItemValue returns NotesItem. The reason I haven't discovered this before is probably because I've been lazy, and used the "dot" notation.

What I was googling for was actually advantages of using Get-/ReplaceItemValue versus the dot-notation. I've mostly used those two methods in loops, when the dot-notation would be horrid and evil to use (the copy-paste-then-edit-index-method). My quest recently is to create readable code, so I'll try using getters/replacers ( :| ) over dot-notation, and see how I feel about that.

To the point of this post.. I discovered the return value in a comment to Andre Guirard's blogpost/article, GetItemValue and ReplaceItemValue vs. "dot-notation".

Since Get-/ReplaceItemValue returns a NotesItem, you can write:doc.ReplaceItemValue( "read_access",_
"Arthur Dent/Earth" ).IsReaders = True

doc.ReplaceItemValue( "write_access",_
"Ford Prefect/Megadodo Publications" ).IsAuthors = True


I'm not totally confident with the readability of the above. A little more readable approach (?):Dim readAccessItem As NotesItem
Set readAccessItem = doc.ReplaceItemValue(_
"read_access","Arthur Dent/Earth" )
readAccessItem.IsReaders = True

Dim writeAccessItem As NotesItem
Set writeAccessItem = doc.ReplaceItemValue(_
"write_access", "Ford Prefect/Megadodo Publications" )
writeAccessItem.IsAuthors = True


Maybe none of the above is easy to read to people unfamiliar with ReplaceItemValue, but the Lotus Notes LotusScript API doesn't always make it easy to write readable code. I wish all chainable methods were as readable as this:Call session.currentDatabase.allDocuments.removeAll()

5 comments:

Nate said...

Tommy,

I love you.

I just wanted you to know that.

10 YEARS I've been writing LS code and I never read that part of the manual. *sigh*

Charles Robinson said...

I tend to shy away from the chained things because of the readability issue. I have been using ReplaceItemValue to return a NotesItem for about as long as Nathan hasn't been aware it's an option. ;-)

Nate said...

For the record, I am a huge fan of chaining. I suppose if there's a readability problem, I just never noticed it. It makes sense to me, and I often nest them four or five deep.

Mike Miller said...

When I started out with LS, I would just throw away the item created from the ReplaceItemValue method. Now, if I don't need the item I simply call the method instead of setting the object.

Did a 'duh' the day I woke up and realized what I'd been doing for a while...

Tommy Valand said...

@Nate: I appreciate your love :P

The Notes/Domino documentation is a beast, and I suspect the reason we overlook stuff like this is somehow connected to how the information is structured.