-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
Description
Currently, the lorem.Provider() exposes the following main methods:
word()words()sentence()sentences()paragraph()paragraphs()text()texts()
Most plural methods (e.g., sentences()) internally call their singular counterpart (sentence()) N times. In turn, the singular methods often delegate to the plural of the next hierarchical level (e.g., sentence() → words()).
The exception is word(), which directly returns words(nb=1)[0].
Additionally, the helper method get_word_list() is invoked twice in the same execution path, but the second invocation is always a no-op (simply returning the received argument):
-
sentence()→get_word_list()- →
words()→get_word_list()
- →
-
word()→get_word_list()- →
words()→get_word_list()
- →
Proposal
I suggest a simplification and standardization of this hierarchy:
- Every singular method should return its plural counterpart with
nb=1, as already implemented inword().
- (Simplifyed) Example:
def sentence():
return sentences(nb=1)[0]
-
get_random_list()should only be called insidewords(). -
New parameters to
sentences()andparagraphs()to preserve current behavior and backward compatibility:sentences()should acceptnb_wordsandvariable_nb_words.paragraphs()should acceptnb_sentencesandvariable_nb_sentences.- The addition of these parameters increases the flexibility of plural methods, allowing calls like
sentences()not to be limited to default word counts and flags.
This would result in a cleaner, more predictable call hierarchy.
Motivation
- Consistency: Aligns all singular methods with the same design pattern.
- Readability: Simplifies the mental model of how plural and singular methods interact.
- Maintainability: Reduces redundant calls to helper functions.
- Flexibility: Expands the usability of
sentences()andparagraphs()methods by exposing parameters directly. - Performance: While initial prototypes show no significant performance gain, the reduced number of function calls may still reduce potential overhead.
Backward Compatibility
- Existing behavior is preserved by ensuring plural methods accept the new parameters with the same default values as they implicitly assume today.
- No breaking changes are expected for current user code.