Technical solutions and general life savers

Sometimes our lives as programmers get a bit easier, Microsoft has launchedlaunched C# 7.0 some time ago. Here are a few examples of the things that made our lives a lot easier.

The microsoft page has better explanations, the code below is just random code from my project. These examples are only intended to outline some of the new features.

Pattern matching

1

2

3

4

5

if(sender isToolStripMenuItemTranslate)

{

ToolStripMenuItemTranslate item=(ToolStripMenuItemTranslate)sender;

TabPageAddOrShow(item.CustomTabPage,ModifierKeys==Keys.Shift);

}

Can be rewriten as:

1

2

3

4

if(sender isToolStripMenuItemTranslate item)

{

TabPageAddOrShow(item.CustomTabPage,ModifierKeys==Keys.Shift);

}

Using the underscore we can ignore variables, which keeps the compiler (and resharper) happy: DoSomething(out String variable1, out String _)
Which brings us the pleasure of inline variable definition as well as ignoring unused ones.

It can also create funny code (introducing underscores in useless places):

1

if(sender isToolStripMenuItemTranslate_)

Tuples
While we have had basic tuples for a long time they grew up and gave us a weapon against the useless class/structs to perform returns (or way to many out variables).

1

public(Stringresults,Int32 exitCode)ExecuteCommand(Stringcommand)

Keep in mind that a tuple is a struct!

Switch statements with patterns
While switching still isn’t available on primitive types it sure improves our lives a lot:

Apache2 (2.4) with nghttp can sometimes appear to be loading for minutes on somewhat larger output files (js e.t.c.). While tools like the Firefox “Developer tools” display “plain” as the content type.

Simply adding the following to the apache config fixes those problems:

Sometimes in the life of a programmer everything seems peachy, C#/DotNet provide relatively easy multi threading capabilities which 99.9% of the time work just fine as explained everywhere online.

However when things get a bit serious as these online explanations tend to be off by a small bit, only that small bit creates havoc….

Imagine having to debug code within a lock() statement that crashes with exceptions like “Collection was modified; enumeration operation may not execute” which seemingly bypass any locking in place.
These are not fun to deal with as everyone will for some reason stubbornly tell you that that is not possible and you are simply lacking a lock somewhere and just moving on like they have just shed divine light.

First of all lets get the common misconception cleared that it is impossible for a single thread to execute code in parallel and even worse that events will always be fired with a new thread affinity. This is just plain wrong!

Create a simple empty form with the following code in the shown event:

1

2

3

Button testButton=newButton{Dock=DockStyle.Fill};

testButton.Click+=Test_Click;

Controls.Add(testButton);

And paste the following somewhere below:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

privatereadonly object_lockHelper=newobject();

privatevoidTest_Click(objectsender,EventArgse)

{

lock(_lockHelper)

{

Debug.WriteLine("A");

inti=0;

while(i++<50)

{

Application.DoEvents();

Thread.Sleep(50);

}

Debug.WriteLine("B");

}

}

If what everybody proclaims is right this code should just lock up the UI and neatly print “A”, “B” in sequence when triggered.
This is where the bad news starts, it doesn’t, simply clicking the button a few times very easily prints “A”, “A”, “B”, “B”.
So with a few lines of code we have disproved two very stubborn myths in one blow.

This properly locks up the UI as expected (it’s example code, not release code) and can even be used for some more interesting purposes when multi threading.

Please don’t be like all the other assholes online who proclaim that this doesn’t happen in normal production code (or even worse that a simple lock() should always be sufficient as events have different thread affinities).
Simply imagine a simple asynchronous communication protocol between two programs or a server and a client, having simple locks can seriously fuck up your day with some long debugging ahead.

For MariaDB fans there are some changes coming in the current Debian testing/unstable, which will hit mainstream sometime this year (I guess).
A lot of things compiled against libmariadbclient.so.18 will start failing, most of which can be solved with a simple symlink except a few…

The opendkim one is pretty vague and seemingly unhelpfull but a bit of digging will reveal the following:Can't load '/usr/lib/x86_64-linux-gnu/perl5/5.24/auto/DBD/mysql/mysql.so' for module DBD::mysql: /usr/lib/x86_64-linux-gnu/perl5/5.24/auto/DBD/mysql/mysql.so: symbol mysql_options4, version libmariadbclient_18 not defined in file libmariadbclient.so.18 with link time reference at /usr/lib/x86_64-linux-gnu/perl/5.24/DynaLoader.pm line 187.
at /usr/sbin/opendmarc-import line 22.
Compilation failed in require at /usr/sbin/opendmarc-import line 22.

Not limiting the search areas, this results in a lot of false positives (e.g. any of the extensions appearing AFTER the filename has been specified (within the encoded attachment) would trigger the rule)

Failing to correctly identify and target the different filename formats (filename=”test.zip” or filename=test.zip;)

Some of us enjoyed the old @reboot crontab entries but the recent upgrades to systemd have created some new challenges.
The most prominent being that a simple systemd service kills any spawned processes after it exits. (I know it is not supposed to be used like this, but for some simple services like mailgraph writing service files for everyone is just too cumbersome).

There is one thing all programmers experience at one point or another, performance issues… One might use simple break points to get a coarse idea of the problem locations or when things get really frustrating a stopwatch with several measurement points.
But even that gets frustrating when there are lots of problem areas and it soon turns into a cumbersome practice which we all loathe.

But nothing beats some good old fashioned scripting to create a more elegant solution:

You might or might not have heard about XML and JSON. And wonder which one is better to represent data with for communication, save functionality or config.

Before I do so I will explain what JSON and XML are in depth. If you are not interested, skip to ‘One over the other’

What is JSON?
JSON stands for JavaScript Object Notation and is actual javascript code. When put in to a javascript evaluator JSON can directly be translated in to objects, array and primitive types (integer, string, float).

A basic JSON string containing a person object could look like this:

1

2

3

4

5

6

7

{

name:‘John’,

age:14,

length:1.55,

profession:‘Student’,

friends:['Suzzy','Cate','Mike','Tom']

}

Although JSON can only be parsed when well formed. Their is no default possibility to define what every value should look like. If you want to put ‘hello world’ into the length property this is valid. To do such checks code should be written to check these properties.

As noted before that JSON is actual javascript code. So parsing of these string is in general very quick.

What is XML?
XML stands for Extensible Markup Language and has been standardized by the W3C (World Wide Web Consortium). And has been created to standardize the way HTML (HyperText Markup Language) is written.

The same object shown above could look like this.

1

2

3

4

5

6

7

8

9

10

11

12

<person>

<name>John</name>

<age>14</age>

<length>1.55</length>

<profession>Student</profession>

<friends>

<friend>Suzzy</friend>

<friend>Cate</friend>

<friend>Mike</friend>

<friend>Tom</friend>

</friends>

</person>

I would like to point out that this representation automatically makes ‘objects’ from friends. Make it easier to put more properties on these. But the same can be done in JSON. So far you can represent the same complexity of data.

XML however has 2 things that make it more powerful then JSON.

First off is XSD (XML Schema Document) can be used to validate the structure and data types (not if it’s well formed, because it needs to be well formed to be able to check it against an XSD). XSD are XML’s with tight regulation on how they should be build and describe how your XML should look like. Thus using an XSD makes it very easy to pinpoint troubles in your data structure as it will give an accurate error of the problem.
For further information check: http://www.w3schools.com/schema/default.asp

Second is XSLT (EXtensible Stylesheet Language) which can be used to format a XML to more readable formats. Although only text can be produced by default, like HTML pages or txt files. This format is also tightly standardized and is being used as a base for PDF generators and other formats.
Check http://wiht.link/XSLT-intro/ for more information.

Finally I would like to point out that XML needs some getting used to when first used to write a data structure for. With JSON objects and arrays explain themselves. Defining structures comes naturally as it is clear what every part does.
With XML it might not always be clear how something should be represented at first and some research is needed. This is especially true when using XSD or XSLT documents.

One over the other
Here under I will point out the strengths and weaknesses of both formats.

Using JSON over other interchange-formatsPro:

Can be read more quickly

Little overhead

Fast writing

Easier to learn

High performance deserializing

Cons:

Needs time getting used to

If you write your own JSON parser its hard to debug

Harder to cut in to chunks

Thoughts on XMLPro:

Better error handling on syntax errors

More easily readable with deep levels

Superiour data integrity checking when XSD is used

XSLT can be used to formating for readability

Con:

To make error handling work their is need to make a XSD

Overhead with closing tags (slows down reading too)

Chance of conflicting names and functions of nodes

Necessity to create well formed XML constructions to represent your data

Harder to learn when structures become more complex

Conclusion
Although you might be dazzled or tempted to choose one over the other. I would like to point out that neither is absolutely superior and either can be used for all purposes you might have to represent data. However one is more suited for certain applications then the other.

If you have a website with lose data calls and no refresh, use JSON. It’s faster and has less overhead. In this fashion JSON is suitable for other communication and cases you need small size and high decoding speed.

If you need readability and tight error control over large sets of data that are being maintained by people. Use XML. It’s XSD data integrity checking makes it far easier to write by hand and XSLT can speed up human checking. So it’s ideal for config files, documents and dumps of large chunks of data that are complex in nature.

Tooltips provide an easy way to add useful information without cluttering the screen with enourmass chunks of data. ExtJs has fitted a couple of elements with a default tooltip. Button, Grid.Column.Header and Window are examples of that.

But sometimes you find yourself wanting to emphasize other things like a grid, rows, grid cells, an icon or blocks of text.

There is qtip, which can be placed within the HTML code directly to provide the user with a simple tooltip. But you have to encode everything to not make the first “ or ‘ break it.

So I recommend using the tooltip class.

Basic tooltip

This will set a tooltip on any element with the CSS class “tooltip-target” that is within the the panel “this.mainPanel”.

The CSS class does not have to exist for this to work. You might however want to add an empty CSS class with a comment, just to be sure it doesn’t get cleaned out by accident by someone that stumbles upon it.

1

2

3

4

5

6

7

8

9

10

vartip=newExt.ToolTip({

target:this.mainPanel.getEl(),//target to apply tooltip on (also scope of the search, MUST be an Ext.element)

delegate:'.tooltip-target',//target the tooltip will be shown on

delay:0,//how long to hover before showing

trackMouse:false,//follow the pointer while over element

renderTo:document.body,//where to draw, document.body is recommended

autoHide:false,//don’t hide after a while

anchor:"right",//where to show the arrow on the tooltip

html:"Tooltip content"//content to show

});

Grid cell

To make it work on a grid, just provide a different delegate and target. This will popup when hovering a row. If you want to make it select on just some cells, make up your mind in the renderer, by adding the previously mentioned “.tooltip-target” as delegate.

1

2

3

4

5

6

vargridTip=newExt.ToolTip({

target:this.grid.getView().mainBody,

delegate:'.x-grid3-row',

renderTo:document.body,

html:"Tooltip content"

});

Updating the content from given element

Nice, but now I got fixed content within the tooltip, making it hard to use. You first thought might be: “I could make more CSS classes to hang other tooltips from .”

Consider that every tooltip will be activated when entering the scope of its target elements. This could create a performance hit.

Instead update the content of the tooltip with the element you just hovered over. Is most cases like with a grid the data that the element you hovered over originated from is in a store. Or more precise, inside a record.

So for a grid you could use this after creating the tooltip.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

gridTip.on("beforeshow",function(tip){

//the element being hovered over

vartriggerElement=tip.triggerElement;

//get row index

varrowIndex=this.grid.getView().findRowIndex(triggerElement);

//get record

varrecord=this.grid.getStore().getAt(rowIndex);

//you could also format this

//or use another store to add even more information

varcontent=record.get(“some-column”);

//update the tooltip

tip.body.update(content);

},this);

Update tooltip with call

As long as the function is delayed, the tooltip will not be shown. So it is a good practice to add a loading message and update the tooltip as soon as data from example an AJAX call is available.