You are viewing a single thread.
View all comments View context
12 points

Totally agree. The hardcoded isAdult: true repeated in all #2 examples seems like a bug waiting to happen; that should be a property dynamically computed from the age during access time, not a static thing.

permalink
report
parent
reply
6 points

Or just a function. IMO computer properties are an anti pattern. Just adds complexity and confusion around what is going on - all to what? Save on a () when you access the value?

permalink
report
parent
reply
4 points
*

Properties make semantic sense. Functions do something, while properties are something. IMO if you want to name something lazily evaluated using a noun, it should be a property.

permalink
report
parent
reply
5 points

Functions do something, while properties are something.

This is my argument against them. Computed properties do something, they compute a value. This may or may not be cheap and adds surprising behavior to the property. IMO properties should just be cheap accessors to values. If it needs to be computed then seeing a function call can hint the caller may want to cache the value in a variable if they need to use it multiple times. With properties you need to look it up to know it is actually doing work instead of just giving you a value. That is surprising behavior which IMO I dislike in programs.

permalink
report
parent
reply
4 points

Properties are great when you can cache the computation which may be updated a little slower than every time it’s accessed. Getter that checks if an update is needed and maybe even updates the cached value then returns it. Very handy for lazy loading.

permalink
report
parent
reply
4 points

Functions can do all this. Computed properties are just syntactic sugar for methods. That is it. IMO it makes it more confusing for the caller. Accessing a property should be cheap - but with computed properties you don’t know it will be. Especially with caching as your example. The first access can be surprisingly slow with the next being much faster. I prefer things to not do surprising things when using them.

permalink
report
parent
reply

Programming

!programming@programming.dev

Create post

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



Community stats

  • 3.8K

    Monthly active users

  • 799

    Posts

  • 6.5K

    Comments