Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize Callable naming convention #285

Closed
vasinov opened this issue Sep 26, 2023 · 3 comments · Fixed by #1275
Closed

Normalize Callable naming convention #285

vasinov opened this issue Sep 26, 2023 · 3 comments · Fixed by #1275
Labels
type:enhancement Improvements to existing features

Comments

@vasinov
Copy link
Member

vasinov commented Sep 26, 2023

Multiple Griptape classes have Callable properties. We use the following naming convention for them:

  1. _func. For example SnowflakeSqlDriver.connection_func.
  2. _fn. For example, LocalVectorStoreDriver.relatedness_fn.
  3. Method-like. For example, BasePromptDriver.prompt_stack_to_string.

The team decided to normalize all callable properties to follow the _fn naming convention.

@vasinov vasinov added the type:enhancement Improvements to existing features label Sep 26, 2023
@collindutter
Copy link
Member

After revisiting the naming conventions, we decided against using the _fn suffix, as it felt unpythonic and inconsistent with naming conventions for other field types. For instance, we don't use _str for string fields, so there's no need to treat callables differently.

For callable fields and methods, the following guidelines should be used:

  1. Action Functions: Functions that perform an action should be named using a verb or verb phrase that clearly describes what the function does.

    • SnowflakeSqlDriver.connection_funcSnowflakeSqlDriver.create_connection
    • LocalVectorStoreDriver.relatedness_fnLocalVectorStoreDriver.calculate_relatedness
    • BasePromptDriver.prompt_stack_to_stringBasePromptDriver.convert_prompt_stack_to_string
  2. Boolean Functions: Functions that return a boolean should be prefixed with predicates such as is_, has_, or can_ to indicate a state or condition.

  3. Event Handlers: Functions that react to events should be prefixed with on_. These functions typically don’t return a value but are triggered in response to an event. If a function transforms an event or returns a value based on it, it should follow the first rule (e.g., process_event).

Keep in mind that these are guidelines, not strict rules, and can be adjusted based on context.

@collindutter
Copy link
Member

collindutter commented Oct 21, 2024

Some team members have noted that these naming conventions may be more appropriate for class methods than class fields. For example, create_connection could be misinterpreted as a boolean that indicates whether a connection should be created.

To avoid this confusion, we are standardizing class field naming to be noun-based. Below are some of the conventions I landed on in #1275.

*_factory: Used for functions that create a new instance of an object.
*_provider: Used for functions that supply a value. These functions may create new instances, as long as the value represents the same resource or value.
*_formatter: Formats an object or value, typically into a string or another representation.
*_callback: Used for functions that react to or handle a specific event or condition.
*_handler: Used for functions responsible for actively managing or processing an event or task.
*_calculator: Used for functions that compute or derive a specific value.
Again, these aren't hard-and-fast rules, but hopefully they provide a clear framework to guide future naming decisions.

@collindutter
Copy link
Member

And finally, after even more discussion we've settled on a variation of the initial proposal:

  • Action Functions: Functions that perform an action should be named using a verb or verb phrase that clearly describes what the function does.

    • SnowflakeSqlDriver.connection_funcSnowflakeSqlDriver.create_connection
    • LocalVectorStoreDriver.relatedness_fnLocalVectorStoreDriver.calculate_relatedness
    • BasePromptDriver.prompt_stack_to_stringBasePromptDriver.convert_prompt_stack_to_string
  • Boolean Functions: Functions that return a boolean should be prefixed with predicates such as is_, has_, or can_, indicating a state or condition.

  • Callbacks/Hooks: Functions that execute during specific events should be prefixed with on_. This naming convention avoids assumptions about the function's behavior, emphasizing that it reacts to an event.

This article comes closest to summarizing these ideas.

Addressing Previous Points:

  • Verbs sound like booleans: While it's true that verbs can resemble boolean names, our extensive use of type hints should minimize ambiguity. Additionally, we can improve our boolean function names over time.
  • What happened to nouns?: Attempting to force noun-based names for functions (e.g., "factory" or "provider") felt clunky and could be confusing for users unfamiliar with these terms. A consistent verb-based naming convention, with a few exceptions, is simpler and clearer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement Improvements to existing features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants