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 removal of convenience methods from Component

See original GitHub issue

_This issue is taken from https://github.com/SpongePowered/SpongeAPI/pull/2275#issuecomment-739912355_

_Also note that this is somewhat linked to https://github.com/KyoriPowered/adventure/issues/222_

TL;DR: The convenience methods on Component, such as style(...), color(...) etc. encourage bad habits and should be considered for removal in the next major release, or should be renamed to make it clear that they return a new immutable object.

For this, I’m mostly copy and pasting from the issue above.


Consider the two methods

Component.text();
Component.text(String);

These return two objects with a very important difference, one is mutable, one is not. And yet, both of these are valid ComponentLikes that can be used in many areas of Adventure:

Component.text().content("Text").color(NamedTextColor.GREEN).style(Style.style().decorate(TextDecoration.BOLD).decorate(TextDecoration.ITALIC).build());
Component.text("").content("Text").color(NamedTextColor.GREEN).decorate(TextDecoration.BOLD).style(Style.style(TextDecoration.BOLD).decoration(TextDecoration.ITALIC, TextDecoration.State.TRUE)); // slighly facetious with the starting method, but it proves a point

They look pretty much the same, and yet have completely different semantics. One is created from mutable builders, one is created from immutable objects. There is nothing to tell me which one I should use (I know it should be the first one) - part of why the lack of verbs is a problem. In the first method, I create three objects, the text component builder, the style builder and the style object (a fourth will be created upon use). The second method creates five - maybe more, I haven’t looked at the implementation. For more complicated structures, this is only going to get worse. There should be some way to differentiate between what happens in both of these methods, and ideally, the latter should not exist at all. But there isn’t. This is bad, and I am concerned it’s going to hit a lot of people, and potentially going to cause a lot of mistakes.

Some thought needs to go into what is the “right” way to do things, and these need to be promoted. I do not know what the right way to do things is as it stands.

I personally think the convenience methods should be considered for removal. If that’s not desired, I suggest prefixing the methods with with, in order to clarify that these methods are not the same as the builder variants.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
octylFractalcommented, Dec 8, 2020

I’d prefer keeping them, but with a with prefix or similar.

0reactions
asteicommented, Apr 16, 2021

the JIT is able to optimize away the allocation

This is highly specific to the use case (it relies on a lot going right in ways that are hard to predict). I doubt Components would be the source of GC pressure in an application except in the most extreme cases (and in that case, your application needs to take measures against that).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convenience Methods · Issue #60 · vuex-orm/vuex ... - GitHub
Many ORMs provide model methods like .find(), .create() or .save(). With Vuex-ORM we have to use rather long lines like this.
Read more >
Component (Java Platform SE 7 ) - Oracle Help Center
A component is an object having a graphical representation that can be displayed on the screen and that can interact with the user....
Read more >
ConfluentAdmin (clients 7.2.1-ce API)
This is a convenience method for removeBrokers(List, ... Note that a removal is considered cleared once a broker starts up with 0 partitions....
Read more >
1114716 - nsTArray could offer convenience methods for removing ...
Currently we have tons of code in the tree that looks like: someArray.RemoveElementAt(someArray.Length() - 1); We should add a method to make this...
Read more >
Better convenience methods for HTML
The helper Object.assign wasn't available in IE11. Remove Self. This one is pretty well-known. const el = document.getElementById ...
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