"Doubt is uncomfortable, certainty is ridiculous." - Voiltaire

Php and Magic Strings

Thought my small programming career I’ve read many pages and heard many quips about the evilness of “magic strings”.   Its undoubtedly true that avoiding magic strings by using the compiler as a tool and strong typing wherever possible does lead to less mistakes, more refactorable code ( especially with ReSharper ), and in my opinion makes for easier, more expressive development.

So it should come to no surprise that while learning Php again I cringe and suffer looking at examples, tutorials, and even Php frameworks that make liberal use of them.  Still, many successful projects, including this blog system are full of them.  Are they so bad?

It makes me question my learned hatred and bigotry against magic strings.  Can you set them once, like using StateBags like Session or Request and just forget about them?  Is jumping through hoops and using Spikey Code ( liberal use of generics to achieve strongly typedness ) heaped with complex expression tree parsing a truly efficient solution to the problem?

The reason my inquisitiveness over magic strings evilness has peaked as much as it has is that I’m working on a project where several bits of code are absolutely horrible messes that nobody can understand that make liberal use of them ( any many other bad practices for that matter ).  The problem is this code performs its intented purpose and modifying it borders on the yagniness line.

Although I struggled with this, my opinion is its necessary to clean the code up, get rid of the black hole of functionality it has become, and do things the “right way”.  My reasons for this opinion has changed though.

I’m becoming more comfortable with “just works”.  Php’s stringyness makes me wonder what other C# “best practices” other languages can’t perform because of the language itself or its just not pracitcal to do so.  If successful applications have been written in other languages, and C#’s best practices aren’t possible in other languages, therefore C#’s best practices are not required to produce successful applications.


 
 
 

Leave a Reply