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

Function rewrite 2 #394

Closed
jdevalk opened this issue Dec 7, 2023 · 0 comments
Closed

Function rewrite 2 #394

jdevalk opened this issue Dec 7, 2023 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Milestone

Comments

@jdevalk
Copy link
Collaborator

jdevalk commented Dec 7, 2023

Example of a rewrite

As iterated in the first rewrite issue, more attention should be paid to the details. For example, this extremely simple function:

/**
 * Sanitize the text position value before being saved to database
 *
 * @param array $position Position value.
 * @return array
 */
function edac_sanitize_simplified_summary_position( $position ) {
	if ( in_array( $position, array( 'before', 'after', 'none' ), true ) ) {
		return $position;
	}
}
  • Docblock error: $position is not an array, it's a string.
  • Docblock error: the function doesn't return an array, it returns a string.
  • Performance issue: in array() is slow.
  • Return error: if $position is not one of the 3 values, the function returns null instead of a default value - which we already know is after.

Should be rewritten like this:

/**
 * Sanitize the text position value before being saved to database.
 * If the value is not one of the 3 allowed values, it will be set to "after".
 *
 * @param string $position The position value to sanitize.
 *
 * @return string Returns the sanitized position value, 
 *                or "after" if the value is not one of the 3 allowed values.
 */
function edac_sanitize_simplified_summary_position( $position ) {
    return ( 'before' === $position || 'after' === $position || 'none' === $position )
        ? $position
        : 'after';
}

or even like this if we want it to be easier to read:

/**
 * Sanitize the text position value before being saved to database.
 * If the value is not one of the 3 allowed values, it will be set to "after".
 *
 * @param string $position The position value to sanitize.
 *
 * @return string Returns the sanitized position value, 
 *                or "after" if the value is not one of the 3 allowed values.
 */
function edac_sanitize_simplified_summary_position( $position ) {
    switch ( $position ) {
        case 'before':
        case 'after':
        case 'none':
            return $position;

        default:
            return 'after';
    }
}

It might be a bit longer, but it's also easier to maintain long-term. If we had PHPUnit tests, we could easily test that function. As it is now, it would throw errors - because it's wrong.

The same thing applies to edac_sanitize_simplified_summary_prompt and dozens of others.

The use of switch

We don't use switch () anywhere in the code right now. As seen above, using switch () instead of if ()/elseif ()/else can make the code easier to read and maintain in some cases - especially when there are many conditions.

@jdevalk jdevalk added documentation Improvements or additions to documentation enhancement New feature or request labels Dec 7, 2023
@SteveJonesDev SteveJonesDev self-assigned this Feb 2, 2024
@SteveJonesDev SteveJonesDev added this to the v1.8.0 milestone Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants