linkedin github twitter

How to Overcomplicate Simple Functions to Ensure Job Security

Start with an easy to read function

Make sure to select only the most mundane functions to refactor into obscurity. Otherwise it might look like you were actually trying.

//output is a random string of a given length
    function makeRandomString(length){
    	//define all possible characters
    	var chars  = 'abcdefghijklmnopqrstuvwxyz0123456789';
    	var output = '';

    	for (var i = 0; i < length; i++) {
    		//loop through and pick random characters and add them to the output
    		output += chars.charAt(Math.floor(Math.random() * chars.length));
    	}

    	return output;
    }

Remove comments, put all your variables on the same line and shorten the function name

Remember, this function is so simple it doesnt even need comments

function rStr(len){
    	var c = 'abcdefghijklmnopqrstuvwxyz0123456789', o = '';

    	for (var i = 0; i < len; i++) {
    		o += c.charAt(Math.floor(Math.random() * c.length));
    	}

    	return o;
    }

Brackets Schmackets

Brackets are for the unemployed

function rStr(len){
    	var c = 'abcdefghijklmnopqrstuvwxyz0123456789', o = '';
    	for (var i = 0; i < len; i++) o += c.charAt(Math.floor(Math.random() * c.length));
    	return o;
    }

Use Bitwise operators that almost nobody uses in the real world

It should looke like special character alphabet soup.

function rStr(len){
    	var c = 'abcdefghijklmnopqrstuvwxyz0123456789', o = '';
    	for (var i=0; i < len; i++) o += c.charAt(~~(Math.random()*c.length));
    	return o;
    }

Is that the best you can do?

5 lines is too many, hell if you can write it in 0 lines of code - go for it.

let rStr = l => [...Array(l)].map(i=>(~~(Math.random()*36)).toString(36)).join('')