Agora Octave: Update #9

Posted On // Leave a Comment

This week

Bundles, continued

I've committed all the progress I've made over the past couple of weeks on bundles. See commit 151. It's still incomplete, but most of the basic functionality is there. Feel free to test it out for yourself.

Optimising dynamic line-number insertion

The dynamic line-number insertion was extraordinarily slow for long snippets. Optimising it was pretty simple, though - I just had to minimise the number of DOM manipulations and to change the $.each() call to a standard for loop (see commit 141). I should really have done this from the beginning, but at the time I thought that the initial method was neater and that performance wouldn't be an issue, which turned out to be very naive.

LESS compilation

I added server-side LESS compilation as an option (commit 143). To trigger it, set compile_less = yes in agora.conf. Now, stylesheets load faster, still work with Javascript turned off, and aren't cached in localStorage when you don't want them to be.

Contributions sidebar for user profiles

There is now a sidebar on user profile pages showing what snippets and bundles that user has created (commit 152).

Fixing registration

There was a bug in registration before relating to an incorrect usage of the django-registration app. It should be fixed now (commit 160).

Other cosmetic changes

Minor cosmetic changes include:
  • Adding word-wrap: break-word to diffs and snippets (so that extremely long words don't break out of the box - commits 158 and 161)
  • Adding overflow-x to the snippet history list in the sidebar, to ensure that it never gets too cramped (commit 145)
  • Adding a top margin to the diff box to ensure that it is aligned with the sidebar (commit 146)
  • Preventing the browser from trying to jump to # when you click the "Highlight code" link (commit 142)

Next week


[Read more]

Agora Octave: Update #11

Posted On // Leave a Comment
This past week has been full of midterms and assignments so this update will be shorter than usual. I made a few small changes (feature additions and bug fixes) to the bundle feature, mostly based on feedback from the mailing list.

This week

The full commit log is available here: http://inversethought.com/hg/hgwebdir.cgi/agora-dellsystem/
  • If an uploaded bundle contains a file named DESCRIPTION, and the bundle's description is empty, and the bundle is indicated to follow Octave packaging formatting standards, then the contents of that file should be used as the description (commit 184, commit 187)
  • If the user has already created a bundle with the specified name, then the form should display a validation error rather than 500'ing (commit 183)
  • Fixed the submit button text in the bundle form (commit 182)

Next week

I'll continue working on bundles and housekeeping changes (see last week's update). I'll also look into documentation integration (suggested by Carlo de Falco via the mailing list).
In other news, I got an email from ESA a few days ago informing me that I have successfully completed SOCIS 2012. Much thanks to my mentor Jordi GutiĆ©rrez Hermoso for all of his help as well as the positive feedback. I will continue to work on Agora Octave after the end of the official coding period (which I just realised is tomorrow, making this my last official blog post :() for as long as it takes to get the project in a usable state, although the frequency of my blog posts may decrease as I get closer to exam period. I'm also interested in eventually getting more involved with Octave as a whole, and hopefully even contributing to Octave itself one day — at the very least, it would be a great opportunity to learn C++. This will probably have to wait until next semester, or at least until I finish my exams (middle/end of December).
[Read more]

Simulating the night sky with CSS

Posted On // Leave a Comment
Have you ever looked up at the sky on a clear, moonless night? When all you can see is an endless field of stars, glittering like an array of celestial diamonds speckled across a cosmic fabric? Did it make you think, "I wonder if I could simulate this in the browser using transparent images and CSS"?
That has never happened to me either, actually. But it was the best segue into this subject that I could think of.
Recently, I built the frontend for a visualisation of Christmas wishes on Twitter. My biggest consideration when thinking of a design was that I wanted to make it somewhat Christmas-themed while avoiding kitschiness as much as possible. I settled on a night sky backdrop, with falling snowflakes simulated using a modified version of this jQuery plugin (okay, a little kitschy, but I'd like to think it embodies a certain classiness as well). The backdrop ended up looking like this:

It looks simple enough, but the technique is actually rather involved. Why? Because the sky is meant to expand to fill the user's browser. Creating a fixed-size image containing randomly-positioned stars would not suffice, because then that image would either have be tiled (which would be very noticeable, ruining the illusion of the endless starfield) or stretched (which could result in very large and pixellated stars for users with larger screens). Neither solution is desirable.
So what's the alternative, if you want to preserve the illusion that these stars go on forever without having to actually generate an endless starfield? My solution was to separate the stars into three different layers: small stars, medium stars, and large stars. Each layer would be a mostly-transparent PNG image, with white dots for stars. And most importantly, the widths and heights of each layer would have to be relatively prime.
In my original design (the one pictured above uses images that are half the size of the original ones, for illustration purposes), the dimensions were:
  • Small stars layer: 1103 wide × 541 high
  • Medium stars layer: 1009 wide × 479 high
  • Larger stars layer: 1051 wide × 524 high
How many pixels of "randomness" do we get before it repeats? Well, all the numbers except 524 are prime, and we can see immediately that 541, 479 and 524 are relatively prime. So the non-repeating area has a width of 1103 × 1009 × 1051 = 1,169,686,277 pixels (the lcm of the individual widths), and a height of 541 × 479 × 524 = 135,788,836. The result is more than hundred million pixels of vertical scroll space and more than a billion pixels of horizontal scroll space before we encounter a pattern of stars that we've seen before. Not bad for less than 14KB of images. (In fact, these numbers are a lot higher than they need to be - anything above 300 or 400 pixels would probably work just as well, with the period being well over a million pixels.)
So that's the basic idea. DesignFestival calls this the cicada principle, which is a catchy if somewhat silly name for something that is a basic property of numbers (there are some really cool examples here). If you want to use this concept in your own project, the HTML structure looks like this:
<div id="stars">
    <div class="large"></div>
    <div class="medium"></div>
    <div class="small"></div>
</div>
Feel free to grab the CSS and the images (stars_large.png, stars_medium.png, stars_small.png), though making your own images is pretty fun as well (I spent a few minutes pasting white dots at random positions in an image document in Inkscape; it was a blast). If you do use this in your own project, I would love to hear about it.
[Read more]

How not to check the validity of an email address

Posted On // Leave a Comment
Pretend, for a moment, that you're a newly hired programmer working on a popular learning management system called Hot4Learning. Your predecessor had been working on adding email functionality to the system, so that any user would be able to send an email to any other user at the school via a web interface. Unfortunately, your predecessor was recently hit by a bus and never managed to complete his magnum opus. Your task is to finish it by adding an email validation feature, to ensure that an email is sent only if the recipient is a valid email address associated with the school.
For example: if Bob is a student at McGill University, then he should be able to send an email to any valid @mail.mcgill.ca or @mcgill.ca email address. So if his friend Jane's school email is jane.smith@mail.mcgill.ca, then Bob should be able to send an email to that address. On the other hand, he should not be able to send an email to jane.smith@gmail.com. Nor should he be able to send an email to thisisnotavalidemail@mail.mcgill.ca.
So your job is to write this feature, assuming you have access to a list of valid emails for the school.
I hope you're thinking, "Please, that's easy. First, I'll create a string by joining the emails in the list with the string '|****|', then I'll pass that to the client by assigning the string to a variable within a <script> tag. Next, in the Javascript, I'll instantiate an Array object and assign it to a variable endearingly named temp, then throw away the aforementioned Array object and set temp to be the array created by splitting the string on '|****|'. Whenever a user enters an email, I'll convert it to lowercase and store it in a variable with the beguiling name curForwardUserName, and I'll create a variable called validUserName which I will set to be the string '0', because why use a boolean when you can use a string? Then, I'll loop through the entirety of temp; for each email address in temp, I'll convert that to lowercase, then check if it's equal to curForwardUserName. If so, I'll set validUserName to the string '1', and break. Finally, if validUserName equals the string '1', I'll consider curForwardUserName to be a valid email address; otherwise, it's invalid.
Actually, no. That's a lie. I really hope that's not what you're thinking. I hope what you actually thought was along the lines of "Well, obviously I'd do the validation on the server side. I can store the valid emails in a data structure that allows O(1) membership testing, then check if the input email is contained in there." Because, well, that's the right way to do it.

This is your brain

In Python, the right way to do it would look something like this:
if input_email in valid_emails_set:
    send_email(input_email, another_param, etc)

This is your brain on drugs

On the other hand, if you're working on a product called Hot4Learning, you probably hate your job and need to consume a staggering amount of drugs in order to keep coming to work. This is what your drug-addled brain might come up with:
Warning: NSFWUYWOH (Not Safe For Work Unless You Work On Hot4Learning)
<script>
// [some other code here]
var userNamesStr = 'a.fakelastname@mail.mcgill.ca|****|another.fakelastname@mail.mcgill.ca
|****|(pretend there are 70,000 more emails here)|****|zamboni.man@mail.mcgill.ca';
var temp = new Array();
temp = userNamesStr.split('|****|');
var validUserName = '0';
// [more code here]
for( i =0; i< temp.length; i++){
if( curForwardUserName == temp[i].toLowerCase() ) {
validUserName = '1';
break;
}
// [some last bits of wisdom before we go]
</script>
UPDATE: Yes, the above code does involve sending all 70,000+ McGill email addresses to the client upon each page load. The source code of the page was over 2.5MB in size. As in, 2.5MB of pure text.
Since your reviewer would likely be as stoned as you, you would get a quick LGTM and the above code would soon find its way into production.

The actual story

Last year, the IT department at McGill University replaced the old LMS (Blackboard) with a new system (which is not actually called Hot4Learning, but the actual moniker is no less trite or gimmicky so I'll spare you). Since the old system was riddled with vulnerabilities and suffered from general all-around shittiness, I was excited to see what the new system would be like. That excitement soon died, just like brain cells do when exposed to copious amounts of drugs.
Upon clicking the "email" tab, I was greeted with 10 seconds of white screen as my browser tried to load the page. "Why on Earth is this taking so long?" I asked myself. "It can't be, like, trying to load every single valid email address at McGill, right? Lol. Haha. Hahaha. Hahahaha. Wait. ohgod"
Words cannot express the horror I felt when I looked at the source of the page and found the code that I have reproduced above. The only time I've been more terrified was when I dreamt that I slept through my cryptography exam and then began to suffocate (but that turned out to just be the blanket).
If you want to take a look at this code in the wild, well, you can't anymore. I reported it as a data leak vulnerability and the vendor actually fixed it within two weeks. However, the code as it appeared then is essentially what you see above, shitty whitespace and all; I just removed some code irrelevant to the functionality at hand and also removed the actual list of emails. Apologies to Zamboni Man if you exist and are a student at McGill.
I would really like to know the combination and quantities of drugs consumed that resulted in this code. Do you know? Can you hook me up? Send me a message on Twitter @dellsystem.

Legal addendum

I hereby claim that the above snippet of code is fair dealing according to section 29 of the Copyright Act of Canada, which allows for usage for the purpose of research, private study, education, parody, satire, criticism or review and news reporting. In fact, this snippet was used for nearly all of these purposes:

Research

I stumbled upon this snippet while doing research for my B.S. thesis, "Not even once: Prolonged drug use and its effect on code quality". It will be the perfect B.S. thesis, complete with B.S. graphs and footnotes in which I beg the reader to take me on as a grad student.

Private study

I saved the source code of the page because I wanted to privately study the correlation between prolonged drug use and code quality.

Education

I would like to educate the public on the deleterious effects of prolonged drug use on code quality.

Parody

I wish I could say that the code above is a parody of the actual source code I encountered. I really do. But I can't.

Satire

The code above was used as part of a scathing satirical indictment of the modern programmer's tragic penchant for drugs. Also, society.

Criticism or review

3/10 would not bang

News reporting

BREAKING: Prolonged drug usage can result in terrible code. CNN had better link back to me when they pick up this story, I could use the ad revenue.
[Read more]