Posts

Showing posts from August, 2013

Obfuscation for its own sake

Wow... I just discovered Twitter Widgets (I needed them for a project I'm working on). When you create them, you get a code fragment to paste into your site to include the widget. Here's mine: <a class="twitter-timeline" href="https://twitter.com/mdpopescu" data-widget-id="...">Tweets by @mdpopescu</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script> Look at that piece of Javascript. Marvel at its obscurity. Find out it can be replaced by: <script id="twitter-wjs" src="//platform.twitter.com/widgets.js"></script> I mean, seriously? Minor nitpick: yes, that javascript code will not re-create the

Duck typing with EF entities

Ok, this might be old news for you, but for me it's a neat trick that I just figured out. Any time I work on a personal project involving a database, I prefer to use Entity Framework Code First (EF from now). It just lets me do what I want (write code) instead of stuff I hate (dragging things around in a badly implemented "graphical editor"). One of the many things I absolutely love about it is that I can use POCOs - Plain Old C# Objects - as database entities. I don't have to inherit specific classes, not even specific interfaces. I do have a few rules when writing these classes, though: All entities have a primary key called Id . The Id is normally an int (it will be automatically converted to an auto-incrementing field); in some special cases (e.g., for security purposes) the Id might be a Guid instead. Any foreign key gets written as a <name>Id field for the foreign key itself and, most of the time, a virtual <name> (s) property for the actual

The visitors

The world's observatories spotted them on August 27, 2014. They came from somewhere in the Orion constellation and took their sweet time doing it: it was more than three months before they became visible enough that the amateur astronomers were beginning to spot them. The White House made an official announcement on December 1st. It didn't say that much, but the news killed all other stories. The Pentagon saw the biggest increase in military spending since 9/11, with a tiny part of that fortune going to NASA and the Pasadena and Houston observatories. The New Year came to a rather anxious world; most of the panic had subsided by then, with barely a couple of street preachers warning everyone of the impending apocalypse. Gun sales were at an all-time high, not so much for protection against the aliens (nobody expected a few shotguns to help there) as against possible riots. On March 1st, two hundred and thirty nine ships stopped at a distance of 2.5 million miles - about t

A generic implementation of a finite state machine

A finite state machine (FSM for short) can be informally described as a network of states, like "the elevator door is open" and the connections between them. There is one state that is the start state and there might be one or more stop states (or none). A FSM has a current state, which can change as a reaction to an event. Let me clarify things with an example: a very simple interface for a music player. This music player has two states: it can play music, or it can be silent. The starting state is the idle one: +------+ +---------+ | idle | | playing | +------+ +---------+ Put more formally, the network of states is a directed graph of nodes (aka states). The connections between the nodes are called edges, and they have a preferred direction - there will be FSMs where you can get from state A to state B, but not back to state A. (If you want to be able to get back to state A, simply add another directed edge from B to A.) How does the current stat

On the impossibility of time travel

I've long believed that time travel is impossible. One of the many reasons I have for it is: it violates the conservation of mass/energy (something disappears from the universe at moment A and something appears in the universe at moment B). Recently, I've had to reconsider this problem. I started with an observation: the conservation of mass/energy is not violated when something moves. Formalizing this statement, an object changing its X, Y and/or Z coordinates does not violate the conservation of mass/energy. Now, if I consider that the universe is actually a 4-coordinate system, it can be inferred by analogy that an object changing its X, Y, Z and/or T coordinates is not violating conservation either. (The fact that we don't currently know any such objects is irrelevant; there was a time when we didn't have airplanes or cars.) Of course, this isn't proof , but I wasn't proving a theorem in the first case; all I had was a thought experiment, an apparent c

Two algorithms

Two small code fragments for problems I found interesting. First, I had a list of criteria that were being used to decide which files in a file list to mark as selected. Some of the criteria were simple, like "none" or "all", but some of them were more complex, like "modified in the last 15 minutes". Since I strongly disliked the idea of a series of if s (or the equivalent switch statement), I came up with the following: var filters = new Dictionary<string, Predicate<FileObject>> { {"None", file => false}, {"All (*.*)", file => true}, {"Invert Selection", file => !file.Selected}, {"Modified Last 15min", file => file.LastWriteTime.AddMinutes(15) >= DateTime.Now}, {"Modified Today", file => file.LastWriteTime.Date == DateTime.Now.Date}, {"*.dll (not Telerik)", file => !file.Name.StartsWith("Telerik"