Why I Did What I Did – Coding for Maintainability

Here is what I spent all day working on today.  I replaced a large block of mixed HTML/PHP code that looked like this…
…repeated multiple times with variations, with this PHP code:

	$current_value      =& MyMirrors::$options->current_mode();
				
	$multi = is_multisite();
	$plus  = FALSE;
	$ultra = FALSE;
				
	$radios = array(DefaultsII::BLOG_CONTROL=>array((TRUE          ), esc_attr( 'Blog',                            'my-mirrors')),
				    DefaultsII::MULT__BLOG_1=>array(($multi        ), esc_attr( 'MultiSite via Blog 1',            'my-mirrors')),
				    DefaultsII::MULT_NETWORK=>array(($multi        ), esc_attr( 'MultiSite via Network Admin',     'my-mirrors')),
				    DefaultsII::BLOG__MANUAL=>array((        $plus ), esc_attr( 'Blog Remote (Manual)',            'my-mirrors')),
				    DefaultsII::BLOG____SYNC=>array((        $ultra), esc_attr( 'Blog Remote (Synchronized)',      'my-mirrors')),
				    DefaultsII::MULTI_MANUAL=>array(($multi&&$plus ), esc_attr( 'MultiSite Remote (Manual)',       'my-mirrors')),
				    DefaultsII::MULTI___SYNC=>array(($multi&&$ultra), esc_attr( 'MultiSite Remote (Synchronized)', 'my-mirrors')));

				 <?php if (MyMirrors::$options->allow_modes()) : 
				 
				 $radio_group     = new RadioGroupII('Initial_Mode', $current_value, $radios);
				 $html_for_radio_buttons =     $radio_button_group->html_of_input();

				 echo($html_for_radio_buttons);


Now, the original is the classic way of doing webpages.  Unfortunately, While this style works well for small, simple things, it breaks down for large complex projects like WordPress plugins.  The problem is that many levels of abstraction are all present at once.

This “everything in one view” style makes it easy to see “what’s on the page”; it obfuscates “what is the code doing”.  It is great for the presentation orientation, and problematic for the process action orientation.  

Now the entire “display” is replaced by one line:


echo($html_for_radio_buttons);

We had half of a page to display one small set of radio buttons, now we have one line.  “The radio buttons are here”

To do that, we created the HTML for the radio buttons using the class method html_of_input() which operated on the previously created object $radio_button_group$radio_button_group, in turn, by instantiating the class RadioGroupII, thus:


$radio_group = new RadioGroupII('Initial_Mode', $current_value, $radios);

All of the HTML details are hidden elsewhere; we don’t need to know all that to understand the function of the code.  Stripped of all of that detail, it is easy to see that the radio button group named 'Initial_Mode' is displaying and altering the variable $current_value, and that the setup for the group is in the array $radios.

Before explaining the how and why of the $radios array, which is the heart of the code here, a bit of explanation of the setup.

This is the My Mirrors plugin, so the class MyMirrors is the main class; nearly everything is either in that class or in a class which that main class uses.  $current_value is a shorthand reference to one of the main static variables in that class.  $multi, $plus, and $ultra are booleans that describe what is available in the software environment, what options are allowed to be selected.  Unavailable options will simply not be displayed.

The array $radios is an exercise in displaying what is relevant to the future maintenance programmer, and hiding what is irrelevant.  It is an array of two dimensions; the keys for the outer array are shown, while the keys for the inner arrays are not.  This is a deliberate choice.  In an earlier incarnation, the inner keys were also shown.  This made a very messy and busy display, and the essential details faded to obscurity.


	$radios = array(DefaultsII::BLOG_CONTROL=>array((TRUE          ), esc_attr( 'Blog',                            'my-mirrors')),
				    DefaultsII::MULT__BLOG_1=>array(($multi        ), esc_attr( 'MultiSite via Blog 1',            'my-mirrors')),
				    DefaultsII::MULT_NETWORK=>array(($multi        ), esc_attr( 'MultiSite via Network Admin',     'my-mirrors')),
				    DefaultsII::BLOG__MANUAL=>array((        $plus ), esc_attr( 'Blog Remote (Manual)',            'my-mirrors')),
				    DefaultsII::BLOG____SYNC=>array((        $ultra), esc_attr( 'Blog Remote (Synchronized)',      'my-mirrors')),
				    DefaultsII::MULTI_MANUAL=>array(($multi&&$plus ), esc_attr( 'MultiSite Remote (Manual)',       'my-mirrors')),
				    DefaultsII::MULTI___SYNC=>array(($multi&&$ultra), esc_attr( 'MultiSite Remote (Synchronized)', 'my-mirrors')));

In the form it is given here, we see the three essential elements to every entry:

  • What the option is. (MULT_NETWORK etc.)
  • Whether is is enabled. (MULT_NETWORK is available if $multi is TRUE)
  • What is the translatable label that the user will see (“MultiSite via Network Admin“)

(The final “my-mirrors” is an unavoidable WordPress requirement.)

The key for each row is important in understanding the code; the internal keys that distinguish the enabling switch from the label are not.

Note that the keys are given symbolically rather than by value. “Magic Numbers” and “Magic Strings” as keys are the constant nightmare of the maintenance programmer.  Here we have where the keys are defined (DefaultsII, where they are defined “once and only once” as intelligible strings), and a meaningful name for each.

However, the keys to the inner array items do exist, albeit not in the source array.  The constructor for the RadioGroupII class handles the translation from group order to named key values.

 


	CONST ALLOW = 'Allow';
	CONST LABEL = 'Label';
	CONST MAP   = array(self::ALLOW, self::LABEL);
	
	      foreach ($my_definition as $mode=>&$rad_def) {
	      	$def  =& $this->definition[$mode];
	      	$list =  $rad_def;
	      	$map  =  self::MAP;
		      foreach ($map as $place=>&$key) {
		      	$def[$key] = current($list);
		      	next($list);
		      }
		  }

Shortcode: Define

A Define Shortcode has no Mirror ID.  A Define Shortcode instantiates nothing in the Post rendering of itself; its only purpose is to update the database, creating a new Alias in an Alias List.

A Define Shortcode will do nothing unless under an Admin login, or the login has my_mirror_define privilege.

A Define Shortcode will do nothing if its Resource ID is already represented in the Alias List, unless the login has my_mirror_redefine privilege.

(my_mirror_redefine privilege, if it implemented, exists only for use in a special login for correcting errors.)

The Define Shortcode exists only to provide a convenient shorthand for the creation of aliases via the copy and paste of a URL while in the Edit Page, without needing to go to the Admin Pages.

[[my_define list=???;  id=???; url=???; ]] 

— or —

[[my_define list=???;  id=???; alias=???; ]] 

Mandatory:

  • list    – An Alias List
  • id    – The Resource ID. If the Resource ID does not exist, an error is generated.
  • Either of:
    • url  – The Nominal URL.  Will be resolved to a Resource Name in accordance with the Filex associated with this mirror.   If there is no entry for this Resource ID in this List, an entry will be created associating this Resource ID with the Resource Name using the Resource Name Filex for this Mirror.
    • alias – Resource Name.  If there is no entry for this Resource ID in this List, an entry will be created associating this Resource ID with the Resource Name using the Resource Name Filex for this Mirror.

 

Admin: Alias Lists

We only edit these list one entry at a time; they can easily become huge, with hundreds of entries. For bulk editing, we provide CSV file export and import.

Select an Alias List to View or Edit

Alias List (select)
SELECT

Selection Filter Regexes

Alias ID Filter (text)
Alias Value Filter  (text)
Default

Select an Alias to View or Edit

Alias ID  (select)
Alias Value  (display)
SELECT

View, Modify or Add an Alias

Alias ID  (display/text)
Alias Value  (display/test)
ADD
MODIFY

Export – Import CSV Alias Lists

Export all to file.

EXPORT CSV FILE

Import from CSV file.

IMPORT CSV REPLACE ALL

IMPORT CSV OVERWRITE or ADD

TEST CSV for UNIQUE IDs
IMPORT CSV ADD NEW

 

Admin: Composite Parameters

Composite Parameters appear in a Composite Shortcode definition with a prefixed “at-sign”, and in a Composite Shortcode instantiation without the “at-sign”.

There is one list of user defined Composite Parameters shared by all Composite Shortcodes defined.  However, not every Composite Parameter need be used by every Composite Shortcode.

In the calling convention, Composite Parameters share the namespace of the Post metadata (postmeta).  Therefore, Custom Parameter names must be unique with respect to all WordPress Post metadata names and with respect to all Post metadata names defined in any plugin currently in installed.  In the WordPress database, Composite Parameters share the namespace of the Blog metadata (options) in a “single Blog” or “Blog 1” installation, and with the MultiSite Network metadata (sitemeta) for a MultiSite Network installation.  Therefore, Custom Parameter names must also be unique with respect to all these WordPress  metadata names and with respect to all these metadata names defined in any plugin currently in installed.

Enter all of the user-defined parameters (without their “at sign”) as a comma-separated list.

List of Parameters (text)
SAVE

Composite Parameter Default Values

A default value must be specified as null for the absence of a parameter in the Composite Shortcode instantiation to have the unique effect of an absent parameter in the included shortcode.

Default values may be:

  • null
  • Integer
  • “Quoted String”
  • @Automatic_String
  • @@Custom_Field
  • @@Post_Meta_Data
  • @@Blog_Meta_Data
  • @@Site_Meta_Data
Parameter Name Parameter Default Value (button)
Parameter (display) Value (text)
SAVE
Parameter (display) Value (text)
SAVE
Parameter (display) Value (text)
SAVE
Parameter (display) Value (text)
SAVE
Parameter (display) Value (text)
SAVE
Parameter (display) Value (text)
SAVE
Parameter (display) Value (text)
SAVE

Admin: Composite Definition

Add New Composite Shortcode

New Shortcode Name (text)
SAVE

Edit Composite Shortcode Definition

Existing Shortcode Name (select)
SELECT

A Composite Shortcode is comprised of from one to nine Link, Embed, Auto and/or ProForma shortcodes.  These shortcodes are instantiated successively according to their definitions (below), with parameters derived from:

  • the calling parameters of the Composite Shortcode instantiation
  • Automatic Strings
  • user defined Composite Parameters
  • Post metadata

 

EDITING:

Shortcode Name (display)

 

Parameters are defined in the Composite Parameters page.

enable shortcodes
A Link, Embed, Auto or ProForma shortcode (text)
A Link, Embed, Auto or ProForma shortcode (text)
A Link, Embed, Auto or ProForma shortcode (text)
A Link, Embed, Auto or ProForma shortcode (text)
A Link, Embed, Auto or ProForma shortcode (text)
A Link, Embed, Auto or ProForma shortcode (text)
A Link, Embed, Auto or ProForma shortcode (text)
A Link, Embed, Auto or ProForma shortcode (text)
A Link, Embed, Auto or ProForma shortcode (text)
SAVE

Instantiation Parameters

The following predefined parameters will auto-generate text from the parameter list of the shortcode instantiation.

  • @id
  • @url
  • @alias
  • @list
  • @pri
  • @sec
  • @ter
  • @prefix
  • @link
  • @postfix
  • @proforma

Mirrors Parameter

With the expression mirrors=”comma separated integer listin the instantiation of the Composite Shortcode, the order of mirror instantiation may be reorganized, including skips.  If present. it must contain at least as many integers as needed for this Composite Shortcode definition.

The value of “@mirror” will be the successive values of this list if mirrors is in the instantiation, and the successive integers 1-9 if it is not.  

The value of “@alternate” will be the successive values of this list if mirrors is in the instantiation, and the successive integers 1-9 if it is not, except that if the mirror resolves to the same as mirror_0, this shortcode within the Composite Shortcode is not instantiated.

Thus a typical Composite Shortcode definition might set mirror=0 for the first Embed Shortcode, and mirror=@alternate for each succeeding Link Shortcode.

The successive values of these parameters increment with each instantiation of a shortcode for which they are a legal parameter, whether a @mirror or @alternate is specified or not.  The values do not increment for any shortcode which cannot take a @mirror parameter.

  • @mirror
  • @alternate

Automatic Strings

The following predefined parameters will auto-generate text from the context of the shortcode instantiation.

  • @resource_id
  • @resource_name
  • @mirror_name
  • @mirror_number
  • @virtual_name
  • @server_name

In addition, all of the Composite Parameters (defined in the Composite Parameters page) which will appear in the Composite Shortcode instantiation may be used.

Shortcode: Composite

A Composite Shortcode combines one or more shortcodes into one call.

[[my_composite  url=???;  composite=???;  {zero or more parameters }]] 

—  —or—  —

[[my_composite   id=???; composite=???; {zero or more parameters }]] 

Allowing a Composite Shortcode to be a single shortcode allows its use to extend a shortcode with custom data.

Mandatory:

  • composite – The Composite Definition name.
  • Either of –
    • id    – The Resource ID. If the Resource ID does not exist, an error is generated.
    • url  – The Nominal URL.  Will be resolved to a Resource ID in accordance with the Filex associated with this mirror.  If the Resource ID does not exist, it will be created. If there is no entry for this Resource ID at this Mirror, an entry will be created associating this Resource ID with the Resource Name using the Resource Name Filex for this Mirror.

Optional:

  • The parameters are defined in the Composite Definition and the Composite Parameters admin pages.  Just as the shortcode parameters, Composite Parameters  (if specified) appear in the Composite Shortcode instantiation in the form:   ParameterName=???    where ??? is either:
      • an integer
      • a “Quoted String”
      • an @Automatic_String
      • an item of @@Post_Meta_Data
      • a @@Custom_Field
      • an item of @@Blog_Meta_Data
      • an item of @@Site_Meta_Data

(The database is queried in the order shown; the first match is used.)

Any Composite Parameter defined used in the Composite Shortcode definition and not present in the Composite Shortcode instantiation will be represented by the predefined default value.

 

Shortcode: Auto

An Auto Shortcode acts like an Embed Shortcode if its Mirror ID resolves to the same Server Site as [mirror‑0]; acts like a ProForma Shortcode otherwise.

[[my_auto mirror=???; url=???; {alias=???;} {list=???; } {proforma=???; }]] 

—  —or—  —

[[my_auto mirror=???;  id=???; {alias=???;} {list=???; }{proforma=???; }]] 

 

Mandatory:

  • mirror – The Mirror number or Mirror name.  For example, either “3” or “mirror‑3”.
  • Either of –
    • id    – The Resource ID. If the Resource ID does not exist, an error is generated.
    • url  – The Nominal URL.  Will be resolved to a Resource ID in accordance with the Filex associated with this mirror.  If the Resource ID does not exist, it will be created. If there is no entry for this Resource ID at this Mirror, an entry will be created associating this Resource ID with the Resource Name using the Resource Name Filex for this Mirror.
  • proforma    – The ID of an existing ProForma specification.

Optional:

  • Either or neither of –
    • alias – Resource Name.  If present, this overrides any Resource Name association elsewhere, such as in an Alias List associated with the Mirror.
    • list    – An Alias List.  If present, this overrides any Alias List association elsewhere, such as an Alias List associated with the Mirror.
  • Any or all or none of –
    • pri – A Primary Branch name or a slash‑terminated Primary Branch string.  If present, this overrides any Primary Branch association elsewhere, such as a Primary Branch associated with the Mirror.
    • sec – A Secondary Branch name or a slash‑terminated Secondary Branch string.  If present, this overrides any Secondary Branch association elsewhere, such as a Secondary Branch associated with the Mirror.
    • ter – A Tertiary Branch name or a slash‑terminated Tertiary Branch string.  If present, this overrides any Tertiary Branch association elsewhere, such as a Tertiary Branch associated with the Mirror.

 

Shortcode: Mutable

A Mutable Shortcode acts like an Embed Shortcode if its Mirror ID resolves to the same Server Site as [mirror-0]; acts like a Link Shortcode otherwise.

[[my_mutable mirror=???; url=???; {alias=???;} {list=???; } {pri=???; } {sec=???; } {ter=???; }]] Some user supplied text [[/my_link]]

—  —or—  —

[[my_mutable mirror=???;  id=???; {alias=???;} {list=???; } {pri=???; } {sec=???; } {ter=???; }]] Some user supplied text [[/my_link]]

—  —or—  —

[[my_mutable mirror=???; url=???; {alias=???;} {list=???; } {pri=???; } {sec=???; } {ter=???; } {prefix=”some user text”; } {link=”more user text”; } {postfix=”final user text”; }]] 

—  —or—  —

[[my_mutable mirror=???;  id=???; {alias=???;} {list=???; } {pri=???; } {sec=???; } {ter=???; } {prefix=”some user text”; } {link=”more user text”; } {postfix=”final user text”; }]] 

Mandatory:

  • mirror – The Mirror number or Mirror name.  For example, either “3” or “mirror‑3”.
  • Either of –
    • id    – The Resource ID. If the Resource ID does not exist, an error is generated.
    • url  – The Nominal URL.  Will be resolved to a Resource ID in accordance with the Filex associated with this mirror.  If the Resource ID does not exist, it will be created. If there is no entry for this Resource ID at this Mirror, an entry will be created associating this Resource ID with the Resource Name using the Resource Name Filex for this Mirror.
  • Either of –
    • link    – The text which will appear in the link.  No following text and closing shortcode allowed.
    • After the shortcode, the text which will appear in the link, followed by a closing shortcode. The prefix, link, and postfix options are not allowed.

Optional:

  • Either or neither of –
    • alias – Resource Name.  If present, this overrides any Resource Name association elsewhere, such as in an Alias List associated with the Mirror.
    • list    – An Alias List.  If present, this overrides any Alias List association elsewhere, such as an Alias List associated with the Mirror.
  • Any or all or none of –
    • pri – A Primary Branch name or a slash-terminated Primary Branch string.  If present, this overrides any Primary Branch association elsewhere, such as a Primary Branch associated with the Mirror.
    • sec – A Secondary Branch name or a slash-terminated Secondary Branch string.  If present, this overrides any Secondary Branch association elsewhere, such as a Secondary Branch associated with the Mirror.
    • ter – A Tertiary Branch name or a slash-terminated Tertiary Branch string.  If present, this overrides any Tertiary Branch association elsewhere, such as a Tertiary Branch associated with the Mirror.
    • Only if link is present,  either or neither of –
      • prefix   – Text which will appear before the link, in the same line.
      • postfix  – Text which will appear after the link, in the same line.

 

Admin: View Proforma

ProForma Name

ProForma Friendly Name (display)

ProForma Prefix Text

ProForma Prefix Text (display)

ProForma Link Text

ProForma Link Text (display)

ProForma Postfix Text

ProForma Postfix Text (display)
Edit

 

Automatic Strings

The following strings will auto-generate text.

  • @resource_id
  • @resource_name
  • @mirror_name
  • @mirror_number
  • @virtual_name
  • @server_name

Admin: ProForma Definitions

enable
ProForma name view edit delete
Name of ProForma (display)
View
Edit
Delete
Name of ProForma (display)
View
Edit
Delete
Name of ProForma (display)
View
Edit
Delete
Black Bayou Software of Louisiana