Monday, 26 March 2018

C++ : Pass-By-Reference Or Die

Before today's Post, I'm on a mission folks, to get 1000 subs on YouTube.  If only 5% of viewers here subscribed we've have met this target in one month...



I've just had group code review of one of my personal projects, and been rather surprised by the vitriol levelled at one of my practices.... Pass by Reference.

The reviewer, one of a group of peers, has had major issues with the project (my personal) insistance on passing by reference wherever possible, in C++ this takes the form of an additional ampersand on parameter definitions; maybe this was the chaps problem, he has to type an ampersand?

So his problem?  Well, without the actual code we'll simplify and use the Compiler Explorer (from Godbolt.org) and we'll take up their basic square function example, it starts up thus:

Giving the assembler:


On the right, and this chap had taken time to prepare a whole slide show of functions, usually simple, and present them at this code review, showing this kind of thing.  His point... Well the very same C++ but with a pass by reference:


Turns up more lines of assembler:


He's got me right, right, I'm taking more time, I'm slowing everything down, by my not taking a copy of everything and using less memory I'm slowing things down....

This is where the sort of power play turned, I allowed him to present everything, I never interjected, never spoke, I allowed him to speak to the whole group.  We've hired a venue for this, we're meeting live for the first time.  This has to be good.... A couple of the chaps who can already see the fault in the complainers logic were smirking, but we let him finish.

Triumphant, he has won the day, he will not carry the torch of coding standard gods...  WRONG.

I pulled over the presentation laptop, opened godbolt.org myself... Added the ampersand to the "num" and let it produce the above assembler... The chap was smirking completely from ear to ear, he knew he had me...

And then I typed three characters....

-O2

Yes, I told the compiler to optimize, and this happened...


Remarkably small code wouldn't you say?  I still haven't spoken, but I turn the laptop back to the presenter and just sit there.

There's a noticable snigger from those in the know, older-wiser heads then my own I hasten to add.  But this young chap is now looking from me to the screen to the overhead projection and back with a mix of fury and completely puzzlement, he'd checked everything, he's dotted every j and crossed every t, he had me down pat, he wanted to usurp me.

Except, he's never ever, been willing to listen, to learn or to experiment, "code runs, that'll do" is very much his style (and Kyle if you're reading, yes I'm talking about you) but getting code to run is not enough, understanding the code you've written is often only just enough, but getting it to run everywhere, the same way, that's an art.  Debug, Release, Optimised, Unoptimised, automatically profiled, link database and continune they're all subtly different.  Just listing one thing out, the only thing you've looked at; because it backs up your point of view; is not enough you have to look around and see the holistic picture.

And optimised without a pass-by-reference?


Spookily similar code in this case, but often times pass-by-reference is prefered, using const-correctness is prefered it communicates a meaning.

For instance in the "square" function above, how does the caller know that the parameter "num" is not altered in value?  How does the caller know it returns the new value only?  It could be returning an error status code and the parameter altered in value!  You don't know, but making the parameter const and a reference you start to communicate more firmly the intent of your code.

Tuesday, 20 March 2018

More Power.... Cap'N

Yeah, I think my git server is chugging now... It only has 1GB of RAM and one core... And when doing a full new clone the "remote compressing" phase is taking well in excess of 14 minutes... Time to upgrade it me thinks.

Penny for your thoughts?

Tuesday, 13 March 2018

Great Rack Mount Mistakes #6

A long time coming, here's another story from my days long past, this one takes me to my very first serious role in an IT department, I was however just the dogs body.  The company ran many old PC's (which I actually was around to see mostly be updated to nice Compaq Pentium III's) and they had a couple of high spec Silicon Graphics workstations in the design department.

The main manufacturing control and purchasing system, as well as payroll and a bunch of other services ran on a dual 386 based mini computer, which had a custom cut of ScoUnix and a bunch of bespoke C programs comprising the actual system stack, this was accessed by a whole host or Gandalf multiplexers combining the serial connections down from a hundred or so Wyse brand terminals (I wish I'd have nabbed one of those before I left).

Anyway, it was time for this back end stack to be updated, and so a pair of Compaq Proliant servers were brought in, these were dual Pentium III class with a dedicated storage unit and a large; and importantly heavy; UPS unit.


The problem?  The IT manager I worked for (Hi Dave) didn't get on at all well with the manager at the co-location this unit was to be installed in.  Therefore in a dual effort to maintain any vestige of control and avoid the guy he didn't like, my boss ordered all the equipment to be delivered to our office... In central Nottinghamshire.... Yet its final destination once configured was to be outside Peterlee in the North East, near Newcastle.

So after around a day of setting up the equipment and (as far as I recall) three days solid compiling time - yes it took that long - the system was ready to go.

However, no-one had kept the boxes, yes it was all out of the box spread on a floor and then hand hauled over to a fire-exit and precariously piled into the back of a Hyundai estate.

Yes, that's how tens of thousands upon thousands of pounds worth of top notch equipment (for 1998) made it's precarious way 120 miles, bouncing and jostling all the way.

At the time I never questioned this, I was a lowly minion, I would of course council against such a move ever again, the installation of the physical equipment should have been done at the remote site, and they definitely should have kept the boxes and packaging in full!

Thursday, 8 March 2018

Sex, Secret or God... Passwords

In the 1990's it was common to have to tell folks not to use "popular" password, like "sex", or "god", believe it or not even "password" and "secret".  Since then times have moved on, folks have become very adept at using other characters in their passwords...

Unfortunately, this is (very seriously) what one of our IT bods here has just found on a machine:


Props to the user for mixing in some numbers, a word and a symbol, however... We can all see the flaw in their storing the password.


(Thanks to our IT Manager for letting me use his picture - it is a lovely left hand, I wonder if he does hand modelling?)

Tuesday, 6 March 2018

KitKat Still Broken....

No, I'm not talking about any build of Android, I mean the actual chocolate bar...

Having my first one since June last year...  Still broken.