Files
thpeetz-notes/Clippings/Stop Doing This Or Nobody Will Understand Your Code.md
T

4.3 KiB
Raw Blame History

title, source, author, published, created, description, tags
title source author published created description tags
Stop Doing This Or Nobody Will Understand Your Code https://medium.com/coding-beauty/methods-vs-properties-077cf44e972b
Tari Ibaba
2024-10-16 2024-10-29 This rule doesnt just apply to async tasks, it applies to any long-running action, synchronous or not: It doesnt just modify the backing _fullness field -- it changes multiple other fields. This…
clippings

[

Tari Ibaba

](https://medium.com/@tariibaba?source=post_page---byline--077cf44e972b--------------------------------)

[

Coding Beauty

](https://medium.com/coding-beauty?source=post_page---byline--077cf44e972b--------------------------------)

I was coding and stumbled upon something really disappointing the other day.

Do you see it? 👇

Lets zoom in a bit more:

This line:

Please dont do this in any language.

Dont await properties.

Youre destroying your code readability and ruining the whole concept of OOP.

Properties are features not actions.

They dont do like methods. They are.

They are data holders representing states of an object.

Simple states:

Derived states — what getters are meant for:

But the status property was returning a Dart Future -- JavaScript's Promise equivalent:

Before:

It would have been so much better to use a method:

After:

And now async/await can make things even more intuitive:

But guess what happens when you try async/await with properties?

Exactly. Its a property.

This rule doesnt just apply to async tasks, it applies to any long-running action, synchronous or not:

Before:

After:

Let them know that the action is expensive enough to deserve caching or variable assignment:

But sometimes it still doesnt deserve to be a property with this.

Check this out — do you see the issue with the level setter property?

It doesnt just modify the backing _fullness field -- it changes multiple other fields. This doesn't make sense as a property, as data.

Its affecting so much aside from itself.

It has side-effects.

Setting this property multiple times modifies the object differently each time.

So even though it doesnt do much, it still needs to be a method.

Name them right

Natural code. Coding like natural language.

So always name the properties with nouns like weve been doing here.

But you see what we did when it was time to make it a property?

We made it a verb. Cause now its an action that does something.

Nouns for entities: variables, properties, classes, objects, and more.

Not this:

But this:

Verbs for actions: functions and object methods.

Key points: when to use a method vs a property?

Use a property when:

  • The action modifies or returns only the backing field.
  • The action is simple and inexpensive.

Use a method when:

  • The action modifies multiple fields.
  • The action is async or expensive.

All for clean, readable, intuitive code.