The Strengths and Weaknesses of Artificial Intelligence in Programming: An Analysis Through Practical Examples

The Strengths and Weaknesses of Artificial Intelligence in Programming: An Analysis Through Practical Examples

By Vincenzo Di Franco 文 (Culture), 森 (Nature), 佐 (Support), 迪 (Guidance), 弗 (Uniqueness), 朗 (Brightness), 科 (Competence)

December 4, 2024

Artificial Intelligence (AI) is transforming the programming world by providing tools that accelerate development and simplify problem-solving. However, its use must be managed carefully—"cum grano salis"—to avoid potential inefficiencies and high computational costs. This article explores the strengths and weaknesses of AI in programming through practical examples, including a previous discussion about creating PHP functions for WordPress.


Strengths of AI in Programming

1. Efficiency in Code Writing

AI can quickly generate functional code based on specifications provided by the developer, significantly reducing the time needed to develop complex features.

Example: In a discussion, we tackled the challenge of adding elements to an Advanced Custom Fields (ACF) field using a filter. AI promptly provided this solution:

add_filter('acf/load_field/name=your_field', 'add_multidimensional_option');
function add_multidimensional_option($field) {
    // Ensure structure exists
    if (isset($field['choices']['main_category']['subcategory']) && is_array($field['choices']['main_category']['subcategory'])) {
        $field['choices']['main_category']['subcategory']['new_option'] = 'New Option';
    } else {
        // Create structure if necessary
        $field['choices']['main_category']['subcategory'] = array(
            'new_option' => 'New Option',
        );
    }
    return $field;
}
        

This code adds a new option to a multidimensional array, saving development time.


2. Support in Problem-Solving

AI can assist in solving specific problems, such as accessing data via custom queries, by offering tailored solutions.

Example: When retrieving data from the database without using get_option, AI suggested leveraging $wpdb:

add_filter('acf/load_field/name=your_field', 'modify_choices_with_wpdb');
function modify_choices_with_wpdb($field) {
    global $wpdb;
    $option_name = 'listingpro_options';
    $results = $wpdb->get_var($wpdb->prepare("
        SELECT option_value FROM {$wpdb->options} WHERE option_name = %s
    ", $option_name));
    if ($results) {
        $listingpro_options = maybe_unserialize($results);
        if (isset($listingpro_options['target_key'])) {
            if (!is_array($field['choices'])) {
                $field['choices'] = array();
            }
            foreach ($listingpro_options['target_key'] as $key => $value) {
                $field['choices'][$key] = $value;
            }
        }
    }
    return $field;
}
        

This code retrieves an option directly from the database and uses it to modify an ACF field.


Weaknesses of AI in Programming

1. Potential for Hidden Inefficiencies

While AI can generate functional code, it may not optimize for performance, leading to high computational costs if not carefully reviewed.

Example: The initial version of a function to retrieve data overlooked caching, resulting in repeated database queries:

function retrieve_listingpro_options() {
    global $wpdb;
    $option_name = 'listingpro_options';
    $results = $wpdb->get_var($wpdb->prepare("
        SELECT option_value FROM {$wpdb->options} WHERE option_name = %s
    ", $option_name));
    if ($results) {
        return maybe_unserialize($results);
    }
    return false;
}
        

Without caching, this approach can cause excessive database access, slowing down the application.


2. Lack of Performance Optimization

AI may omit strategies like using transients for caching, leaving programmers responsible for further optimization.

Example: To improve performance, we modified the function to use transients:

function retrieve_listingpro_options() {
    global $wpdb;
    $option_name = 'listingpro_options';
    $transient_name = 'listingpro_options_transient';
    // Attempt to retrieve value from transient
    $listingpro_options = get_transient($transient_name);

    if ($listingpro_options !== false) {
        return $listingpro_options;
    }

    // Transient not found or expired, execute query
    $results = $wpdb->get_var($wpdb->prepare("
        SELECT option_value FROM {$wpdb->options} WHERE option_name = %s
    ", $option_name));

    if (!$results) {
        $listingpro_options = array();
        set_transient($transient_name, $listingpro_options, HOUR_IN_SECONDS);
        return $listingpro_options;
    }

    $listingpro_options = maybe_unserialize($results);
    set_transient($transient_name, $listingpro_options, 12 * HOUR_IN_SECONDS);
    return $listingpro_options;
}
        

This reduces the number of database queries, improving efficiency.


The Importance of Context and Critical Analysis

Developers must critically analyze AI-generated code, adapting it to the specific context and optimizing it for performance.

Comparison of Function Versions:

Initial Version (Without Optimization):

function modify_choices_with_data($field) {
    $listingpro_options = retrieve_listingpro_options();
    if ($listingpro_options && isset($listingpro_options['target_key'])) {
        if (!is_array($field['choices'])) {
            $field['choices'] = array();
        }
        foreach ($listingpro_options['target_key'] as $key => $value) {
            $field['choices'][$key] = $value;
        }
        asort($field['choices'], SORT_NATURAL | SORT_FLAG_CASE);
    }
    return $field;
}
        

Optimized Version (With Early Return and Transient):

function modify_choices_with_data($field) {
    $choices = retrieve_listingpro_options();

    if (empty($choices)) {
        return $field;
    }

    if (!is_array($field['choices'])) {
        $field['choices'] = array();
    }

    $field['choices'] = array_merge($field['choices'], $choices);
    return $field;
}
        

Advantages:

  • Reduced computational cost with early returns.
  • Use of caching via transients to minimize database queries.


Conclusion

"AI accelerates software development, but it’s the programmer's expertise that optimizes the code, ensuring performant and maintainable applications over time."


AI provides powerful tools for speeding up software development, but developers must remain active participants in the process. By critically analyzing and optimizing AI-generated solutions, developers can ensure their applications perform well and are maintainable in the long term. Leveraging AI "cum grano salis" allows us to harness its benefits while preserving the value of human experience and expertise in crafting high-quality code.

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics