Programming

PHP Formatter

The best part about the holidays is the free time to write fun code. I’ve wanted to write a PHP formatter since I first used Go’s gofmt, so this week it was a pretty easy choice.

The code is available on GitHub: PHP Formatter, and licensed under the MIT License like most of my code.

It uses PHP’s built-in token_get_all function to tokenize the PHP into operators, keywords, strings, HTML, and whitespace, and then steps through these tokens, focusing mostly on rearranging the whitespace. All whitespace (outside of strings and HTML, of course) is stripped except for multiple sequential newlines which are reduced to a single newline. After that, “correct” whitespace is added back into the code.

In order to avoid creating an unruly mess, it then goes through and ensures that no line is longer than 97 characters unless absolutely necessary (i.e. it doesn’t automatically break strings up yet). For consistency sake, it also re-arranges sequential single-line comments to the same line length.

Additionally, PHP Formatter will format inline HTML for indenting, although the HTML formatting is much more permissive, with whitespace for the most part being left untouched. The indenting rule here is that each line that has an opening tag is indented once (regardless of how many tags are opened on that line), and likewise each line with a closing tag is unindented. For this to work, closing tags are rearranged to remain on the same line as the other corresponding open tags were originally. If a closing tag occurs out of order, unclosed tags will be closed to help promote valid markup.

As an added perk, inline JavaScript (through script tags) will be formatted using the JS-Beautify PHP port if it’s available alongside the PHP Formatter. I forked this in order to make it ignore PHP tags inside the JS as though they were just comments, since it’s not actually valid JavaScript, although that’s only necessary if you mix PHP and JS.

All in all, this actually took me a fair bit longer than I thought it would – I was expecting to get a lot more done today and yesterday outside of this – as it turns out that nearly everything about the way we format code is an edge-case, with the word “exception” (or worse “the one exception”) appearing all too often in the comments. Nonetheless, save for a few quirks, it effectively formats code the way I’ve been trying to, and does so with minimal effort on the developer’s part.

 

Why I’m a Programmer

I’ve been thinking a lot lately about why and how I chose to be a programmer. In doing so, I discovered an interesting fact: as of next week, I will have been writing software for more than half of my life. That actually makes figuring out the original decision process somewhat difficult, because honestly I don’t have too many memories that predate being a programmer.

Perhaps this sheer volume of time illustrates rather poignantly how great programming is. When I first started programming, I was 10 (let’s not count writing HTML and copying and pasting Perl scripts around from a few years prior to that, since that only barely counts as programming). Within several months, I had released my first open source project with some 20,000 lines of code for the world to see.

Read More…

Google Doodle Basketball – Automatic 45 Points

Enamored by Google Doodle Basketball, but unable to get more than 42 points, I was intrigued at the upper limit of the game’s score. I knew for sure you could get away another shot past 42 once the balls move off the screen, but I was never able to get there.

The easiest solution was to write 40 lines of JavaScript to win for me. I spent some time playing around with the shot times, and was unable to get more than 45. But at least now I have a script that gets 45 points automatically every single time:

Read More…

Unit Testing in C++

I was chatting with an old friend this morning who was expressing frustration about the fact that C++ lacks a well-documented and easy to use unit testing framework. All he wanted was a way to write annotated tests to confirm that some parsing routines of his did indeed parse the correct values out of various known edge cases as he continued to expand on the parser. He said he had spent an hour or two searching and trying various libraries, but they were all overkill for what he was trying to do.

I suggested that perhaps the reason that there aren’t any good micro unit testing libraries available for C++ is that you could write your own faster than you could find one, and so everybody just did that; actually, my words were closer to “you could probably do it in a half an hour”. He wasn’t convinced, so I went ahead and did it.

Read More…

Introducing SDFRY, The Modern Programming Language

Backstory

Programming languages have stagnated. 1958 was a great year for language development, as was 1995, depending on who you ask. The only problem is 17 years have passed since then and we’re still trying to shoe-horn software development principles into languages not built for them.

It seems that we’re primed for a revolution. Not one rooted in languages picking up steam despite being older than the average Valley CTO, but new languages that make us really think.

Thus, I introduce SDFRY (pronounced “super deep fry”) that strives to optimize writing software for humans, not for computers, named after its fundamental rule: Seriously, Don’t Freaking Repeat Yourself. Designed without buzz-words like “dynamically typed” or “purely functional”, SDFRY fills a gaping void by being a pragmatic every-day language built for developers.

Behold the power of a truly expressive language that eliminates the dry aspects of programming.

Demo

Play Connect Four | View Code

Read More…

How to Handle Time Zones in PHP

Handling time zones is one of those things that developers would rather not think about; the entire system is built around edge cases, making things more difficult than they need to be. Here’s a quick tutorial on how to manage users’ time zones quickly and easily by making PHP do the heavy lifting.

Read More…

Puzzle Book

While waiting at the airport for a delayed flight Friday afternoon, I purchased a puzzle book at the newsstand titled “Ultimate Favorite Variety Puzzles”, claiming to have more than a dozen different types of puzzles, and 260 puzzles in total.

After completing the first couple of puzzles by hand, I came across a new puzzle that they called “Framework” which didn’t quite seem to aimed at humans, or at the very least, not humans looking for intellectual stimulation while waiting for a plane; my patience was already being tried, after all.

Read More…

Solving ITA’s Word Numbers Puzzle

With all the stores closed and cold and damp weather outside, yesterday was the perfect day to work through a programming puzzle. I skimmed down ITA’s puzzle archive and thought Word Numbers seemed interesting. It was retired in September of 2008, so I don’t feel bad publishing my solution publicly. I’ve seen this puzzle before briefly, but had not actually made an attempt at solving it until yesterday.

The gist of the problem is to determine what number is completed at the 51 billionth byte when you concatenate the numbers 1 through 999,999,999 written out in words, such as “onemilliontwohundredthousandthreehundredfortyfive”, sorted alphabetically. What seems like an easy problem at first glance becomes dramatically more difficult with those last two words; sorting the word numbers alphabetically is what makes things interesting. The problem also asks for the sum of the numbers up-to-and-including that number ending at the 51 billionth byte in the sorted order.

With infinite resources, this problem is trivial: write out all billion numbers, run it through sort, and walk through the file until we get to the 51 billion byte mark, tallying up the values along the way. Unfortunately, my infinite supercomputer is stuck in an infinite loop from a few years back and I haven’t been able to make use of it since, so we’re going to have to approach this the old-fashioned way and use our brains. Full solution after the fold.

Read More…

 Scroll to top