About TheJoey.Net

TheJoey.Net is the weblog of Joe Casabona, a web developer who attends the University of Scranton, now for Graduate Studies. He is real bad at writing these about pages and hates writing in the 3rd person...more

**The layout is new and there might be some bugs. If you see any, please email me at Joe@Casabona.org

Archive for the 'Code' Category

Web Design tutorial blog NETTUTS published an article I wrote about Building a Facebook Appication Check it out!

New post here Friday.


One thing that was really driven home during my first year of grad school was the importance of reusable code. Not to say that it wasn’t taught to me as an undergrad, because it was; this year it just seemed to hit harder. As a freelancer in a niche market (small businesses, facebook apps), I tend to generate a lot of similar code. Just recently (within the last few months), I’ve started to generalize that code into reusable PHP objects, and it really helps.

I point out freelancers in the subject of this post because I feel, at least starting out, it’s not something freelance programmers (or designers for that matter) think to do. I know my concerns were more getting those jobs so I could code, not the process of coding. But whether it be a list of objects you can use on each site, or a HTML/CSS template with variables for the title, key words, headers, etc., you can save a lot of time in the long run by taking the extra time to abstract out the details and come up with something general you can use over a number of different sites. I, for example, have general code for: XHTML/CSS templates (NOT the design, just the general tags that should be applied to every site), [basic] contact forms, [basic] google maps, database objects, image uploading objects and XML parsing objects. Right now I am taking the time to look at other code I’ve done to see how I can generalize it.

This doesn’t only save time in writing the code either, but in testing. If you have core code that you know works, you don’t need to test for the general cases, or debug that code; just what you’ve added to customize it for that site or app. It’s stuff like this that will increase your productivity and your profits in the long run.

So generalizing code helps- and not just with time, but productivity and profits. And just about anything can be generalized to some extent. So the next time you code, take an extra moment to think how you can abstract away the details of that project (the variables if you will), and how much of what you are doing is reusable.


As I said in the last two posts, Google I/O was truly inspiring. It got me to thinking about how I program, what languages I chose to do it in, and what I can do to become a better software developer. Two things I will be doing are getting more proficient with Java (especially for my Master’s Thesis) and learning Python. I’ve begun going through and learning some of the syntax and nuances of Python and found I follow a pretty similar (and effective) pattern for each language I learn. (It’s worth noting that in the following, I assume the reader has at the very least read about programming and has some language to learn in mind.)

First, pick up a book. I usually go with the for the Absolute Beginner series. It taught me PHP/MySQL and did it in a very effective, modular way. The authors don’t assume you know how to program, but the books aren’t so slow that if you have, you’re bored. They also provide the tools you need to set up your environment, resources, and all of the book’s sample code on a CD. Each chapter takes you through 2 or 3 smaller programs, and builds a full one at the end. In my opinion, they have the perfect combo of code and text.

Then, build one program. Just one- and continue to expand it as you learn new syntax. I build a “Guess the number” game. It’s a simple problem with an easy solution that you can expand on. My plan of action for this program goes like this, following along with the book I’m using.

  • Basic print statement. Something like "I'm thinking of a number..."
  • Variable assignment. Hard code the first number you want the user to guess. That way you know the right number and can check your logic for the next step. x= 3.
  • Get input from the user/store it in a variable. guess= get_input("What's your guess").
  • If statements. If guess == x: print something, else: print something else.
  • While loop. Do something like prompt to user to see if they want to guess again, accepting yes or no. while keepGoing != no: doing it again

These five basic steps show you the important basics- input, output, and flow of control. The next few steps could be language dependent, but you can also take some liberties and get creative.

  • Change x to a random value. If the user wants to guess again, the number will now change. x= rand(0, 10).
  • Allow the user to enter five guesses at a time. This will do a couple of things for you- make use of an array, and use a for loop.
while i <= 5:
   guesses[i]= get_input("What's one of your guesses?")
   i= i+1

for guess as guesses:
   if guess == x: print something,
   else: print something else
  • Write a function/method to check if the guess is correct. It might be a trivial thing to do here, but you’re really just doing it to learn the syntax.
  • Create a ‘Guesser’ class. This of course assumes you’re using an object oriented language. Write one class with functions to prompt the user, store the input and make the guess, etc.
  • Finally, try storing the results in a file. Keep a counter for how many times the user, plays, and for each time, store each guess, the actual answer, and if they got it right or not.

With these steps, you’re learning a new control structure, how to use arrays, write to files, and most importantly how to modularize your code through functions and classes. I think with that, you will know enough basic stuff about the language to go off and write some other less trivial programs.

After that, it’s really up to you to dig deeper into the particular language you are learning to really make use of its power. The Absolute Beginner books take you beyond the above exercises and do some language specific things. With PHP, it was creating a web app and connecting to a MySQL database. With Python, it looks like you build a basic game. With Java, you should probably explore Generics or threads. Either way, once you learn the basic syntax, it’s time to harness the true power of the language.

My last piece of advice is to pick up the O’Reilly cookbook for that particular language. They’re usually for a more advanced user of the language, but are extremely helpful in doing specific things. The PHP and Java Cookbooks have helped me immensely.


Over at LifeHacker, they are hosting a spirited discussion on what the programming language to learn first is. I think this is a pretty interesting discussion to have and want to comment on what the others are saying, as well as justify my stance. First of all, I did weigh in, saying:

I’d say if you’re not going to be a serious applications programmer, learn some weak-typed language like PHP or Javascript first to get your feet wet with program logic without having to worry about whether your types (the difference between say a number and a word) are correct. This will help you with the control structures(if statements, loops), how the logic works and how to solve some basic problems. Then you can move on to a more advanced language and learn the different types, objects, etc.

Many of the others who commented said that Javascript is not a programming language, it’s a scripting language. Is there a huge difference? Maybe, but not to someone first learning. They will not be doing things a programming language calls for- they will be learning control structures and how a program works, what kind of logic goes into writing a program, etc. To be honest, I first learned Java when I was a freshman in college, but I didn’t really understand programming until I picked up PHP over winter break of that academic year. So is there a difference? Sure. Is it a huge deal to someone starting out? Not at all.

Then we’ve got those who say, “Learn C first, it’s the best” or someone even said, “Learn Assembly.” I know how to program and Assembly is hard. You’re not writing code in something that looks remotely like English and you need an understand of how data is represented on a machine. It sort-of applies for C too, as C is a low level language. Don’t get me wrong, C is the best language I’ve coded in- it’s fast and you can do really powerful stuff. But I think pointers alone is enough to turn someone off to C and programming in general.

Finally, we’ve got those who say learning an Object Oriented (OO) language is the way to go. I can see that. I feel there is enough abstraction there that the learner might be ok. They shouldn’t dive right into object, however, because objects are pretty difficult to visualize if you’re just starting out.

I still say my approach is at least a good one. Learning a simpler language that doesn’t put restrictions on data types will allow the learner to focus on program logic more than data representation. They will begin how to think like a programmer and can move up from there. PHP isn’t a bad place to start because it acts like a simple language (weak typed, syntax is pretty easy to understand), but you can do some powerful things like create objects.

Teaching yourself to program is a pretty daunting task to begin with. However, if you pick up a good book and start with a simple language, you should be fine.

PS- HTML is not a programming language. It’s a Markup Language. You will not learn anything about programming.


Nov 10

Reuseability

Code

A few months ago I developed a Scrubs Quotes application for Facebook. This is a simple app that displays a random quote from the show on your profile. One of the first lessons/ideas being taught to me in the Software Engineering Masters program here at the University of Scranton is that when you develop software, you should make it as abstract and general as possible. Reusable code is key. With that in mind, I set out to make my quotes app a little more reusable.

As far as the scope of the project goes, it wasn’t that daunting of a task. When programming in PHP, I usually make everything a function anyway because of the ease of use. But there were some things I needed to clean up. The first thing I did was create a config file with all of the application specific information- database connection info, application title, links to include in the app, etc. This makes changing the app as easy as changing a single file. Other files that I felt could be edited I placed in separate files as well. This includes the style sheet, which up until that point was included in the header file.

Once that was done, there was not a heck of a lot more to do. In an upcoming version of this software, I want to include an SQL file to generate the databases, and an easy switch to make the app sightly different if there is a single person being quoted, or multiple people.

My first use of this reusable version of the app manifests itself in a Demetri Martin Quotes application. The set up was really easy and I was able to have the app up and running in just minutes. This is a pretty exciting little project for me because it’s a great practice in system design and software development. As time goes on I hope to grow this engine and possibly make releases of the software for others to use. I would like to learn how to make this easily updateable as far as adding features go and see how scalable it really is.