With <these things> Do <those actions>

I originally started this blog to discuss code, so I guess it’s time for a comeback of that practice!

While most of us are still struggling with coming to terms with the inherent design failure of the following idea:

Conventional Table of Contents

[Source: Manners for the Millions, 1932]

Namely that the word “Chapter” is repeated 24 times.

I wish to apply the same DRY(Don’t Repeat Yourself) convention to code, or rather extend the one we have today.

Programming abstractions exist to allow us to tackle software problems from different vantage points. Thus a large software system is often more humanly comprehendible if it is written in a higher-, rather than a lower-level language. This is the point of such abstractions, to hide boring(exciting) and complex code behind a compiler, and allow us to code using whichever conventions we find best, given they fit the constraints of our compiler.

Higher-level languages EXIST to overshadow the stupidity of a computer and allow the human being to read, analyze and basically code FASTER. While beauty of code is an art, we are far from having the tools to write intuitive code, in fact, certain (maybe needed) constrains limit us from doing so.

Basically, I’m saying that a media and communications nerd needs to look at the syntax of our code, and tell us computer geeks where we got it wrong. Source-code syntax is after-all meant for human comprehension and construction, not merely computer compilation.

We’ve come a long way from Assembler to Java, but allow me to ask the exact same question as above about this exemplar peace of code:

lblData1 = new JLabel("0");
lblData2 = new JLabel("0");
lblData3 = new JLabel("0");
lblData4 = new JLabel("0");

Again, there’s something inherently wrong with this picture – it’s the fact that we have write  = new JLabel(“0”); four times over and over again. Essentially we are performing the same function, but waste so much time writing that up!

One cannot nest this functionality in a for-loop directly, since the distinct labels will be used for distinct things in the future. One could of course define a custom label that took an enum value of what type of label it was, and then somehow through complex means one COULD put this into a for-loop. While I may be guilty of performing such hacks in my own code, for the sake of DRY or some other convention, it is of course, STUPID.

Ideas of aspect-oriented programming come to mind here, but again, why complicate things any further? All we need is a little syntactic sugar. We already have the idea of With in VB.NET:

Dim myButton = New System.Windows.Forms.Button()
With myButton
   BackColor = Colors.Black
   ForeColor = Colors.Red
   Text = "Go!"
End With

[Some smart-guy should now come along and say that I can push these along in C# as well via the constructor of a WinForms Button, the beauty of the With statement however, is that it can be used in any place with any thing!]

I’m sure other languages have similar implementations, and, while this may be a common feature to use, for some reason this is not a two way street!

Often, especially in cases of user interfaces you will end up doing the same things to different objects, and sometimes it just simply doesn’t make sense to abstract those elements in a way described above. So here is my suggestion for a bit of new syntactic sugar:

with {} do {}

or perhaps

do {} with {}

Where the latter is far easier to pronounce, but the former makes more grammatical sense when reading the code (I’m guessing).  In the do code-block we of course define a set of actions to perform, and in the with code-block the objects to perform those actions on, which all have a common super-type that implements all the methods requested in the do statement.

So let’s take a quick example, how might rewrite the aforementioned bit of Java code?

with { lblData1 , lblData2 , lblData3 , lblData4 }
do { #1= new JLabel("0") }

Or something like that, I’m open to suggestions for a better syntax! Specifically the task becomes more complex with something like:

lblData1 .setText(datastore.data1.toString());
lblData2 .setText(datastore.data2.toString());
lblData3 .setText(datastore.data3.toString());
lblData4 .setText(datastore.data4.toString());

One could imagine the need to introduce numbered or even tagged elements! Here’s a thought:

with { “name”:{ lblData1 , lblData2 , lblData3 , lblData4 },
  “value”:{ datastore.data1, datastore.data2, datastore.data3, datastore.data4 } }
do { #name.setText(datastore.#value.toString()) }

But wait, we are still rather not DRY, we still repeat lblData. and datastore. in our with block. Remember, this is syntactic sugar, so anything is possible! So instead, how about having the do-block as a form of a eval-statement block, and the with a list of arguments(perhaps as strings, but who wants to write the “” all the time?) . The idea is probably best illustrated with an example:

with { 1, 2, 3, 4 }
do { lblData#1.setText(datastore.data#1.toString()) }

Which is where a loop(1 to 4) would fit perfectly! And actually, a do with makes great grammatical sense as well, just look:

do { lblData#1.setText(datastore.data#1.toString()) }
with { 1 to 4 }

BEAUtiful isn’t it?

Of course, one could stack statements into both do and with, thus, this ought to be a many-to-many relation, but maybe that would decrease readability if used extensively, but then again, which language feature does not?

Unfortunately, our compilers can’t understand things like these(yet), and I don’t understand why, as long as the arguments in the with block are immutable of course! Maybe you have a valid reason?

P.S. Here use of static variables is actually practical, where as the constraint to static variables in a Java switch-statement is far less so! Which is why I often end up replacing such none-sense with an if-elseif 🙂


2 responses to “With <these things> Do <those actions>

  1. Hey Oleks

    You have a point in this article, and not to start a flamewar. But this is actually doable in PHP only with function names. Besides that you could’ve stored the labels in a list and looped over this list.

    Regards Willems

  2. Sure you can, as you can in JavaScript, ActionScript, and any language with `eval` functionality which often implies a scripting language, but of course, not necessarily 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s