question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider using functions instead of getter properties

See original GitHub issue

This is most definitely a style preference, but I think many newcomers will be confused by the use of dynamically defined properties that return promises here, and I think they could lead to bugs due to their magicness.

Is there a rationale for preferring:

let html = await r2('https://www.google.com').text

over

let html = await r2('https://www.google.com').text()

Especially considering that the latter aligns with the Fetch API that users might be familiar with?

Since the promises returned here perform I/O, I think it’s a stronger signal to the reader to use functions instead of properties.

It’s a relatively well accepted maxim in languages that have had property methods for a while (eg, C#) that they should still be inexpensive wrappers to internal state instead of performing any real work – as this goes against users’ expectations of what a property is.

Again, totally just a personal preference – but I think it falls well within the principle of least astonishment.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:30
  • Comments:10

github_iconTop GitHub Comments

11reactions
gr2mcommented, Feb 27, 2018

we will change the API to functions, just doing some spring cleaning first 🛁✨

5reactions
mhartcommented, Sep 7, 2017

@MaikuMori yeah, that’s wrong – fetch doesn’t retrieve the body of the response, only the status code and headers. Methods such as text(), json(), etc are the ones responsible for actually streaming the response body, as described here: https://fetch.spec.whatwg.org/#concept-body-consume-body

You can test this by fetching a very large file – the initial fetch promise will return quickly (with whatever latency the initial connection requires), and you’ll have your response object that you can get the status or headers from – but if you want the large body, you’ll need to resolve one of the body methods, and that will stream the rest of the response over the network.

Read more comments on GitHub >

github_iconTop Results From Across the Web

3. Properties vs. Getters and Setters | OOP - Python Courses eu
Unfortunately, it is widespread belief that a proper Python class should encapsulate private attributes by using getters and setters.
Read more >
Why use Getters and Setters functions | Zell Liew
When you try to get the length of an array, you write array.length . This length property acts like a Getter function. It...
Read more >
Using @property versus getters and setters - Stack Overflow
Using properties lets you begin with normal attribute accesses and then back them up with getters and setters afterwards as necessary.
Read more >
Property getters and setters - The Modern JavaScript Tutorial
Getters and setters ... Accessor properties are represented by “getter” and “setter” methods. In an object literal they are denoted by get and...
Read more >
Kotlin: should I define Function or Property? | by Igor Wojda
One of them is property access syntax (we use height instead of getHeight/setHeight) which is more concise, another is the fact that getters...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found