About Programming

Real Programmers don't comment their code. 
If it was hard to write it should be hard to read.

Always code as if the guy who ends up maintaining your code, 
will be a violent psychopath who knows where you live.

Programming is Our Religion.
Algorithms are Our Bible.

We ./make Our Own God.

There are 10 types of people in this world; 
those who understand binary and those who don't.

Without requirements and design, 
programming is adding bugs to a text file.

I don't need tests.
Tests are for people who write bugs.

Tests won't fail,
if you don't write tests.

Project Manager is a Person who thinks, 
nine women can deliver a baby in One month. 

‘Bankers’ Algorithm – SQL Function

CREATE FUNCTION dbo.BankersRound(@data DECIMAL(25,10), @digits INT)
RETURNS DECIMAL(25,10) AS
BEGIN
	SET @data = @data * POWER(10, @digits)
	RETURN
		CASE WHEN @data = FLOOR(@data) THEN @data
		ELSE
			CASE SIGN(CEILING(@data) - 2 * @data + FLOOR(@data))
				WHEN 1 THEN FLOOR(@data)
				WHEN -1 THEN CEILING(@data)
			ELSE 2 * ROUND(@data / 2, 0) END
		END / POWER(10, @digits) 
END
GO

Usage

SELECT 
     dbo.BankersRound(2.1, 2) AS r1
    ,dbo.BankersRound(2.11, 2) AS r2
    ,dbo.BankersRound(2.555, 2) AS r3
    ,dbo.BankersRound(2.565, 2) AS r4
    ,dbo.BankersRound(3.915, 2) AS r5

Backup all MySQL databases

#! /bin/bash

TIMESTAMP=`date +%Y-%m-%d_%H-%M-%S`
BACKUP_DIR="/backup"
BACKUP_TIMESTAMP_DIR="$BACKUP_DIR/$TIMESTAMP"
MYSQL=/usr/bin/mysql
MYSQL_USER="user"
MYSQL_PASSWORD="pass"
MYSQLDUMP=/usr/bin/mysqldump

mkdir -p "$BACKUP_TIMESTAMP_DIR"

echo "Starting MySQL Backup";
echo `date`;

databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|mysql|information_schema|performance_schema)"`

for db in $databases; do
  echo "Dumping database: $db"
  $MYSQLDUMP --force --opt --skip-comments --compact --user=$MYSQL_USER -p$MYSQL_PASSWORD --single-transaction --databases $db | gzip > "$BACKUP_TIMESTAMP_DIR/$db.sql.gz"
done

tar -zcf "$TIMESTAMP.tar.gz" -C "$BACKUP_TIMESTAMP_DIR" .
rm -rf "$BACKUP_TIMESTAMP_DIR"

find "$BACKUP_DIR" -name "*.tar.gz" -mtime 182 -exec rm -f {} \;

echo "Finished MySQL Backup";
echo `date`;

Swift UIColor extensions

extension UIColor {
    convenience init?(rgb: Int) {
        self.init(
            red:    CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
            green:  CGFloat((rgb & 0xFF00) >> 8) / 255.0,
            blue:   CGFloat(rgb & 0xFF) / 255.0,
            alpha:  1.0)
    }

    convenience init?(rgba: Int) {
        self.init(
            red:    CGFloat((rgba & 0xFF000000) >> 32) / 255.0,
            green:  CGFloat((rgba & 0xFF0000) >> 16) / 255.0,
            blue:   CGFloat((rgba & 0xFF00) >> 8) / 255.0,
            alpha:  CGFloat(rgba & 0xFF) / 255.0)
    }
}

Usage

var color1 = UIColor(rgba: 0x435363FF)
var color2 = UIColor(rgb: 0x435363)