Extensions: val vs zero-arg fun

If you’re creating an extension, and your choices are either a val (extension property) or zero-arg fun (extension method)…what are people’s criteria in making that choice?

I find myself relying on “gut feeling” than I like, so…if there are any rules/guidelines I should know about, I’d love to hear them.

Thanks!

When it is a non-trivially computed value I don’t use properties.

Coding Conventions: Functions vs Properties

Thanks, I was actually looking on this page for something like this, but didn’t find it.

I was hoping for more semantic guidelines. To me, properties make sense when you’re accessing an attribute (field or computed) of an instance, while methods are more like operations that do real work. But if you have a property that requires some non-trivial amount of work to calculate…it blurs the lines.

Yeah it does. I think it’s up to each programmer to decide case by case what is to much work for a property and what isn’t.

Another rule I would add to the style is that properties should have no side effects. At least the getter shouldn’t. Maybe it’s even worth to create a PR to add this rule.

1 Like

At least no external side effects. Property could be lazy and thus change internal state of the object.