I recently added an .origin() function to URI.js:
http://medialize.github.io/URI.js/docs.html#accessors-origin
I’m enjoying working more with and contributing to the open source community 🙂
I recently added an .origin() function to URI.js:
http://medialize.github.io/URI.js/docs.html#accessors-origin
I’m enjoying working more with and contributing to the open source community 🙂
If you’re on Windows and driving node.js through the shell that came with git for windows instead of CMD, it can be surprisingly difficult to invoke shell commands from javascript. The child_process.exec function will always call into CMD.exe instead of bash and if you try to use spawn it will expect you to parse your command into an array as well as escape strings and use backslashes.
It was surprisingly difficult to execute an arbitrary shell command for me so I just wanted to share the small amount of code I ended up writing to achieve it.
Note: this assumes you have Git for Windows or some other shell which you are then running node from.
[code lang=js]
var spawn = require('child_process').spawn;
function run(cmd, env, callback) {
if (!cmd) return callback();
var p = spawn('sh', ['-c', cmd], {
env: env,
stdio: 'inherit'
});
p.on('close', function (code) {
if (code !== 0) return callback(new Error('Command failed', code, cmd));
callback();
});
}
module.exports = {
run: run
};
[/code]
Last weekend I gave a presentation to the Twin Cities Code Camp about Electron called:
How I Learned to Stop Worrying about Cross-Platform and Love Electron
It went pretty well I think, there was more content than time permitted me to go through but for the most part the presentation went along smoothly. I made a kind of interactive demo / tutorial application in Electron that I used to give the presentation, which can be found here:
https://github.com/justinmchase/electron-tutorial
If you want to play around with electron or use the tutorial yourself to give a presentation feel free! Also please feel free to send a PR if there are any improvements you’d like to see.
You may have heard the expression “prefer composition over inheritance“.
But what does it really mean? What’s wrong with inheritance? Even the Wikipedia article on composition over inheritance is using classes and inheritance to explain composition. If composition is so much better than inheritance (at least sometimes), then why do we have to explain composition in terms of inheritance? And why do most modern programming languages and platforms support classes and inheritance syntactically but not composition? What would a programming language even look like if it had syntactic support for composition? Furthermore, what even is the design pattern for composition?
Many of the examples you see from cursory searches online simply show one class containing references to others and that’s about it. This kind of overly simple example seems to acknowledge the problem without really explaining the full solution and it’s completely framed in a context of inheritance. Simply introducing classes containing references to other classes doesn’t give us enough information to establish a pattern and it ends up raising more questions than it answers.
One really good example of composition can be found in Unity3D. Composition in Unity3D is a first class concept that runs very deeply into the built-in game engine that drives everything that Unity does. After studying it for a while I would like to use the patterns of composition found in Unity as a basis for our design pattern and our hypothetical programming language.
In a compositional system you therefore need at least two kinds of Types:
The Object in a compositional system is not the same as an Object in an inheritance based system. An Object in a minimal compositional system has the following attributes:
A Component has the following attributes:
If we were to design a minimal version of this system (in C#) it may look something like this:
| interface IObject | |
| { | |
| string Name { get; } | |
| IObject Parent { get; } | |
| void Add(string name, IComponent component); | |
| void Add(IObject child); | |
| void Remove(IObject child); | |
| IComponent GetComponent(string name); | |
| IEnumerable<IObject> GetChildren(); | |
| void SendMessage(string message, params object[] parameters); | |
| } | |
| interface IComponent | |
| { | |
| IObject Object { get; } | |
| } |
| // A naieve implementation of SendMessage | |
| public void SendMessage(string message, params object[] parameters) | |
| { | |
| var types = parameters.Select(a => a == null ? typeof(object) : a.GetType()).ToArray(); | |
| foreach (var component in this.components.Values) | |
| { | |
| var method = component.GetType().GetMethod(message, types); | |
| if (method != null) | |
| { | |
| // Call methods on the component via reflection | |
| method.Invoke(component, parameters); | |
| } | |
| } | |
| } |
For a more robust example I highly recommend studying Unity3D in detail. However the question I am asking is what would a system like this look like if it was not framed in the context of classes. How would it look if it were to have syntactic support in a language instead of simply implemented as a design pattern in terms of inheritance? I don’t fully know the answer to this question but I have been experimenting with some ideas and would like to have a discussion around them.
| component c1 { | |
| function receive(message: string) { // handles "receive" messages | |
| print(message) | |
| } | |
| } | |
| component c2 { | |
| var running: bool | |
| function run() { // Handles the "run" message | |
| if(!running) | |
| running = true | |
| this->receive("hello world!") // Sends a message to "receive" with an argument | |
| else | |
| this->receive("error!") | |
| } | |
| } | |
| // An object containing the above components | |
| var x = { | |
| c1 = new c1() | |
| c2 = new c2() | |
| } | |
| // send a message to components on x named "run" | |
| x->run() // > hello world! | |
| x->run() // > error! |
| var x = { } // an empty object | |
| var y = { | |
| value: "hi" // any value type, not a function | |
| } | |
| var z = Root { // a named object | |
| } | |
| var j = Root { // An object with children | |
| First { | |
| } | |
| Second { | |
| } | |
| } | |
| for(var child in j) { | |
| print(child.name) // First, Second | |
| } | |
| j.First.foo() // Get child named "First" send it a message named "foo" |
The first thing to realize with this system is that instead of designing Objects up front with static definitions like classes you can only instantiate them and you can only design their hierarchy.
Components are more interesting and do have a static definition. They contain state like a class which means they fulfill the OO principle of encapsulation. They may handle messages of any shape which also means they are polymorphic but they are not inheritable in this system. You may be tempted to add inheritance to components at this point but I believe that this is unnecessary and a mistake. The reason for this is that inheritance is basically just another kind of relationship but it is one that has a much higher degree of coupling between objects. There are a variety of reasons why this form of relationship can cause problems. One is the lack of abstraction and thus isolating the unit for test can become very complex and costly. Instead of using inheritance simply break out shared behavior and state into even more, smaller and more focused components.
A composition system like this is actually similar to the DOM you would find in a browser. The primary differences are that in this case it would not be tied specifically to a single domain (e.g. the domain of laying out and rendering documents) and instead of using an event system it uses message passing. In this way composition allows us to be extremely loosely coupled without requiring extra abstractions.
I can imagine systems where, instead of having a component keyword in the language, you compile entire files into components similar to the way you would compile an entire file into a module in a CommonJS system such as node.js. Your main app would essentially setup your starting objects the rest of your files would be Components. These ideas are very powerful I believe. The game industry has known about them and has been perfecting them for quite some time now, while the rest of the programming community appears to be largely unaware as far as I can tell. I would love to see some experimentation with these ideas in non game domains and find out what the community can come up with.
I’m happy to officially announce that I have accepted a job at a local start-up here in Minneapolis called Evolve.
We’re going to be a very small crew, working closely together to bring Evolve to the next level. I’m extremely excited to take this next step closer to my original passion: video games. I am also very excited to learn more about start-ups and what it takes to put them together and make them successful.
And if you want to play some games head over to my Evolve profile and add me as a friend!
You must be logged in to post a comment.