Swap with no Temp Variable

When I used to TA for the University of Saskatchewan in our intro C/C++ first year classes I used to try and challenge the students sometimes.  When the assignment rolled out on sorting the students were instructed to swap two variables.  As brand new programmers sometimes something as simple as swapping two variables doesn’t leap out at you.  Of course we just teach them that you need a temp variable and mission accomplished.  Well I always offered bonus marks for anyone who could derive the variable swap with no temp variable.   Years pass, no student ever got back to me with the answer ( they obviously didn’t have Google Fu ).  I find myself writing a routine today that required a swap.  I still remembered the trick:

int main()
{

int x = 42;
int y = 51236;

printf(“X before swap = %d\n”, x);

x ^= y;
y ^= x;
x ^= y;

printf(“X after swap = %d\n”, x);

return 0;

}

XOR god mode and fairly easy to remember. X xor Y, Y xor X, X xor Y.  Useful for any POD that is expressed as bytes consistently across architectures.

Needless to say this is a fun trick to blow the minds of long time programmers.

Google’s Coding Style Guide

Regardless of personal preference you will often be forced to adapt to the coding style of someone else.  Generally this will be an enforced style at the company you work for.  Some see this as a minor annoyance.  I personally see this as important and practical.  It helps maintain code readability, this is important for new comers or looking back at your own code.  By making code more readable you also make it more understandable, this makes maintenance even easier.   Decision about style have more impact than arguments like whether braces should be on the same line.

The coding preference which most closely matched my habits turned out to be Google’s C++ Coding Guide.  This made most sense to me as I learned programming starting with C then C++.  In reality I started coding in BASIC on a Commodore 64 but that is a different story.

I find this style guide has a lot of good habits one can derive from it.

Google’s C++ Style Guide

There are some changes to the style I make, here are some important ones:

  • Class members are all prefixed with an underscore
  • Two spaces between functions implementations

Server Backup Management Script

Running the infrastructure for your home, small business or company can be fun but you also take the risks on yourself.  Data loss being the biggest point of failure.  Nothing short of a steady daily incremental backup and monthly full backup.  These backups should be stored offsite.  All important storage drives should be raided with redundancy.  The server(s) needs to be on a UPS (and test the UPS).  Frankly the majority of this is common knowledge.  The big pain comes in actually setting the backup system up.  Reinventing the wheel is always a bad idea so instead of coming up with your own set of backup scripts the first step should be to see what existing software or solution are out there.

This applies only to Linux.  I tested many pieces of software and found most of them clunky at best.  Without naming names I’ll just say every piece of software I tried was lacking when compared to a scripted setup.

Searching the internet I came across this perfect script for which I take no credit for but I want to share.  Backup-Script (mirror: Backup-Script)

All instructions to run the script are contained within the file description.  It is very simple to setup, the instructions even include a good common cron tab line.

I customized the script a bit to suit my needs a bit better.  It is ready to go for a vanilla ispconfig or plesk setup.  It manages both the backup but also the restore.  It does a full backup and then incremental backups.  Restoration is easy, the same script can be used.  All the instructions are in the script header.

So I ran a full backup onto a local hard drive.  Then moved that hard drive to an offsite storage location.  The incremental backups are fairly small in comparison so those auto transfer via rsync upon completion.