Daily WTF: PHP “Annotations”

by jmorris 3. November 2012 21:08

Annotations or Attributes (Java vs. .NET/C#) are a means of decorating classes, methods and properties with additional metadata or declarative information. The annotations/attributes can then be queried at runtime via reflection and methods associated with them can be invoked.

Incredibly powerful and useful, they are quite common in various frameworks for tasks associate with say validating data associated with a class or property or for mapping properties on an entity to column names in a table in an RDBMS. There are many, many other uses as well.

Here is an example in C#: https://github.com/jeffrymorris/attributes-example

image

Note that in both Java and C#, annotations/attributes are a first class language construct. This is useful for many reasons, including improved readability and comprehension, they are type-safe, you can attach a debugger and step into them, etc.

Today I learned that PHP also has a form of annotations…well, sort of! It seems that a couple of PHP frameworks (Symphony 2 and Doctrine 2) have “implemented” them not as a language construct but as a hack via comments:

image

Folks, those aren’t comments…that is code that will get executed! Yuck, this is wrong in so many ways…especially since there is an RFC for adding annotations to PHP in the works: https://wiki.php.net/rfc/annotations.

Just because you can, doesn’t mean you should!

Tags:

rant

Obama’s Website Reminds me of Couchbase’s and other Notes

by jmorris 15. December 2011 13:32

This is what inspires me to blog again after months of inactivity:

image image

Is it just me or don’t they, kind of look alike?

That observation aside, the Obama website is kind of creepy. It has an several forms for soliciting contact information from the sheeple:

http://my.barackobama.com/page/signup/who-would-you-bring

And this one which collects more data, allows you to make a donation and enter a Republican you knows contact info and they will send them a message:

“This holiday season, we're giving you a chance to have a little fun at the expense of a Republican in your life by letting them know they inspired you to make a donation to the Obama campaign.

Simply enter their name and email address below. Then, we'll send them a message letting them know they inspired you to donate. (Don't worry—we won't hold on to any of their information.)”

Immature and trite, if not weird action from the POTUS…meh politics.

Another interesting “feature” of the website is this splash page that comes up the first time you hit the site (try clearing cookies):

image

Note that it attempts to immediately get your email and zipcode? More point-of-contact and demographic information for the the big “B’s” big data machine. Notice how small the “continue to the website…” part is? It’s even in a muted color (in comparison to the “SHOP NOW” button).

Inspiration, wherever it may find me…

Tags:

rant

The Mysterious +<>c__DisplayClass1

by jmorris 9. July 2011 01:23

While unit testing a a VirtualPathProvider today, I came upon an interesting exception:

System.Runtime.Serialization.SerializationException : Type 'Foo.Web.Core.UnitTests.Plugins.Modules.PluginRegistrarTests+<>c__DisplayClass1' in assembly 'Foo.Web.Core.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

I was stumped by this…what, where and who  is +<>c__DisplayClass1'? Granted my scenario was somewhat complex in that I am testing in a by using a fake AppDomain that mimics the ASP.NET HostingEnvironment ala this post.

When I saw that error, I immediatly thought the problem was that I was missing the Serializable attribute, since the exception explicitly states: “[type] is not marked as serializable.” I added that attribute to class that I was loading into the faux AppDomain for the same result:

image

WTF? I was stumped! I googled around a bit and got side tracked by some discussions of MarshalByRefObject and finally stumbled upon something on Ode2Code. I wasn’t quite sure of what the problem was until I read some of comments. Most notable this one. A quick check of the IL with ILSpy confirmed my suspicions:

image

Fix was easy: simply move the declaration of the FakeHttpApplication class to within the scope of the delegate itself. Here is how I had it defined:

image

And after I moved into the scope of the delegate:

image

So, what was the problem? Basically it comes down to the scoping of Anonymous methods and how the compiler generates code to support them. Anonymous methods are simply compiler generated types. The generated type c__DisplayClass1 is not marked as serializable, so it fails when passed into the scope of the Anonymous delegate call…which is executing in another AppDomain (that does not contain the Anonymous type definition).

Tags: ,

Unit Testing

What Am I Missing Here?

by jmorris 9. March 2011 07:16

So I am trying to purchase VS2010 for home use, side projects, etc…I google VS2010 price and click on the first “relevant” result: http://www.microsoft.com/visualstudio/en-us/products/2010-editions/professional

And click the “BUY NOW” link which opens:

image

Great $549 for VS2010, way to expensive but within the budget. So click on the link to purchase and this is what I am presented:

image

$799.00? WTF? But wait, full version $549.00 (my only option, really). And what do I see in the cart?

image

Fantastic….

Tags:

rant

The Dilemma's of a Developer

by jmorris 12. January 2011 01:43

I ran across this today while refactoring/reviewing some code:

image

What in the heck I am I supposed to do with this? Delete it? Refactor it? Quickly close the file and pretend I never saw it? Mind you that it’s in code that’s in production and get’s run several times a day…and the code works as expected.

I chose to refactor it:

image

Unit tests pass:

image

Hold your breath!

Tags: , , , ,

Refactoring

Design Fail

by jmorris 28. October 2010 13:59

While I am not much of a front end developer or much less a designer,  the design elements of some sites are so poor, that even I shake my head in disbelief. While doing some research for a future post on refactoring and method groups, I came across this site: www.thedesignmethodgroup.com

image 

Note that the site is all flash and you navigate with that teeny, tiny little menu thing!  As you click the on the menu item squares the flash app moves another layer into focus which contains quotes from various famous people. The problem is, WTF does the company even do? It took me awhile navigating through this maze to find out they are a marketing company…apparently the focus on “creating customers”. Maybe the should focus on “communicating with customers” first.

BTW, here is the “Clients Page”:

image

Is it any wonder?

Tags: , ,

rant

What is the ObsoleteAttribute Anyway?

by jmorris 22. June 2010 17:31

From time to time during the lifecycle of any API, certain methods or classes simple become obsolete; meaning "No longer useful" or "No longer relevant." Often times it is a warning to users of the API that the lifetime of the said method is "finite" and will or may be removed in the future.  It is also may be an indication that another type or method has been created which supersedes the functionality of the decorated subject.

The FCL even provides a special attribute for flagging methods and types as obsolete: System.ObsoleteAttribute. Here is the MSDN description of the attribute:

“Marks the program elements that are no longer in use. Marking an element as obsolete informs the users that the element will be removed in future versions of the product.


Typically it is added when a method or class must be changed in a way that backward compatibility may not be completely possible. After all, in general most developers try as hard as possible to maintain backwards compatibility; or at least try as hard as their level of comprehension of the API and its clients. Occasionally this isn’t possible, so the attribute is used to flag a clear direction to the developer: “DO NOT USE THIS CLASS, METHOD, ETC…USE CLASS/METHOD/TYPE X, Y, OR Z INSTEAD!!!”

For example, the following method has been flagged with the ObsoleteAttribute and a clear, description directive has been made to all users of the method:


For those that ignore that message and use the method in there code, the compiler will even issue a warning:

So as a plea to all developers, when you encounter the ObsoleteAttribute, please, please respect it and don't use methods or types in your code that are flagged with it Smile

Tags: ,

rant

Jeff Morris

Tag cloud

Month List

Page List