You want to be a programmer?

Fun fact: this post has been stuck in drafting hell since 24 August, 2013!

I am not sure if this is an uptick in the number of people saying this or I am just around more people, but I have been hearing this 6 word phrase (and its variations) a lot more than I used to:

How do I become a programmer?

Before I begin, I must define what a programmer is. In my book, a programmer is one who writes and creates desktop, mobile, or web applications using any recognized programming language, such as Python, JavaScript, C#, C++, etc.

Continue reading

Creating Hangman in PHP and JavaScript

It is a fact: my advisor/primary instructor likes making games for his programming classes projects. Last semester, I developed a Tic-tac-toe game in JavaScript for him. Although it was part of the class, I created a game of Battleship in JavaScript, and he and I talked about how he once made the game using PHP and radio buttons. So when my final project in PHP class came in on 10 March, I was not very surprised to see the suggested projects:

As I knew creating Tic-tac-toe in PHP would be an absolute pain (making it in JavaScript was already painful enough), I decided to tackle Hangman. Within a few hours, I had a random word and corresponding hint selector (using simple a text file for the dictionary) and within two days I had a WIP page layout, not to mention trying to add some humor to development:

I then began working on the game logic, starting on communicating with the PHP backend. Using an OO architecture was a must, as was easily displaying content, as I had a feeling from the very beginning simply echoing to the page would not provide the layout control I needed.

I devised two ways of accomplishing this goal: treat the page as a form, POSTing to the PHP and reloading the page with the updated content, or using AJAX to POST to the PHP and JavaScript to inject the returned content onto the page.

  • As stated, POSTing to the page in a form-like manner required a page reload order to process the guess and display new content. I was not particularly fond of this method, as it would get annoying fast and require keeping track of the game state by storing the values in $_SESSION on object destruction (and subsequently restore them on object creation).
  • At the time, I assumed the second, preferred method of using AJAX for the request and JS for the page content would not require me to keep track of the object’s state, as it would be preserved between requests. This method not only removed the page reload, it also made it much easier for me to display the content (obviously PHP has no knowledge of your DOM structure, whereas JavaScript can and often does.). This method is additionally more “real-world”. Websites commonly POST to PHP scripts for data processing then use JavaScript to display information. However, the benefit with this method was also the primary problem: JavaScript. After all, the class was called “PHP Programming I”, and I did not know if my instructor would accept this method.

That Monday (16 March, to give some bit of a timeline), I presented these ideas to my instructor and asked for permission to use the second method. After a bit of discussion, he agreed with me and allowed me to proceed with my desired plan. With permission granted, that afternoon I whipped up some quick JavaScript to interact with my game logic. One month and many changes later, I finished Hangman with roughly two weeks before due date.

As this was my first big (or even real) project using PHP, I did learn some things in the process.

  • If something can break, it will break.

    Too many times I had things break that should not have broken. For example, once my letter white list, designed to not make players type certain characters (spaces and dashes) broke while I worked on code that did not relate to or interact with it. Yes, random breakage can happen in any code base, but it seemed to happen more frequently with Hangman.

  • PHP is confusing and convoluted as all get out.

    Look, I have read many blog posts on this topic. Some say PHP is bad, others good, and still others say it is getting better. As you know, I am of the opinion PHP is absolutely crazy. I have never had so much trouble figuring out what function to call, what syntax is correct (is it -> or =>?), nor why all the aforementioned random breaking kept occurring. Could I use PHP in further programming? Probably so, but I will make sure to have plenty of candy and breaks to relieve the intense stress and pain when writing PHP. :P

  • Just like with Tic-tac-toe, Hangman is simple to play but hard to convert to code. Though not near as complex as Tic-tac-toe, yet again it is proven that creating a game, even something as simple as Hangman and Tic-tac-toe, is more complex than first imagined. The primary issue for me was determining the current score, e.g., correct or incorrect answer, continue guessing or game over. Things your brain already understands has to be reimplemented in code, and that was probably the most challenging part of this project.

If you are wondering if you can play my Hangman game, the answer is yes and no! Yes, you may play it as I have open sourced it on GitHub under the MIT License! However, you may not play it on a live site like Tic-tac-toe because unfortunately I still lack the means to acquire my own domain and self-host it. :( However, it should run under practically any PHP environment as it uses no libraries or platform specific code. Developed under PHP 5.4 and tested under PHP 5.6. Do note Hangman, like Tic-tac-toe before it, is not super-mobile-optimized, and layout issues are likely to be present. :)

Next possible code project: convert the game to be HTML5 <canvas> powered.

-le717

The craziness that is PHP

For my non-programmer readers, this is probably a new and foreign topic to you., but for my mid-level to experienced web developer readers, this topic is definitely nothing new. In fact, you have probably read this article before and sent it to fellow programmers (that is how I came across it). The flaws, issues, and downright craziness of PHP is so elaborate and elaborately documented, hated, raged about, ironically depicted, debated over, cried over, apologized for, and just about any other describing phrase you can add here, that the language has almost become- if not already is- the laughing-stock of server-side programming.

Continue reading

Writing Good Comments

This post is an excerpt from a beginner-level Python book I am working on. Though it focuses on Python, the core message is language-agnostic. Additionally, I have generalized some of the more Python-specific sections.

Comments are little notes you write alongside your code. They are used to document your code, stating why it works that particular way or what it accomplishes. Comments are also used to detail what type of input the code expects and its type of output.

Continue reading

Double-Clicking on the Desktop

Quick, when you click a link on a web page, do you click it once or twice? How about when tapping an app icon on a touch screen, one taps or two? What about when opening the File menu in an application? Finally, opening Documents via the shortcut on the Desktop of a new Windows installation?

Did you pick up on the difference? Every single one of these common use cases involve single clicking/tapping to achieve the desired action except in the last case, where double-clicking is required to view the directory.

Why is this still the default behavior, even in the very-soon-to-be-released Windows 10?

Continue reading