Improved coding

The following code works (whew…after a long day’s trying complex IF THEN ELSE conditions). However, I assume there are better ways of coding the logic. I am concerned that by simplifying each condition through many repeated $list widgets, when the numbers of tiddlers increase in the wiki, performance will decrease, especially as this procedure is in a viewtemplate for many tiddler types. It is easily understandable and that is a plus for future tech support.

I’d be interested in seeing others’ coding suggestions/improvements.

NB. the display of labels in each $list condition and the List-Links macro output are purely for debugging and will be removed

\procedure TLSbacklinks()

<<list-links filter:"[all[current]backlinks[]]" >>
 
Linked from
<br>

<$list filter="[all[current]backlinks[]]" >
     <$list filter="[<currentTiddler>prefix:title[MGA]caption[History]]" >
          <$link to=<<currentTiddler>> >
           History: <$text text={{!!caption}} />
           </$link>
           <br>
      </$list>
     <$list filter="[<currentTiddler>prefix:title[MGA]caption[Local History]]" >
          <$link to=<<currentTiddler>> >
           LocalHistory: <$text text={{!!caption}} />
           </$link>
           <br>
      </$list>
     <$list filter="[<currentTiddler>prefix:title[MGA]caption[Gallery]]" >
          <$link to=<<currentTiddler>> >
           Gallery: <$text text={{!!caption}} />
           </$link>
           <br>
      </$list>
     <$list filter="[<currentTiddler>prefix:title[MGA]caption[Technical]]" >
          <$link to=<<currentTiddler>> >
           echnical: <$text text={{!!caption}} />
           </$link>
           <br>
      </$list>
     <$list filter="[<currentTiddler>prefix:title[MGA]caption[Map]]" >
          <$link to=<<currentTiddler>> >
           Map: <$text text={{!!caption}} />
           </$link>
           <br>
      </$list>
     <$list filter="[<currentTiddler>tag[All Guns]]" >
          <$link to=<<currentTiddler>> >
           All Guns: <$text text={{!!caption}} />
           </$link>
           <br>
      </$list>
    <$list filter="[<currentTiddler>tag[Gun Type]]" >
          <$link to=<<currentTiddler>> >
           Gun Type: <$text text={{!!title}} />
           </$link>
           <br>
      </$list>
    <$list filter="[<currentTiddler>tag[Fort]]" >
          <$link to=<<currentTiddler>> >
           Fort: <$text text={{!!title}} />
           </$link>
           <br>
      </$list>
     <$list filter="[<currentTiddler>tag[Ship]]" >
          <$link to=<<currentTiddler>> >
           Ship: <$text text={{!!title}} />
           </$link>
           <br>
      </$list>
    <$list filter="[<currentTiddler>tag[Manufacturer]]" >
          <$link to=<<currentTiddler>> >
           Manufacturer: <$text text={{!!title}} />
           </$link>
           <br>
      </$list>
    <$list filter="[<currentTiddler>caption[Ammunition]]" >
         <$link to=<<currentTiddler>> >
           Ammunition: <$text text={{!!title}} />
           </$link>
           <br>
      </$list>
    <$list filter="[<currentTiddler>caption[Carriages]]" >
         <$link to=<<currentTiddler>> >
           Carriages: <$text text={{!!title}} />
           </$link>
           <br>
      </$list>
</$list>
\end

bobj

So to be clear you are using the caption field as a defacto tiddler type. Perhaps it is best to use another field and leave the capotion free to be unique if required?

I can write it using the %if as a more performant structure if you want?

Ok I see the caption only uses the prefix to divide tiddlers.

<% if [all[current]backlinks[]] %>
   <% if [<currentTiddler>prefix:title[MGA]caption[History]] %>
           <$link to=<<currentTiddler>> >
           History: <$text text={{!!caption}} />
           </$link>
           <br>
    <%else%>
    <% if [<currentTiddler>prefix:title[MGA]caption[Local History]] %> 
          <$link to=<<currentTiddler>> >
           LocalHistory: <$text text={{!!caption}} />
           </$link>
           <br>
    <%else%>
    <% if [<currentTiddler>prefix:title[MGA]caption[Gallery]] %> 
          <$link to=<<currentTiddler>> >
           Gallery: <$text text={{!!caption}} />
           </$link>
           <br>
    <%endif%>
    <%endif%>
    <%endif%>
<%endif%>

I just did three to illustrate, untested. this is a “case structure” which only asks as many questions as it must, but all these questions could be asked inside a filter if you use the 2nd approach below.

However further optimisation could;

  1. make a macro for the content of each
  2. or a list widget could itterate “History [[local History]] Gallery etc…” and everyting displayed through a macro.

Opps the first If needs to be a list :frowning:

\function .caption-is(cap) [<currentTiddler>prefix:title[MGA]caption<cap>]

<$list filter="[all[current]backlinks[]]" >
   <% if [.caption-is[History]] %>
           <$link>
           History: <$text text={{!!caption}} />
           </$link>
           <br>
    <% elseif [.caption-is[Local History]] %> 
          <$link>
           LocalHistory: <$text text={{!!caption}} />
           </$link>
           <br>
    <% elseif ... %> 
    <% elseif ... %> 
    <% elseif ... %> 
    <% endif %>
</$list %>
\function .caption-is(cap) [<currentTiddler>prefix:title[MGA]caption<cap>]
\procedure link(cap) <$link><<cap>>:  <$text text={{!!caption}} /> </$link><br>

<$list filter="[all[current]backlinks[]]" >
   <% if [.caption-is[History]] %>
     <<link History>>
    <% elseif [.caption-is[Local History]] %> 
     <<link LocalHistory>>
    <% elseif ... %> 
    <% elseif ... %> 
    <% elseif ... %> 
    <% endif %>
</$list %>

Thanks guys. I was playing with nested if then else structures but found they were very unstable. Unless the operator started in char 1 of a line, it might not be recognised and in the end I got so confused with the logic, I gave them away and started with a clean sheet of paper to write down the login. That’s why I ended up with multiple $list widgets and recognised there might be a more consise way of stating things.

Bobj

I think your <$list is perfectly fine for the beginning. Especially if you are still developing it. Once the stuff has stabilised it may be possible to create a procedure that does create the lists dynamically, to have less duplicated code.

The performance will be very similar for “linear duplicated code” or “dynamic” code. Only dynamic code will be more fragile if you need new parameters or special handling for exceptions.

Your list filters should not be that expensive, since most of them start with one tiddler <currentTiddler>. The only expensive one is the backlinks[] filter, which is only used once.

I think you can simplify a fair bit with a few reusable nested procedures:

\procedure TLSbacklinks()

  \procedure cap-cap(type)
    <$list filter="[<currentTiddler>prefix:title[MGA]caption<type>]" >
      <$link to=<<currentTiddler>> >
        <<type>>: <$text text={{!!caption}} />
      </$link>
      <br>
    </$list>
  \end cap-cap

  \procedure tag-cap(type)
    <$list filter="[<currentTiddler>tag<type>]" >
      <$link to=<<currentTiddler>> >
        <<type>>: <$text text={{!!caption}} />
      </$link>
      <br>
    </$list>
  \end tag-cap

  \procedure tag-title(type)
    <$list filter="[<currentTiddler>tag<type>]" >
      <$link to=<<currentTiddler>> >
        <<type>>: <$text text={{!!title}} />
      </$link>
      <br>
    </$list>
  \end tag-title

  \procedure cap-title(type)
    <$list filter="[<currentTiddler>caption[Ammunition]]" >
      <$link to=<<currentTiddler>> >
         <<type>>: <$text text={{!!title}} />
      </$link>
      <br>
    </$list>
  \end cap-title

Linked from<br>

  <$list filter="[<currentTiddler>backlinks[]]" >
     <<cap-cap "History">>
     <<cap-cap "Local History">>
     <<cap-cap "Gallery">>
     <<cap-cap "Technical">>
     <<cap-cap "Map">>
     <<tag-cap "All Guns">>
     <<tag-title "Gun Type">>
     <<tag-title "Fort">>
     <<tag-title "Ship">>
     <<tag-title "Manufacturer">>
     <<cap-title "Ammunition">>
     <<cap-title "Carriages">>
  </$list>
\end

There might be some way to combine these nested procedures too. But I don’t understand your structure well enough to know for sure.

1 Like

Thanks again everybody, your thoughts are revealing (in a good way :slight_smile: )

I have, for now, adopted a mixture of @TW_Tones and @CodaCoder suggestion and implemented a ‘case’ structure. Following @pmario’s suggestion about keeping it simple when creating and review once working, I will keep the multi-filter conditions for each tiddler type I am looking for and review once tiddler types have been settled.

bobj