Skip to main content

Posts

8 Tips to Improve Your Golf Game

I wasted more time golfing this week than I should have. No, not real golf... code golf . Trying to write small programs in the least number of keystrokes can be fun, challenging, and sometimes even addicting. It's not about writing pretty code or code that's easily-understandable. It's all about cramming as much code as you can into the least number of characters. While some languages golf better than others, you can still write impressively small code in your language of choice if you're familiar enough with the intricacies of its behavior. Here are 8 tips to improve your golf score when golfing with PHP: Use short tags. Using <? instead of <?php and using <?= instead of <?echo will both save you 3 characters. Avoid initializing variables if possible. Uninitialized variables assume the value 0 , "" , or false depending on the context in which they're referenced. $x=0; is 5 characters too much! Know your function aliases. A few...

I Remember...

I remember when Java first came out surfing the Internet on my 28k dial-up connection and hitting a page with a Java applet ; either Netscape would come crashing down or my computer would lock up... I remember schoolmates telling me Java was "the future" because it ran on a virtual machine which meant it was cross platform, and then being ostracized by them when I pointed out that QBasic would be considered cross platform , too... I remember reading Java programming books before I understood OOP and wondering why I had to write a hundred lines of code just to output "Hello World" ... I remember reading Java programming books after I understood OOP and still wondering why I had to write a hundred lines of code just to output "Hello World"... I remember trying to convince myself every time I started Eclipse and watched my P4 run slower than a 486 that I just hadn't given Java a fair shake and I needed to embrace it with an open heart and mind....

End of Support isn't the End of the World

The PHP development team released PHP 5.2.14 last week and with it comes the end of active support for the 5.2 branch. A bit of dissent rippled throughout the community... but is it really a big deal? Contrary to popular belief, downloads from php.net don't come with an expiration date. There is a lot of legacy code running mission-critical applications. These apps work and are stable so the time, effort, and expense required to upgrade them put doing so very low on a companies' priority lists. A few years ago I worked as a System Administrator for a credit union turned bank; the core processing system was written in PL/I and the ATM switching system was written in COBOL . There are probably more applications written in non-OOP PHP 3 code with register globals running atop a Linux 2.4 kernel than any of us want to acknowledge. But version numbers are just mile-markers that reference a snapshot of the project at a given time. The development team is continually improving PHP...

Learning Prolog

I'm not quite sure exactly I was searching for, but somehow I serendipitously stumbled upon the site learnprolognow.org a few months ago. It's the home for an introductory Prolog programming course. Logic programming offers an interesting way to think about your problems; I've been doing so much procedural and object-oriented programming in the past decade that it really took effort to think at a higher level! I found the most interesting features to be definite clause grammars (DCG), and unification. Difference lists are very powerful and Prolog's DCG syntax makes it easy to work with them. Specifying a grammar such as: s(s(NP,VP)) --> np(NP,X,Y,subject), vp(VP,X,Y). np(np(DET,NBAR,PP),X,Y,_) --> det(DET,X), nbar(NBAR,X,Y), pp(PP). np(np(DET,NBAR),X,Y,_) --> det(DET,X), nbar(NBAR,X,Y). np(np(PRO),X,Y,Z) --> pro(PRO,X,Y,Z). vp(vp(V),X,Y) --> v(V,X,Y). vp(vp(V,NP),X,Y) --> v(V,X,Y), np(NP,_,_,object). nbar(nbar(JP),X,3) --> jp(JP,X). pp(pp(PREP,N...

On Unobtrusive JavaScript

Traditionally, unobtrusive JavaScript means the code is kept separate from the page's markup. This separation makes it easier to maintain and reuse the code. However, I view unobtrusive code as more than simple separation. True unobtrusiveness means your JavaScript code will function alongside other code that may be loaded by the browser, and the other code will function alongside yours. Don't pollute the global scope in the execution environment; don't clobber global objects, their properties, or prototypes with your own; and be able to work alongside various frameworks.

Use Discretion when Reviewing Code Metrics

Code metrics are interesting creatures. Some are just raw numbers, such as depth of inheritance or lines of code, while others are a bit more subjective, like a maintainability index. But ultimately they are all meaningless without broader context and an understanding of the code. As a brief example, consider the following C# function which accepts a string and returns the 40-character hexadecimal representation of the string's SHA1 hash . public static String Sha1(String text) { using (SHA1Managed sha1 = new SHA1Managed()) { Byte[] textBytes = Encoding.Unicode.GetBytes(text); Byte[] hashBytes = sha1.ComputeHash(textBytes); return Convert.ToBase64String(hashBytes); } } The function accomplishes one task. Variables are defined closest to their usage. Function calls are clear and do not nest other function calls. Even using ( ) is used so the run-time can automatically dispose of the SHA1Managed resource. Yet a scan using Visual Studio 2010 ...

Signing Assemblies with a Strong Name

Code Analysis/FXCop warned me that my credit card application was not signed with a Strong Name, which would make it more difficult to determine if the assemblies had been tampered with. For more information on Strong Names and why they're a good thing, see this Tech Republic article . you first need a cryptographic key before you can sign an assembly. The key is created using the sn.exe tool provided by the Windows SDK . sn.exe -k sgKey.snk I added my sgKey.snk file to my project in Visual Studio, and then in the Application's properties I went to the Signing tab, checked the "Sign the assembly" box, and specified my key file. I had forgotten that I used a 3rd party library to manage logging the user out after a configurable period of inactivity and their assembly was not signed. You can't sign an assembly unless all of its dependencies are signed as well, which makes sense. You need to replace the unsigned assemblies with signed ones first. If you can compile ...

Code Analysis in Visual Studio

Continuing to play around with Visual Studio 2010 Ultimate , I ran the Code Analysis tool on some code I had written for a customer-- a desktop-based application which securely stores credit card information using hardware identifiers as portions of the encryption key. I started with over 300 warnings and have now worked them down to around 120 warnings or so. Those that remain are Globalization related, which I'm not concerned about since it was a one-off project that is unlikely to be internationalized. FxCop , the utility which Code Analysis is based on, is freely available online .

Visual Studio 2010 Released

If you haven't heard the news already (which I hadn't because I typically don't keep up with such things), Visual Studio 2010 came out earlier this month. I've been playing around with Visual Studio 2010 Ultimate (a 90-day trial version is available) and I must say I'm impressed. Microsoft has finally added common features like block editing and text-zoom, an Extension Manager to extend the IDE à la Eclipse, and support for jQuery. If I were a Fortune 500 company hacking out Windows code all day I could justify a couple of licenses if it really helped my developers efficiently produce a more secure and stable application, but the price tag puts it far out of reach for my needs. I'll stick with either VS2005 or C# Express 2010. I'll probably post a few follow-up entries as I explore more throughout the next 90 days, so be sure to keep an eye out!

Null Pointer Exception

This is why I don't program in Java: import java.util.ArrayList; class Program { class Person { public Person partner; } class Prostitute extends Person { } class Eunuch extends Person { } private Prostitute prostitute; private Eunuch eunuch; public static void main(String[] args) { Program p = new Program(); p.run(); } public void run() { eunuch.partner = prostitute; } } > Exception in thread "main" java.lang.NullPointerException But if you do, you should check out the nifty online JXXX Compiler Service .