February 3, 2012
Fixing "Open With" on Windows 7
So I've had this issue happen twice, and this time it really irked me. You go to change a filetype to open with a specific program, browse for it, and when you find it you think it will show up on this list but it doesn't. Why? Well... Windows stores every application you add to the "open with" window in the Registry. That in itself is a good thing, however there is a downside. Windows will not update the programs location if you were to delete or move it. Sounds dumb, well yes, it is; However, you can fix it!
Open up regedit1:
Start > regedit
Navigate to:
Computer -> HKEY_CLASSES_ROOT -> Applications
Find the program you have been trying to set as the default, the location will probably be different than where it currently is. Now you can change it to it's correct location and quickly get back to having control over Windows.
Enjoy (:
Notes
January 10, 2012
Fix cancelled / un-finished downloads
Ever had a download on Firefox or Chrome cancel due to crash or some other random issue?
Well, it's an easy fix.
Navigate to the folder the file was downloaded in. Find the file that didn't finish and remove the un-finished extension, if any exist, until it matches the URL of the file you were downloading. Chrome is crdownload, Firefox is part.
Now open Terminal1 or Command Prompt2 in that folder.
Navigation through Terminal or Command Prompt
This applies if you are not using the open * here
*Unix:
$ cd ~/Downloads
Windows:
> cd "c:/full/path/to/Downloads"
Continuing the Download
Now to continue the download we are going to use wget3, type this into Terminal or Command Prompt:
$ wget --continue URL_HERE
Notes
cd in term, or download a 3rd-party application / script to add the context to right-click menu.open command here option, or start / windows icon > run > type: cmd > press enter > follow Navigation Through Command Prompt section.
January 8, 2012
PHP Coding Conventions
Conventions in coding are like opinions. Everyone has one; Preferences. That's all they are, however these are what I've found to be the most legible and easiest to utilize, not to mention you don't have to drastically change them when you learn a new language. When you code something open source... people are going to look at it, so make it look good or at least navigational.
Here are a few quick surefire ways to make your code readable.
1. Don't put your brackets on separate lines.
<?php
if($annoyed)
{
$brackets_on_seperate_line = true;
}
This causes a whole slew of inconsistencies in libraries, and projects. One person does it, another doesn't, you are constant battling. So when should you put a brace on a new line? In PHP, this really doesn't matter, but it utilizes important whitespace and shows that you indent incorrectly and to cure the issue you use braces as your guideline, which is wrong. Most IDE's will show you what block you are currently in, underlining the braces or boldening them.
So in my eyes, same line is the best approach as when you get into other languages you will notice, almost all of them require braces on the same line, especially scripting languages. I think this is something all PHP developers do in the beginning because you are constantly learning and trying to find your... style; It's not just about your style, it's about how other people will receive it as well as when it comes time to learn a new language, you will try to use your style, and it will not work out as planned and you have to learn a new one.
2. Only use underscores in certain cases.
<?php
$variables_should_not_have_them = true;
define('GLOBALS_SHOULD', true);
class Also {
const CAN_HAZ = true;
}
Underscores should only be kept to things like global variables, enumerated lists, constants, and cookies / sessions. Why? Because, in variables not only do they take up space stretching out the time it takes to repeatedly type a variable, or even function for that matter. It's not illegal to mix and match your case as long as you have a strict guideline like the one below:
- Globals, Constants, and Sessions / Cookies use
underscore_casing - Functions, Variables, and local variables use
camelCase - Classes, Modules, and Namespaces use
UpperCamelCase
It not only saves you time, it prevents things like the disorderly PHP function list from happening in your library.
3. Wrapping long lines, The Smart Way.
<?php
$variable = new LongClassName('Some Variable', new AnotherLongClassName('why'), array('it','is','fun'));
Having long lines sometimes can't be avoided, however stuff like that can. First, think about your class naming structure... is it simplified and readable? If it is, then look at the line, can you break it down?
If you can, go ahead and break it down, separate lines for variables and if statements won't hurt you, in-fact they can increase readability.
<?php
$variable = new LongCN(
'Some Variable',
new AnotherLCN('why'),
array('it', 'is', 'fun')
);
4. Utilize Brackets Effectively.
<?php
if($annoyed) {
$terribleSyntax = 50/50;
}
You don't always need the brackets when doing single line if-else or for-if-else or for / if statements. It can increase readability and boost coding time by letting you type less. That is the goal here, faster code time means more time to focus on something else, even if you don't have that issue, certainly can't hurt to increase it.
There are some cases where you will need them on single lines but here are a few examples of when you can use it:
<?php
// Simplified
if(!$annoyed)
$terribleSyntax = false;
// if-else no-brackets -- I try to avoid this
if($terribleSyntax)
$annoyed = true;
else
$annoyed = false;
// Continued Above -- and instead if possible use conditionals
$annoyed = ($terribleSyntax) ? true : false;
// If with else bracket block
if($terribleSyntax)
$annoyed = true;
else {
$annoyed = false;
$happy = true;
}
The foreach works the same way essentially. If you have more than one line on the block, you need brackets.
Hopefully these help you out and your teammates if you are that guy they hate when it comes to coding with you ;P Let's hope not!
Updated To lessen the condescending tone because sarcasm doesn't convey well in text.
January 6, 2012
Simple Javascript Classes
Usually creating JavaScript OOP calls for you to modify __proto__ or add tons of obscure methods to create simple parent / child objects. After reading tons of methods that other people use, and came up with my own method.
A single function. Class():
var Class = function (properties) {
function clone (source, target) {
Object.getOwnPropertyNames(source).forEach(function(prop) {
Object.defineProperty(
target, prop, Object.getOwnPropertyDescriptor(source, prop)
);
});
return target;
};
this.inject = function (properties) {
clone(properties, this);
};
this.extend = function (properties) {
var parent = this.prototype || Class;
var proto = Object.create(parent);
clone(properties, proto);
var body = proto.Constructor;
if (!(body instanceof Function))
throw new Error("Constructor missing in a class!");
body.prototype = proto;
body.parent = parent;
body.extend = this.extend;
body.inject = this.inject;
return body;
};
return this.extend(properties);
};
That's it, that will allow you to create classes using simple objects, there is only one required function and that is the Constructor or initialization function. By using this you can easily manipulate objects, extend and inject without ever looking at prototype again.
Here is an example of an Entity (Parent) and a Monster (Child):
Entity = Class({
attacks: 0,
Constructor: function(name, health) {
this.name = name;
this.health = health || 0;
},
isAlive: function() {
return this.health > 0;
},
setHealth: function(value) {
this.health = value < 0 ? 0 : value;
}
});
Monster = Entity.extend({
attack: function(what) {
var dmg = (Math.random() * 110) + 1 | 0, leftovers = what.health-dmg;
if(leftovers < 0)
what.setHealth(0);
else
what.setHealth(leftovers);
this.attacks++;
console.log(this.name + " attacks " + what.name + " for " + dmg + " dmg.");
}
});
See it on jsfiddle!