How my stand up comedy class instructor made me a better coder

Years ago, while I was still serving as a military medic my sergeant asked me why I hadn’t signed up for a unit golf tournament.

“Well sergeant,  I don’t like to do things that I do not excel at.”

Her facial expression upon hearing this was very memorable.

As I grow older I’ve started to realize that my drive to find better ways to do things sometimes gets in my way.

 

I was working on a function to recursively parse a string with multiple delimiters into an associated array.

I had it working,  but thought that I could make it more efficient.

That worked,  but thought I could make it ever more efficient.

…you get the idea.

It was getting to the point where I was getting frustrated.  There was an issue with recursive function calls weren’t returning the proper data.

I couldn’t figure it out.

I took a break and saw an email from Jim,  the instructor of the Stand-Up Comedy class I’m taking at Second City in Toronto.

The email contained some notes on a 3-minute set I’d delivered at the last class.

Jim gently pointed out that I was striving for perfection right off the bat and that it was getting in my way.

He talked about how even seasoned comics work to avoid that trap.

He was right.  I had practiced bits of the set for hours working on every nuance of delivery and wording.

I turned back to my code a few minutes later and realized that seasoned programmers and seasoned comics have some things in common.

73 lines of code became 127 lines, and done!

 

 

 

 

Switching Active Tabs Programattically: Jquery/Bootstrap

There are lots of different ways to control visibility of different types of web elements.

I’m working on the web front end of what will eventually become an HL7 message inspection tool.

I’ve set it up so that when the user pastes an HL7 message into the textarea on the #omsg pane,  the results will be pasted into the #parsedmsg pane.  I then want the latter pane to become visible.

I set all this up using bootstrap tabs.

The tab controls are anchors wrapped in list elements.  To show what tab is visible, Bootstrap relies on the class “active” to be added to the parent li tag.

I don’t know if I’m having an off day but I couldn’t figure out how to have that happen when I switched visibility programmatically.

The panes would switch visibility just fine, but I naturally wanted the active tab button to be highlighted.

Instead of toggling visibility of various elements,  I decided to write a function using DOM traversal to simulate clicking the desired button.

I’ll go into more detail below, but this is the function itself.

function activeTab(newtabid) {
$('#lpane > ul > li > a[href="'+newtabid+'"]').trigger('click');
});

The tab controls are contained in a div with the id of “lpane”.  As Bootstrap tabs rely on the href attribute, its easy enough to find which element to click using that particular parameter.

Here is the HTML

<div id="wrapper" class="col-lg-12">
<div id="header" class="col-lg-12">
<h3>HL7 Tool</h3>
</div>
<div id="lpane" class="col-lg-12 centered">
<ul class="nav nav-pills " style="text-align:center;">
	<li role="presentation"><a data-toggle="tab" href="#omsg">Original Message</a></li>
	<li role="presentation" id="lpmsg"><a class="btn" data-toggle="tab" href="#pmsg" href="#">Parsed Message</a></li>
	<li role="presentation"><a class="btn" data-toggle="tab" href="#redset" href="#">Redaction Settings</a></li>
	<li role="presentation"><a class="btn" data-toggle="tab" href="#redmsg" href="#">Redacted Message</a></li>
</ul>
</div>
<div id="cpane" class="tab-content col-lg-12">
<div id="omsg" class="tab-pane active">
<label for="#omsg">Original Message</label><textarea id="msg" class="form-control" placeholder="Paste Msg Here"></textarea>
<input type="button" class="btn" value="Parse" id="parsemsg"/><input type="button" class="btn" value="Clear" id="clearomsg" />
</div>
<div id="pmsg" class="tab-pane">
<div class="col-md-6" id="parsedmsg"></div>
</div>
<div id="redset" class="tab-pane">
REDSET
</div>
<div id="redmsg" class="tab-pane">
REDMSG
</div>
</div>
</div>

and finally…the actual script


<script>

$(document).ready(function(e){
 
$('#parsemsg').click(function(e){
 
$('#parsedmsg').parsehl7({msgin:$('#msg').val()});
 
 activeTab('#pmsg');
 
 }); //parsemsg
 
 }); //document ready

</script>

 

 

Mirth: Making sure that dev/test code doesn’t get into LIVE (Is this lazy, or just accommodating reality?)

In a perfect world database connections and other things that must be changed when you migrate code between environments are neatly stored in one place where they can be easily changed.

With Mirth you can do this using global maps,  however when you do that,  they are exposed to anyone who has appropriate access to Connect.

I get around this by declaring variables at the top of the filters or transformers where I make database calls.

One of the things with my position is that I wear many hats and am the organization’s ‘go to’ for a great many things.  This means that interruptions are frequent.

This means that I when I’m doing something like preparing an interface to move from TEST to PRODUCTION that I can miss things.

Some of you might scoff,  but that is the reality of my world,  and almost 50 years on this planet has taught me that reality is often very far from ideal,   so you plan for reality.

I’m working on a project where I reach out to a couple of databases to pull data that isn’t included in the HL7 message.

Some of the transformations are complex,  and things were complicated by very tight deadlines,  and a situation where the spec “needed clarification”.  (as in I built my end to spec but they wanted a change).

Those of you familiar with this reality thing will know that this often leads to kludged together solutions.

After spending several hours making sure that our iPeople Echo downloads were moving the correct data from our test environment and copying/altering stored procedures and SQL functions I finally got to combing through my Mirth interface to make the changes there.

To give you an idea of the complexity,  I have 13 transformers in my source connector.

I realized that when I moved this to Live that there were too many potential failure points and wanted to prevent them.

So I added this code to my source filter:


var channelName = ChannelUtil.getDeployedChannelName(channelId);

if (channelName.indexOf('LIVE') > -1) {

logger.error('******************** CHECK ' + channelName + ' FOR DEV/TEST SETTINGS! ****************');
 return false;
 
}

return true;

/* Changes for Live
-remove anonymizer
-change db settings in source PID, PV1, OBR and ZDR
-change db settings in destination MR PID transformer

*/

Hey look…a checklist of sorts!

My thinking is that if I forgot to make this very basic change that I’ll look and wonder why all my messages are being filtered!

But then I got to thinking of how many times I’ve been interrupted today and what would happen if I missed a single change…

So I wrote this:

function envcheck(sql,strTest,strLive) {

 var channelName = ChannelUtil.getDeployedChannelName(channelId);

 if (channelName.indexOf('LIVE') > -1) {

 logger.error(channelName + ' **** CHANGE SQL STATEMENT FOR LIVE ' + sql);

 sql = sql.replace(strTest,strLive);
 
 }

 return sql;
 
 
}



You know you should take a break from coding when…

Recently,  I was trying to get something in our Mirth Integration Engine working.  A project that was behind for a variety of reasons.

I was exhausted,  and stressed when co-workers heard me exclaim:

WTF do you mean its undeclared!   Its declared right there you stupid machine!!!

I waved my fist at the monitor in a threatening manner,  yet this infernal contraption kept telling me “variable mytpe undeclared in line 936″

I thought of EVERYTHING!   I checked to see if scope was the issue,  I was pretty sure it wasn’t.  I thought maybe its a datatype thing,  which made no sense at all…

Finally, in a fit of frustrated rage I walked away, grabbed a coffee, and paced around for a bit before going back to it.

I then took another look at the code


var mtype = msg['MSH']['MSH.10']['MSH.10.1'].toString();

if (mytpe.indexOf('D') > -1) {

}

I checked everything except spelling!

There’s a few minutes of my life I’ll never get back.

Unintentionally Creepy Declarations #1

When we’re in the zone,  sometimes we don’t really think about our variable names,  so long as they’re meaningful.

I just typed out a table declaration for a new Table Value Function in SQL Server.

After I coded that bit I kept thinking,  there’s something wrong with that…

Then I saw why it was creeping me out…


DECLARE @table as TABLE (

DICTSECONDS varchar(30) NULL,
DRAFTSECONDS varchar(30) NULL,
FINALSECONDS varchar(30) NULL, -- do you see it?
SIGNEDSECONDS varchar(30) NULL
)

My Coding Playlist

Music is like religion to me.

A close friend once told me that she can tell how my life is really going by what music I’m listening to.

She refused to elaborate,  knowing full well it would change the algorithm.

My various playlists have everything from Five Finger Death Punch to Handel’s Water Music.

You will find they are heavy with Electronica though.   This surprises many as I’m months away from my 50th birthday and am not inclined to attend “Raves”  (most of them happen after my bedtime).

Electronica is my preferred “Coding Music”.   It is energetic,  powerful and driving,  however it mostly lacks any sort of lyric or message that will stop me dead in my tracks.  “Eat, Sleep, Rave Repeat” was an exception,  I was laughing when I first listened.

I thought I would share the favorites from my Coding Playlist.  Those that have lasted the longest.

That Miami Track – Tocadisco

You’ll Find Yourself featuring Sarah Howells   John O’Callaghan

Better Off Alone:  Alice Deejay

Galvanize: Chemical Brothers

And of course…

Eat Sleep Rave Repeat : Fatboy Slim

That should keep you going for awhile 🙂

PHP: BREAK and CONTINUE from Nested Routines (Such as loops and switch statements)

One of the most frequent “gotchas” that I run into are breaking out of loops properly.

Its one of those things that I don’t really think about when I’m banging out code,  and that I can miss when I go back to see why things aren’t working.

I’m currently working on a project that requires me to parse through comments entered into our Meditech Hospital Information System,  looking for key phrases and performing certain actions based on these key phrases.

The issue is that the comments are a single string (meaning that users may enter several things into the same field (its a Meditech thing),  and that this is free text.

I won’t bore you with the details, but suffice it to say, “Yay Reg Ex!” 🙂

The routine I’m working on looks to see if the user entered a month if a particular code (EDD) has been entered into the field.

That was easy enough,  but what I found that some users would enter a numeric month day,  or day month.

So I did a preg_match to find that.   I found that the script would hit this every time,  even though I placed it where it should only run if the month string wasn’t found.

I quickly realized that the break statement I placed to break out of the month search loop was only breaking out of the case statement!  So when it went looking for the next month,  it wouldn’t find it and as such would run the digit match routine!

The way to handle this is really easy…both the break and continue statements in PHP have optional integer arguments where you can specify how many structures to break out of!

Don’t worry,  I’m not going to paste in my whole solution and let you find the interesting bits.  You can always contact me if you’d like more detail on the project I describe.

In this example I’ll show you how to use continue from loops and nested loops.
You can use the same logic for breaks.


foreach($results as $row) {

 if ($row['text'] == 'EDD') {
 
 continue; //this will skip to the next $results;
 
 }

 foreach($pattern as $check) {
 
 if ($check['action'] == 'edd') {
 
 //do something here
 continue; // continue to the next $pattern
 
 } else {
 
 if ($something == $thathing) {
 
 continue 2; // continue to the next $results
 
 } else {
 
 break 2; //breaks out of $results
 
 }
 }
 
 
 }

}

In this example I’ll show you how to break out of a switch case statement

foreach($results as $row) {
 
 switch($row['text']) {
 
 case 'edd':
 
 if ($datacheck == 'yes') { //something indicating that you're finished processing $results
 
 //some code here
 
 break 2; //breaks out of both switch and $results
 
 
 } else {
 
 
 continue; //jumps to next $results
 
 }
 
 
 break; // case break
 
 default:
 
 //switch value wasn't handled
 break;
 
 
 }
 
 
}

Hopefully you found this useful. Please feel free to ask questions or suggest improvements in the comments section!