Skip to content

Add extension methods for all methods#5

Merged
suragch merged 6 commits into
suragch:masterfrom
plokmij:master
May 18, 2024
Merged

Add extension methods for all methods#5
suragch merged 6 commits into
suragch:masterfrom
plokmij:master

Conversation

@plokmij

@plokmij plokmij commented Oct 29, 2023

Copy link
Copy Markdown

added extensions methods all of validators and most of sanitizers

@suragch

suragch commented Oct 31, 2023

Copy link
Copy Markdown
Owner

Thank you for your pull request. Extension methods are a good idea. Thanks for adding tests too.

I'd recommend a minor change on the API for some of the methods, especially the boolean isFoo methods. Let's use a getter instead of a regular method. Thus, myString.isEmail() would become myString.isEmail. This is more consistent with the current boolean getters on String (for example, myString.isEmpty). Other methods like '123'.toDouble() can retain the parentheses.

Here is some advice from ChatGPT about when to use getters or not:

In Dart and Flutter, the choice between using a getter method (e.g., foo.isSomething) and a regular method (e.g., foo.isSomething()) often depends on the nature of the operation and the convention within the community. Here are some guidelines:

Use a getter when:

  1. The operation is side-effect-free. It shouldn't modify any state.
  2. The operation is relatively quick to perform. It shouldn't involve complex computations, I/O operations, or network requests.
  3. You're retrieving a property that logically belongs to the object, or you want to compute something on-the-fly based on the object's existing state.

Use a method when:

  1. The operation has side effects or modifies the object's state.
  2. The operation is computationally expensive, involves I/O, or makes network requests.
  3. You need to pass additional arguments to compute the value or perform the operation.
  4. You want to make it explicit that the operation performs something more than just fetching a property.

Examples

  1. Getter: list.isEmpty checks whether a list is empty. This is side-effect-free and quick, and it's a property of the list.
  2. Method: list.removeLast() removes the last item from the list. This has a side effect and thus is a method.

In designing your API, if isSomething represents a state or property of foo that can be determined quickly and without side effects, a getter (foo.isSomething) would be appropriate. If it's an operation that's more complex, has side effects, or requires additional arguments, then a method (foo.isSomething()) would be more fitting.

For idiomatic Dart code, following these guidelines is generally a good practice.

Also, would you mind updating the README?

@suragch
suragch merged commit 3690545 into suragch:master May 18, 2024
@suragch

suragch commented May 18, 2024

Copy link
Copy Markdown
Owner

LGTM, Thanks!

@suragch

suragch commented May 18, 2024

Copy link
Copy Markdown
Owner

Published as 1.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants