Naming functions

104 views
Skip to first unread message

Lars Jacobsson

unread,
Oct 24, 2016, 7:18:59 AM10/24/16
to Elm Discuss
Hey you guys! I've started to build out my first real Elm app and I'm loving it. On my model there is a field called items

 Model
   
...
    items
: List Item
   
...

which is a list of the type Item. Coming from a rails mindset I created an "updateItemById" -function, which I use like this in my update
 case msg of
   
...
   
OnSubmit updatedItem ->
       
{ model | items = updateItemById updatedItem }
but then it dawned on me that this does not read very well in this context. We'll be getting back a list of items, while "updateItem" implies that we are working with a single item. A CORRECT name for the function would be something like getNewItemsListWithReplacedItemById which works, but is not very elegant. Of course I could make it a little shorter, but you get the idea.

Any thinking going out there on around naming conventions OOP vs functional? I'd be grateful for any input on this matter of life or death!

Wouter In t Velt

unread,
Oct 24, 2016, 9:10:53 AM10/24/16
to Elm Discuss
Op maandag 24 oktober 2016 13:18:59 UTC+2 schreef Lars Jacobsson:
Any thinking going out there on around naming conventions OOP vs functional? I'd be grateful for any input on this matter of life or death!

I always try to keep my naming conventions close to the Core functions.
E.g. in my Elm app, I have functions similar to yours. They are called listUpdate and listInsert with the following signatures:

listUpdate : Int -> (RecordWithID a -> RecordWithID a) -> List (RecordWithID a) -> List (RecordWithID a)
listUpdate index updatefunction list =

listInsert
:
Int -> RecordWithID a -> List (RecordWithID a) -> List (RecordWithID a)
listInsert index newRecord list =

type
alias RecordWithID a = { a | id : Int }

The names were inspired by Dict.insert and Dict.update, which were the closest to what I was looking for.

Lars Jacobsson

unread,
Oct 24, 2016, 9:35:56 AM10/24/16
to Elm Discuss
| The names were inspired by Dict.insert and Dict.update, which were the closest to what I was looking for.

Yeah, I'm probably just too used to that dot notation.


I don't know why but
items.replaceItemById id item
looks better than
replaceItemById items id item
. Somehow it feels like a standalone function named "replaceItemById" won't give us a list in return. But taking your idea to heart this would then be something like itemsUpdateById

Max Goldstein

unread,
Oct 24, 2016, 10:13:33 AM10/24/16
to Elm Discuss
If you're going to refer to items by ID a lot, you should probably use a dictionary keyed on ID. Assuming items is a record with an id field:

{ model |
items = model.items |> Dict.insert updatedItem.id updatedItem }

Lars Jacobsson

unread,
Oct 24, 2016, 10:45:24 AM10/24/16
to Elm Discuss
Ok Max, thanks I'll give it a go and see how it works out.

Mark Hamburg

unread,
Oct 24, 2016, 3:57:06 PM10/24/16
to elm-d...@googlegroups.com

Elm style puts the items last so that you can write:

    items |> replaceItemById id item
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joey Eremondi

unread,
Oct 24, 2016, 3:59:26 PM10/24/16
to elm-d...@googlegroups.com
Elm style puts the items last

Also, putting the item last lets you partially apply the rest of the function, so if you wanted to replace by ID on a bunch of items, you could do

List.map (replaceItemById id) someListOfItems.
Reply all
Reply to author
Forward
0 new messages