Friday, 1 April 2016

Coding Standards: Functional Programming (Code Maintainability)

Functional Programming

When I were a lad, and was being taught to program, I read a bunch of Basic code and wrote great long lists of instructions; that's pretty much how many people in the 90's described programmers, coders or hackers "someone who spends lots of times typing great long lists of instructions into their computer" - Robert X Cringley.

However, long lists only get you so far, so my next step into the world of programming was to learn Pascal, and I first read a simple book as part of my A-Level, by P. M. Heathcote, which introduced very basic programs in Pascal, something like...

program HelloWorld (input, output)
begin
    println ('hello world');
end;

So, I suddenly had this idea of putting the long lists of instructions into sort of functions:

program HelloWorld2 (input, output)

   procedure foo()
   begin
       println ('foo');
   end;
   
   procedure bar()
   begin
       println ('bar');
   end;
   
begin
   print ('hello ');
   foo();
   print (' and hello ');
   bar();
end;

(Note: before you call me out and say you can "GOSUB" or "CALL" other functions in BASIC, not in the dialect I first learned, all you could do was SUB to a line number, following the instructions until you used RET to go back to where you were... So they were technical functions, or procedures, but they were really just more lines of code within the huge list of lines of code, no indentified in anyway, except by comments, as being functions).

Interestingly at this time, I was taught there were two kinds of functions you could call, ones which returned a value, known as "functions" in Pascal, and ones which didn't return anything known as "Procedures".  These latter you'll be very aware of from other langugages like C, where the return type is part of the function declaration, so "void foo();" obviously doesn't return anthing, whilst "int bar();" returns an integer.

That "Procedure/Function" definition stuck with me a long while, I was a kid, I was taught something and got a certificate to say to the world "He knows what he's talking about", so it was with some trepidation, years later, that I had to admit it was rubbish and everything was a function.

And this revelation came with my being taught about "Functional Programming", this was important for large projects written in languages like C, because you really wanted to start to learn how to keep functions doing one task.  So when you designed, named, write and test a piece of code, you can break it down, and know each piece, or each function, is doing just one job.

"void Max(const int& p_Left, const int& p_Right, int& p_Result)"

Here I've just defined a function prototype, even without my explaining, I'm pretty sure you could guess what it does... Yes, it takes the left and right integers given and decides which is the bigger, placing that value in the result.

We can design code, define the prototype, and hand the nitty gritty of the actual code body of to someone else, to write the body of it, or just to test our implementation works.  And though this is a trivial example, think about a single function to say format a disk, it might contain calls to dozens, or hundreds, of other functions, but it itself does one task, and each sub task is itself kept in a single function so you break the whole job down.  Within your large projects therefore you maintain a level of ease in how to debug, maintain and update the code, if your "foo" function doesn't work, just fix that one function and re-test, there shouldn't be multiple-foo's and there shouldn't be more than one task performed within the code inside "foo".

This was the first major meeting, and teaching, I had on "Functional Programming", and it was something I took to heart.  My code took on very much a "one function" style, where each function had a single purpose.

Twenty years on, and even in an Object Orientated world (of C++, C# and Java) I utilise the functional idea.

The main thing applying the single function ideal to my code has lead to is a vast improvement in maintainability, this has been important for my job and on going sanity with large projects.

However, there are other caviates, for there are other parts to functional thinking which have to be taken into account.  For example, are you going to allow functions in your code to have just one, or multiple exit points from a function?  For example:

const int bar (const int& p_i)
{
   return p_i * 2;
}

const int foo ()
{
    if ( x > 0)
    {
        return bar(x);
    }
    else
    {
    return bar(-x);
    }
}

This is perfectly reasonable code, foo does one job, deciding upon the value of x which value to pass, however, this is a trivial example, what if there are tens, hundreds or even thousands of different paths you could take as the result of foo?... A case statement with every character possible already throws hundreds of options our way, so it's not out of the ordinary.

You could be tracing through this code and ANY of those many exit points could fail, causing a crash, which you then have to dig through after the stack has all unwound.

Using many exit points is fine, if you can justify it, please don't get the impression I'm hating on the concept, if you have a low memory situation for example, I can see you won't have space spare for my suggested solution; and this is part of the many trade-offs you will learn about in a career of programming, when to, and when not to employ a technique.

But in the above example, I would change foo as follows:

const int foo ()
{
    int l_result = 0;
    if ( x > 0)
    {
l_result = bar(x);
    }
    else
    {
    l_result = bar(-x);
    }
    return l_result;
}

So, you see I have a local value copy, this is using more memory, and wants to allocate that memory... and there are other options than allocating that as a local variable, but for maximum maintainability keeping that value within the actual function is a key feature of this example.

And so how does this assist in maintainability?  Well, we can now debug the function a lot more easily, we can see the value of "l_result" at any moment in a watch within the debugger, we can wrap individual - failing - calls to "bar" into try-catch statements and debug the returned value for the whole function at the return at the bottom.

I've seen some horrors in the poor use of both returning from within a function, where return points are hidden three, four or more nestings deep, and it's made for nothing but frustration, try to avoid multiple function exit points.

What else does functional programming offer us?... Well, it also offers us an easy way to make code self-documenting, if we have a function like the "max" function earlier, it explained itself in just it's definition, take advantage of that fact.

But what about functions inside objects?  I hear you cry, well lets take a pure C++ example, in C++ (or at least most C++ compilers) we're allowed to just define a function prototype and go to town, but that's really; technically; C not C++.  To make things C++ we really should have the functions all inside a class definition... I stick to this idea, even if we just need a pseudo class of static functions:

class Helpers
{
    public:
    
          static void Max(const int& p_Left, const int& p_Right, int& p_Result);
          
          static const int Sum(const int& p_Left, const int& p_Right);
          
 };

 One can see what the functions mean, and the function have meaningful names, the class itself can have, a useful name, and indeed the class can then be in a namespace which itself has an even more useful name.  So we build up not complexity, as some assume, but ease of division of functionality, breaking numbers from user interfaces from strings from file management, break it down, divide and conquer, that was the original purpose of functions in computing, and so that original function is now emphasised in the languages and methods we employ today.

 C++, C#, java, Python, all can benefit from using the function idiom... Your projects certainly can.

Thursday, 31 March 2016

RIP Ronnie Corbett

And it's good night now from them both...


A comedian and entertainer, surpassed perhaps only by the other half of the Ronnie Duet which so defines his legacy, I'm so sad to hear of his passing this morning.

Though many consider his career in terms of his work with the late Mr Barker, Ronnie Corbett was by far the better man in the conversational prose, his work with the audience from a simple seat in the post-light is quintessentially the definition of his comedic genius, and no-one can deliver quite like Ronnie did from that position.

His warmth, passion for entertainment, and quick-wit always shone through, and he'll be missed.

Yet another great gone in 2016.

Tuesday, 29 March 2016

Wiring, Monitors, Ponies and Virgin Media

Sorry for the long delay since my last update, in the interim time I have been working on improving my recording technique from Arma 3 - so much so I have accrued around 25gb of footage, covering four different operations in the game, in long play mode - however, due to Virgin Media currently screwing me over (more about this later) I'm unable to upload any of it within a decent time-line.

So, what have I been up to apart from playing Arma 3?... Well, I've taken delivery of a new monitor, and Asus MX27AQ, which is absolutely lovely.  I've gone from running, and recording the games I play, in 1280x1050 to playing them in 2540x1440 and shadow play is recording them in 1080p.

Counter-intuitively for Arma 3 however, it's performance has improved, I've changed no settings, just moved from rendering over DVI at the lower resolution to using HDMI in the higher resolution, and the FPS has gone up, I now get 40-50 in a busy server (20 in towns) where I was getting, 10-15 in a busy server and 2-3 fps in towns.... Which is totally bonkers, and something I need to look at more closely.

In personal coding time, I'm totally on hiatus, I've been playing with Ackermans function, a few other code projects, the 2D engine for Dungeon crawling has been tinkered with, but I'm just so busy there has been no personal coding time.

I have however, finally had time, with the arrival of the monitor to sort out my desk wiring and networking, I've got the solid cat-6 cable from the router up through the floor and now going to a 12 port hub, from this I have my laptop, two PC's and a Pi working.  They're providing lots of working network points basically, for streaming or work.

I've then sorted the power wiring, to include  power smoothing extension now, which has 3 power smoothed and surge protected slots on it, these power the main PC's.

We've also been busy with Gerty, the pony, she had her first meeting with the dentist recently, having to have her baby teeth crowns removed to allow her adult teeth through, and the removal of two wolf teeth...


As you can see, she was pulling some funny faced afterwards.

And now VirginMedia, the wife asked me a couple of months ago to check our bill... £50 ish we're paying... and I figured, we're on the 200mbit, Tivo box, with channels galore, no problem... That's about right, I saw the newly named "Vivid 200" offer for £49.95, and figured we were on that... and left it be.

However, having tried to upload this serious amount of data over the Easter weekend, and been totally hampered by their throttling the upload down (yes, they still do this https://my.virginmedia.com/traffic-management/traffic-management-policy-thresholds.html) to nearly nothing, I got onto the chat help with them to ask for information... And it was very worrisome to hear the member of their staff telling me that they don't throttle things down.... Reading their live, current, traffic management policy, they do just that... so what hymn sheet are their staff reading from?

"Your downloads are not managed"... Whatever, I am talking about UPLOADS!

They just were of no help, and I figured, it's Easter, they'll be in some foreign call centre being paid pennies to read the answers from an expert system which is scripted to make them sound a little less useless then someone paid pennies to read a screen, should sound.

Today, I've logged into VM to see what my account status is, and not only am I now clamouring to talk to them, but I'm fucking furious... I'm paying over £50 a month for the M package!... the medium... 50mbps... 

For that same price one can get on the Vivid 200 package!... So I automatically hit the button to check what offers they'll give my account... they're only offering me the Vivid 100.... 

Why's this making me furious?.... Because I was told I was getting 200mbps just the other day, how can I be when my package is capped at 50?... GAH... so now I'm trying to chase this all up.... Watch this space.

Oh, and now their support site is fucked... VIRGIN FIX YOUR EGAIN SYSTEM!

Thursday, 10 March 2016

Arma 3 - Grenade Worries

Last night I decided to take a look at come add-ons for Arma 3, notably the ACE3 pack, I was unfortunately disappointed with the pack, it seems to obfuscate so much of the learned functionality and change so much as to make it feel alien rather than a helper.

One notable problem was, no matter what I did, I could not lock nor fire an AT missile from a Titan Compact launcher, even dropping into the editor, sitting a target in the open and trying to fire point blank it'd not lock on.



Reboot and try without Ace, everything was fine, and this leads to a problem Dyslexi has passed the mantle of his excellent looking grenade mod over to Ace, if that grenade mod ends up mixed into the mash up within ace then it maybe impossible to separate it back out and run it without the perceived confusion Ace has caused me.


The other thing I looked at in the edit, as one can see from the above screenshot, was wind direction and it's affect on the battlefield.  Primarily I looked at this because I've been continually gravitating on the Zeus Community Server, however, their server seems to never have any weather other than the occasional splatter of rain.  So seeing wind make smoke drift was interesting.

Don't forget, you can find highlights and full streams of my adventures in Arma 3 on youtube now.



Sunday, 6 March 2016

My New Monitors - Research & Pricing

No game footage this evening, we have been playing, but we've also been working hard on the house.  Well, since its now a fortnight since my surgery, I've been forced out into the cold by the wife to get on with some jobs, the first of which has been to clip the rear conifers, they were getting... well I'd like to say out of hand, but that's an understatement, you know when you don't notice something?... Well we'd not really noticed they'd expanded at the base by 6 feet in diameter.

Clipping this excess away on the drive-way side has allowed us to move the junk from below them, and now fit the new van along with the estate car onto the drive way, result!

Next, I've been told to decorate the kitchen, this is on hold awaiting some tools I need (mate you know which ones I'm on about, hehe).

I also have to finish the plastering in the middle room and get on with painting it, so it's all go.

But, last night we spent a little over £114 at Ikea, we bought a new stainless steel standing lamp, the Klaab model, for the middle room, I got some LED down-lighters, and stuck them under the kitchen cupboards, to the kitchen is now lit at night not by the combines 120watts of overhead lighting, but just 0.6watts of LED lighting, and it's almost as bright at the worktop level, where we need the light of an evening.

We also have a new solid metal hat & coat stand, and I have a lovely new pillow for my office chair, which I'm sat on right now.

However, all this aside is not the exciting thing about the evening, no, for this evening I've had the green light to pick new monitors for my office...

My requirements here have been dual screen for a long long time, and I generally play on my Dell monitor and play video on my phillips monitor, they're both 1050p (no, that is not a typo, they're not even full 1080p).

Now, both of these screens are going to be retired, one will be kept as a spare, the other sold on ebay for a bare minimum.

But, the question is where do I go?... Now, I still need a VGA connector on one of these new beasts, and I need at least two... The rest is up in the air, I have dial DVI-D on my graphics card, a HDMI and a display port output, so I have the world of monitors at my behest....

Wait, wait, why VGA you ask?... Well, because I have to repair broken machines from other people, and I have a vested interest in older PC hardware.  All of which require a VGA port as a bare minimum for me.

This VGA requirement is most definitely not for my main screen, so lets ignore that for now and discuss more about what I require for my main screen.

MOOORRRREEEEE SCREEN SPACE!... Yes, it's as simple as that, with virtual machines, development pages, scripts and other junk open on the main screen; even with the second screen; I need more space in that my main work space, so I want a resolution of 2560 x 1440.  I'd also like the screen to minimum eye strain, so Low blue light, and a high view angle (IPS) would be good.

How big do I want the screen?... Well, the current screens are both 23.5 inch.  And we're upgrading... So lets say 27" as a minimum, now I want two of them, so that's a combined length of 54".  And looking at my screen any larger is not going to fit.

Right, our first milestone, we've picked a size... So, what have we got... Hitting the "Gaming Monitor" reviews, I want anything with a response time of lower than 5ms... I would say I'd love 144hz or 1ms but, to be honest, we running an nVidia 770 GTX card, so 144hz refresh rates at such high resolutions we'll have available are going to be impossible.

Now, searching, I got several options, my notes...


Asus seemed to be a top running brand, as does Acer, Benq and Samsung...

Samsung however, all seemed to be more expensive and less able, so I dropped them out of the mix.  This is a shame as I have all Samsung electronics screens for my TV's in the house, and I'm happy with them, but I'm not going to be paying a premium for lower spec screens here.

I did also, at this time, take a good look and have notes on 24 inch screens, and came across the Asus VX24AH, a 2560x1440 resolution 5MS IPS screen.  At only £200 this was a bargain, and is still a strong contender for my secondary screen, however, it's only 24 inches... 

I then considered three of these, in portrait mode, that would rock, even with the bezels in the way, however, the stand was not adjustable, it only provided tilt, so three of them was not going to be possible without also buying into a complete mounting system, but these desks could't support that, so I'd also have to be fitting to the wall, and the wall has old Victorian era plaster (yes 150 year old plaster!) which is about ready to come off.

So, quit the 24 inches, and look at Asus 27 inches... Ooo, ooo... I got a heavy high contender for the main screen... The Asus MX27AQ, it has ultra low blue light, IPS, it's 100M:1 ratio, 300cd/m2, 5ms response, 2560x1440 resolution and it has display port and HDMI, so can be driven easily from my card without need of a splitter or display port hub.  It come in at a little under £400....  Via amazon....That is very expensive, but I want to buy something of quality and then a lower priced utility second screen... However, I notice it's sold through Amazon by Scan, so what's their direct price... £375... Already £25 cheaper... I could potentially go pick this screen up too, that'd be £4.95 delivery saved... That's 5 litres of diesel paid for, which is more than enough to cover a 4 hour round trip... But, for now, we'll assume delivery from Scan at £4.95 on top, so we're at a price of around £380.

Next a secondary, the Asus appeal now is the small bezel, and this is going to be side by side, so what else do they offer... Well, in the MX series, there's the MX279H, a screen with rave reviews, AH-IPS, 300 cd/m2, it has VGA and 2 x HDMI, so is okay for my bare minimum connectivity, it's only 1920 x 1080 resolution, but it is 100M:1 ration, 5ms response time and AH-IPS.  It's a cost of £229 though, to very much too much with the main monitor included.  Though they'd look identical.

With Scan this second screen is £10 more than with Amazon, and with their delivery it's £14.95 more, so Amazon is the source there.  Two different screens from two deliveries, not ideal.... But, we're after cost here.  However, my budget for this is around £500, and I'm at a standing of  £609, that's definitively over budget.


However, in the Asus range I also spotted the VC279H, it's a much lower fidelity screen, 80M:1 ratio, I doubt from my old screens I'd see any difference, but it's a cost to performance challenge now, then the resolution is the same 1920x1080, and 5Ms response, the connectivity however is a little better for my needs, VGA, DVI and HDMI.  Costs, this is £172, that's bringing the main screen and this as an option in at £552, I think £52 over budget I can talk my way out of.

The question is with this lower priced VC series, is, "how different" is different looking?... And it seems, not very...

From the images I've looked at (which we can see above) the main differences are the bezel, on the main screen will be silver, whilst black on the secondary, and then the stand is a hollow sun dial style on the primary and a solid plastic on the secondary.

I think however, this is the end of my searching, and I'm going to the wife with a cost of £552.

In my defense, my current right hand screen is a little under 15 years old, whilst the second screen is around 7 years old.  I got both of these for around £125 each, so I've made that investment last... At this ratio, I need to make these new screens last well into 2022, and I think they'll do that with ease, I certainly see my needing to rebuild the whole PC before I buy anything newer than these screens.

Friday, 4 March 2016

Arma 3 - Coop - Medic by Night, Light Machine Gunner by Day

Today's coop round finds us running around as a medic by night, and then a light machine gunner by day...


I also get very grumpy with a certain helicopter pilot... or is that wanker who thinks he can DJ in a game?....

Tuesday, 1 March 2016

Arma 3 - Coop Play - Infantry Havoc

This stream saw us take up the role of medic for Alpha Squad, we were inserted into the AO via nighthawk and moved along the coast from the low ground, things went pretty much wrong from there on in, we lost our recon team, our A10 over cover was shot down, our support helo was shot down and when we were tasked with rescuing the pilot he was shot dead upon our arrival.

The stream picks up nearly an hour into this havoc, and finds Alpha pinned inside the perimeter of a small electric generating site, the roof of the generation hall has proven deadly, and we're running low on ammo...


After that hiccup the stream continues with the massive push by the enemy, so many soliders (rendered in ultra quality) are approaching the server is struggling to control them all, and my 770 GTX is struggling to render them all.


Finally the fire fight is over, Alpha Evac is ordered, we pop blue smoke, though the support pilot does not land on our marker... But we lived to tell the tale... Or did we?