83
submitted 1 week ago by [email protected] to c/[email protected]
you are viewing a single comment's thread
view the rest of the comments
[-] [email protected] 24 points 1 week ago* (last edited 1 week ago)

I largely agree with this nodding along to many of the pitfalls presented. Except numbers 2s good refactor. I hope I won't sound too harsh/picky for an example that perhaps skipped renaming for clarity on the other parts, but I wanted to mention it.

While I don't use javascript and may be missing some of the norms and context of the lanugage, creating lamda functions (i don't know the js term) and then hardcoding them into a function is barely an improvement. It's fine because they work well with map and filter, but it didn't address the vague naming. Renaming is refactoring too!

isAdult is a simple function with a clear name, but formatUser and processUsers are surprisingly vague. formatUser gives only adult FormattedUsers, and that should probably be highlighted in the name of formatUser now that it is a resuable function. To me, it seems ripe for mistaken use given that it is the filter that at a glance handles removing non-adult users before the formatting, while formatUser doesn't appear to exepct only adult users from it's naming or even use! Ideally, formatUser should have checked the age on it's own and set isAdult true/false accordingly, instead of assuming it will be used only on adult Users.

Likewise, the main function is called processUsers but could easily have been something more descriptive like GetAdultFormattedUsers or something similar depending on naming standards in js and the context it is used in. It may make more sense in the actual context, but in the example a FormattedUser doesn't have to be an adult, so a function processing users should clarify that it only actually creates adult formatted users since there is a case where a FormattedUser is not an adult.

[-] [email protected] 12 points 1 week ago

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.

[-] [email protected] 6 points 1 week ago

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?

[-] [email protected] 4 points 1 week ago* (last edited 1 week ago)

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.

[-] [email protected] 5 points 1 week ago

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.

[-] [email protected] 1 points 1 week ago

that we agree on: properties should be cheap to compute.

Making a simple ternary condition as a function instead of property is a wasted opportunity to make its usage cleaner.

[-] [email protected] 2 points 1 week ago

Make its usage cleaner? I don't see how a property does that at all. We are talking about x.foo vs x.foo() really. And IMO the latter tells you this is a function that needs to do some work even if that work is very cheap. x.foo implies that you might be able to set the value as well. But with computed properties maybe not. Which IMO makes the program a bit harder to read and understand as you cannot simply assume it is a simple assignment or field access. It could be a full function call that does different things depending on other values or even if you are setting vs getting the value. I prefer things being more explicit.

[-] [email protected] 0 points 1 week ago* (last edited 1 week ago)

Because you're assuming foo won't be renamed when it becomes a function. A function should start with a verb, say get_foo(), because just foo() tells me nothing about what the function does (or what to expect as output). If you make it a property, get_ is implicit.

So if the age is computed from the year of birth for example, it's really e.g. thing.age or thing.get_age() - both of which are fine, but I'd pick the property version.

[-] [email protected] 2 points 1 week ago

Or just thing.age() which is fine and is fairly obvious it will return the age of the thing. And that is what is done in most languages that dont have computed properties. get_ on a method really adds no value nor clarity to things. The only reason foo() is ambiguous is because it is a bad name - really just a place holder. Missing out the brackets here adds no value either, just makes it hard to tell that you are calling a function instead of just accessing a property.

[-] [email protected] 4 points 1 week ago

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.

[-] [email protected] 4 points 1 week ago

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.

[-] [email protected] 3 points 1 week ago

I get that, it’s a valid point. But in OOP, objects can be things and do things. That’s kinda the whole point. We’re approaching detailed criticism of contextless development concepts though so it kinda doesn’t matter.

load more comments (9 replies)
this post was submitted on 07 Sep 2024
83 points (93.7% liked)

Programming

16999 readers
118 users here now

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 [email protected]



founded 1 year ago
MODERATORS