Warning: Declaration of BP_Walker_Nav_Menu::walk($elements, $max_depth) should be compatible with Walker::walk($elements, $max_depth, ...$args) in /home/tedccoh5/public_html/wp-content/plugins/buddypress/bp-core/bp-core-classes.php on line 2264

Warning: ksort() expects parameter 1 to be array, object given in /home/tedccoh5/public_html/wp-content/plugins/buddypress/bp-core/bp-core-template-loader.php on line 192
Tedbits | Thoughts from the mind of a madman

Best practice passing options to methods

Here’s a simple issue that can be solved quite simply. A complaint a developer made once of my code was a good one: try to avoid passing booleans as method parameters because they don’t convey meaning. Example:

 public void applyMagic(String spell, boolean wideArea, boolean greenSmoke, boolean loudNoise);

The call would look like this:

applyMagic(“3wishes”, true, false, true);

The problem is, if you’re just reading the method call and unfamiliar with the method declaration (and possibly, even if you are), what the heck does “true, false, true” mean? It’s ambiguous and does not convey intent. The proper way to refactor this method declaration is to force the passing of symbolic values that do convey meaning.

One of the simplest and nifty ways to do this is to pass a variable length array of enum options. For example:

public enum Options {
WIDE_AREA, GREEN_SMOKE, LOUD_NOISE, etc.
}

public void applyMagic(String spell, Option… opts {
List<Option> options = Arrays.asList(opts);
boolean optWideArea = options.contains(WIDE_AREA);
boolean optGreenSmoke = options.contains(GREEN_SMOKE);
boolean opt_LOUD_NOISE = options.contains(LOUD_NOISE);
}

where the new method call then becomes:

applyMatgic(“3wishes”, WIDE_AREA, GREEN_SMOKE, LOUD_NOISE);

Much more informative and clear, right?

Thoughts from the mind of a madman


Fatal error: Uncaught Error: [] operator not supported for strings in /home/tedccoh5/public_html/wp-content/plugins/buddypress/bp-xprofile/bp-xprofile-loader.php:362 Stack trace: #0 /home/tedccoh5/public_html/wp-includes/class-wp-hook.php(287): BP_XProfile_Component->setup_settings_admin_nav('') #1 /home/tedccoh5/public_html/wp-includes/plugin.php(206): WP_Hook->apply_filters('', Array) #2 /home/tedccoh5/public_html/wp-content/plugins/buddypress/bp-core/bp-core-component.php(448): apply_filters('bp_settings_adm...', '') #3 /home/tedccoh5/public_html/wp-content/plugins/buddypress/bp-settings/bp-settings-loader.php(194): BP_Component->setup_admin_bar('') #4 /home/tedccoh5/public_html/wp-includes/class-wp-hook.php(287): BP_Settings_Component->setup_admin_bar('') #5 /home/tedccoh5/public_html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array) #6 /home/tedccoh5/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #7 /home/tedccoh5/public_html/wp-content/plugins/buddypress/bp-core/bp-core-depe in /home/tedccoh5/public_html/wp-content/plugins/buddypress/bp-xprofile/bp-xprofile-loader.php on line 362