Defensive programming

From a general point of view, defensive programming is generally seen as a good practice to achieve software reliability.

But, for me, it has always seemed difficult or tedious to acieve with tiddlywiki programming (I mean, using tiddlywiki or programming filters or widget in javascript and maybe other things). Because we have no easy way to take care of this.

And this is due to having limited ability to decide what to do next. Having to resort to filter to decide what the next point will be and use macro is a lot of boilerplate and makes for a reduced readability when things get complex. And defensive programming add many a test while checking for input sanity. We would gain a lot if a new mechanism were invented to take care of that.

Or maybe I’m just wrong and don’ts know better.

I created this topic because someone, a competent someone, once declared to our Great Leader that he had invented a monad with Tiddlywiki. And Maybe types are an example of monads and could help program defensively. So maybe we could be not so far away from a better place in that respect.

Let’s look at a real life example of my cde base. I have to get a list of colums name from a tiddler and succequently doing some stuff with it. They must be in a field called “columns” and each be only made of lowercase letters with maybe additional digits. No space! I would like to suspend my treatment if that would be the case, but I don’t want to uglify my code. Te code below is but a simple part in building a table listing raw data. How could we imagine being defensive here?

<tr>
<$list variable=colname filter="[list[columns]]">
  <th><<colname>></th>
</$list>
</tr>
1 Like

It would help if I knew your definition of “ugly code”.

Where I can, I define sensible defaults (contained in variables) which are populated ahead of the tree of widgets that need values that might 1, go wrong, 2, be missing, etc. When those conditions arise, the code switches to the variables (perhaps with a warning printed somewhere). Usually this involves using filters with then/else ops.

@CodaCoder This seems interesting. Could you upload an example?

As for what I call ugly, I have no definitive advice. It’s something that is not beautiful, that seems cumbersome andwhere the aim of the author is difficult to grasp. Boilerplate code is ugly par excellence!

yep. And I’m the author with a terrible memory :wink:

Extricating an example would take me too long. Here’s some pseudocode

<$let thing="default value" other-thing="other default stuff">

<<start-of-complex-tree content-in-multiple-tiddlers>>

</$let>

Elsewhere, deep in the complex-tree…

<$let 
  var={{{ [<some-filter>else<thing>] }}}
  other-var={{{ [<whatever>then<something>else<other-thing> }}}
>
<!-- do-fabulous-things -->
</$let>

Sometimes, if the start-of-complex-tree is in a hosting tiddler solely devoted to being that host, I’ll use fields instead:

EDIT: just to add, I think of these as fallback constants, used where missing values (for whatever reason, bad coding, etc) the entire wiki could crash and burn.

@CodaCoder Thank you. This is interesting. In effect, this ressemble having a pre-conditions checking and setup first. I kinda do likewise, but perhaps less well. I should look at that.

Have you a kind of post-conditions checking at the end of your treatments?

A colleague of mine, in a java context and for complex stuff, used to break a fubar function just like that (pseudo code):

function fubar(v1, v2) {
  var results = null;
   function fubar_pre() { ... }
   function fubar_act() { ... ; results = stuff_done; }
   function fubar_post() { ... }

   fubar_pre() &&  fubar_act() &&  fubar_post();
  return results;
}

Of course, he was using exception in case pre or post conditions found any problems beyond fixing.

Yep. That’s the point, cya up front :slight_smile:

I think of it as akin to a ternary operator in JavaScript or elvis (null-coalescing) operator in Groovy (java).

 let x = value? value : "42";

Hi all!

“less is more”… a simpler syntax idea for tiddlywiki

1. idea

1.1 link idea: https://html-to-pug.com/

2. Would it be possible to have a tiddlywiki-pug?

Yes as a plugin.

TW has an HTML parser with some sanitation for security reasons. It seems pug doesn’t sanitize.

The pug-plugin would introduces 3 dependencies to import 1 library. TiddlyWiki is a single-file app. It needs to work without a server, so everything needs to be in 1 HTML file.

As you wrote.

So why make the underlaying software more complicated to add a syntax that introduces one more thing to learn for our end-users.

It’s already hard enough to master the existing wikitext syntax.

1 Like