Skip to content

Latest commit

 

History

History
176 lines (138 loc) · 4.09 KB

File metadata and controls

176 lines (138 loc) · 4.09 KB

Clever Coding vs. Readable Code

The Problem

"Clever" code tries to pack too many operations into a single line. This makes the code:

  • Hard to read - You have to look multiple times to understand what's happening
  • Hard to debug - When there's an error, you don't know which part failed
  • Hard to maintain - Changes are risky because you're not sure what's affected

Bad Practice (bad.php)

return get_userdata( ( $p = get_post( $post_id ) ) ? $p->post_author : 0 )->display_name ?? 'Unknown';

Problems:

  • Variable assignment inside a condition
  • Ternary operator nested with function calls
  • Null-coalescing at the end for error handling
  • Impossible to debug - where exactly did the error occur?

Good Practice (good.php)

$post = get_post( $post_id );

if ( ! $post ) {
    return 'Unknown';
}

$author = get_userdata( $post->post_author );

if ( ! $author ) {
    return 'Unknown';
}

return $author->display_name;

Benefits:

  • Each operation on its own line
  • Explicit error checking after each step
  • Easy to set a breakpoint
  • Immediately understandable what's happening

More Examples

Example 2: Chained Operations

Bad:

return wp_mail(
    get_post_field( 'post_author', wp_update_post( array_merge( ['ID' => $post_id], $data ) ) ),
    'Update',
    'Your post was updated'
);

Problems:

  • Nested function calls make it impossible to debug
  • No error handling if wp_update_post fails
  • Can't inspect intermediate values

Good:

$update_data     = array_merge( [ 'ID' => $post_id ], $data );
$updated_post_id = wp_update_post( $update_data );

if ( is_wp_error( $updated_post_id ) ) {
    return false;
}

$author_email = get_post_field( 'post_author', $updated_post_id );
$email_sent   = wp_mail( $author_email, 'Update', 'Your post was updated' );

return $email_sent;

Example 3: Multiple Assignments in Conditions

Bad:

if ( ( $posts = get_posts( ['numberposts' => 10 ] ) ) 
    && ( $count = count( $posts ) ) 
    && ( $filtered = array_filter( $posts, fn( $p ) => $p->post_status === 'publish' ) ) ) {
    return array_map(
        fn( $p ) => ['id' => $p->ID, 'title' => get_the_title( $p->ID ), ... ],
        $filtered
    );
}

Problems:

  • Multiple assignments in condition
  • Arrow functions make debugging harder
  • No way to inspect intermediate values
  • Unclear what happens if any condition fails

Good:

$posts = get_posts( [ 'numberposts' => 10 ] );

if ( empty( $posts ) ) {
    return [];
}

$published_posts = array_filter( $posts, function ( $post ) {
    return $post->post_status === 'publish';
} );

$result = [];
foreach ( $published_posts as $post ) {
    $author      = get_userdata( $post->post_author );
    $author_name = $author ? $author->display_name : 'N/A';

    $result[] = [
        'id'     => $post->ID,
        'title'  => get_the_title( $post->ID ),
        'author' => $author_name,
    ];
}

return $result;

Example 4: Switch(true) (Anti-)Pattern

Bad:

switch ( true ) {
    case !$condition_1 && $condition_2:
        $value = 'this';
        break;
    case $condition_1 && !$condition_2:
        $value = 'that';
        break;
}

Problems:

  • switch(true) is a code smell - just use if/else
  • High cognitive load to understand
  • Assignments inside switch cases
  • More complex than needed
  • Only useful in very specific scenarios

Good:

if ( ! $condition_1 && $condition_2 ) {
    $value = 'this';
} elseif ( $condition_1 && ! $condition_2 ) {
    $value = 'that';
}

Benefits:

  • Simple, direct if/else statements
  • Early returns eliminate need for temporary variables
  • Clear control flow
  • Easier to add conditions or modify logic
  • Even shorter than the switch version

Key Takeaways

One operation per line
Explicit error handling
Meaningful variable names
Write code for humans, not compilers
Use simple if/else instead of switch(true)

❌ No nested ternary operators
❌ No assignments in conditions
❌ No overly long method chains without checks
❌ No switch(true) - just use if/else