Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To be or not to be: custom types #38

Open
badlydrawnrob opened this issue Aug 16, 2024 · 0 comments
Open

To be or not to be: custom types #38

badlydrawnrob opened this issue Aug 16, 2024 · 0 comments

Comments

@badlydrawnrob
Copy link
Owner

badlydrawnrob commented Aug 16, 2024

A few notes and examples from the forums.
My attempt at a custom type and other people's suggestions.

Simple rules to follow

I start with the caveat that for me, as a "non-programmer" just make your apps as simple as feasibly possible. Then handover to someone who knows what they're doing when you scale up!

  1. Start with the simplest thing first, until you find that you need a custom type to convey useful information.
    • It's probably right to say that much of the time, regular Elm data types are just fine.
  2. As soon as you see complexity and complection arise ... STOP! Simplify it.
    • You don't need to set default data for optional form fields. Don't bother to Json.Encode them
    • You'll have missing sections in your data but just treat them as empty views.
  3. Make sure to think through your data structures first. Only use a type Custom if it describes the problem better, or does something a simple data type can't.
    • There's nothing wrong with Maybe, just don't overuse it.
    • Here's a very good reason to use a custom type. Tags could also be a great reason.
    • You might want to distinguish things like Id Int from OtherId Int in your code.
  4. Entries Entry (List Entry) could be better than Entries (List Entry)
  5. But codecs and hacking json to replicate types can make life complicated.
  6. Sometimes you may want to reach for packages that do the work for you
    • Such as RemoteData, nonempty lists or List-Extra
  7. But again, we come back to (1) — Keep it simple, stupid!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant