# Is using get() not as an attribute getter a good practice?

**URL:** <https://discuss.kotlinlang.org/t/is-using-get-not-as-an-attribute-getter-a-good-practice/24104>\
**Category:** Language Design\
**Created:** [February 3, 2022, 8:07pm UTC](https://discuss.kotlinlang.org/t/is-using-get-not-as-an-attribute-getter-a-good-practice/24104 "2022-02-03T20:07:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![borvalcode](https://avatars.discourse-cdn.com/v4/letter/b/c5a1d2/32.png) [@borvalcode](https://discuss.kotlinlang.org/u/borvalcode)\
**Post date:** [February 3, 2022, 8:07pm UTC](https://discuss.kotlinlang.org/t/is-using-get-not-as-an-attribute-getter-a-good-practice/24104/1 "2022-02-03T20:07:42Z")

</div>

Hello, Is it a bad practise to use get() not exactly as an attribute getter, but as a val inside a companion object ?

Something like this:

```auto
class Foo() {

fun method() {
     server.start()
}

companion object {
    private val server get() = generateANewServer()
}
}

```

Instead of

```auto
class Foo() {

fun method() {
     newServer().start()
}

companion object {
    private fun newServer() = generateANewServer()
}
}

```

Thanks!

---

<div class="post-metadata">

**Author:** ![chipays](https://avatars.discourse-cdn.com/v4/letter/c/ad7895/32.png) [@chipays](https://discuss.kotlinlang.org/u/chipays)\
**Post date:** [February 3, 2022, 8:51pm UTC](https://discuss.kotlinlang.org/t/is-using-get-not-as-an-attribute-getter-a-good-practice/24104/2 "2022-02-03T20:51:57Z")

</div>

When to use a property vs a function is one of the topics mentioned in the [Kotlin coding conventions](https://kotlinlang.org/docs/coding-conventions.html#functions-vs-properties):

> Prefer a property over a function when the underlying algorithm:
> 
> - does not throw
> 
> - is cheap to calculate (or cached on the first run)
> 
> - returns the same result over invocations if the object state hasn’t changed

If `generateANewServer` isn’t a cheap function, or cached as some kind of singleton, or returns the same server, then I wouldn’t declare it as a property.
