nil2l
06/03/2020, 6:12 PMUse property if… is cheap to calculateWhen deciding to use property or function/method. Do you think this guideline about the calculation heaviness is significant? E.g. #1
val File.entropy: Float
get() = TODO()
#2
fun File.getEntropy(): Float = TODO()
streetsofboston
06/03/2020, 6:18 PMequals
among the returned objects, and any of those equals
calls may return false
) always use a function/method.
If it would always return the same object, use a property. If something needs to be lazily evaluated, still use a property, but implement its get()
getter.nil2l
06/03/2020, 6:50 PMSam Garfinkel
06/03/2020, 8:01 PMby lazy {}
streetsofboston
06/03/2020, 8:02 PMstreetsofboston
06/03/2020, 8:03 PMby lazy
quite a lot. For top level functions/properties, this is not possible and I use a get() =
instead.streetsofboston
06/03/2020, 8:06 PMList.size
). If it doesn’t, and it is more of a translations/mutations, I use function/method (e.g. toInt()
or toList()
or asXXXX()
, etc.)nil2l
06/03/2020, 8:10 PMstreetsofboston
06/03/2020, 8:17 PMstreetsofboston
06/03/2020, 8:18 PMgetEntropy()
, that is fine, but it wouldn’t be my first choice 🙂nil2l
06/03/2020, 8:33 PMstreetsofboston
06/03/2020, 8:39 PMi = i + 1
or i++
? Both are perfectly fine 🙂nil2l
06/03/2020, 8:39 PMstreetsofboston
06/03/2020, 8:39 PMnil2l
06/03/2020, 8:59 PMMatteo Mirk
06/04/2020, 12:52 PMnil2l
06/04/2020, 12:59 PMIn general, methods represent actions and properties represent data. Properties are meant to be used like fields, meaning that properties should not be computationally complex or produce side effects. When it does not violate the following guidelines, consider using a property, rather than a method, because less experienced developers find properties easier to use.https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms229054(v=vs.100)?redirectedfrom=MSDN
nil2l
06/04/2020, 12:59 PMMatteo Mirk
06/04/2020, 1:06 PM