string_validator icon indicating copy to clipboard operation
string_validator copied to clipboard

Add extension methods for all methods

Open plokmij opened this issue 2 years ago • 1 comments

added extensions methods all of validators and most of sanitizers

plokmij avatar Oct 29 '23 08:10 plokmij

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 avatar Oct 31 '23 03:10 suragch

LGTM, Thanks!

suragch avatar May 18 '24 12:05 suragch

Published as 1.1.0

suragch avatar May 18 '24 12:05 suragch