Coding Horrors: PHP’s strpos()

I decided to start a column where I will point my finger at repulsive coding and software engineering cases, which happened to be released to the unsuspecting public too hastily and / or without good thinking.  Someone will say it’s easy to criticize.  I don’t care.  Someone has to tell the plain facts and the sooner we raise our voices, and the louder, the better.  Demand for programmers has spawned numberless hordes of dim-witted script-scribblers, who claim themselves programmers, and who spill out hideous crap to the innocent audience out there.

The choice for my first rant is PHP’s function strpos().  Actually, PHP itself deserves a scorching rant, and maybe I will come to it eventually.  Right now, I will only focus on this specific function.

But what’s wrong with strpos()?  It is a simple routine, available in a basic library for virtually any language out there.  It merely checks the occurrence of a substring in a string.  And so it does.  Except for this very peculiar unique behavior, stated in the official documentation:

Returns the position as an integer. If needle is not found, strpos() will return boolean FALSE.

If you know that strings in PHP are zero-based and the frivolous nature of PHP when it comes to type-safety you will be already on your toes.  It is a realm for unspeakable bugs out there.  The warning in the official documentation, and the vast number of user comments in the official documentation page are a living proof of the disaster.  Please read these notes, really.  They would be hilarious, if the fact that something like this made it to the broad audience weren’t so sad.

I really wonder why the hell a simple function like this should possibly return values in more than one type, and for no apparent reason.  Just tell me the added value, please! 

Sadder, no one ever bothered to fix this ill behavior.  Yeah, I know compatibility will suffer.  And compatibility is important.  But when you have something that broken, hell, deprecate it!  State clearly that there is a replacement, and that this freak should no longer be used!  Instead, PHP guys got themselves busy with implementing half-assed OOP features in the language, and useless exception support.  Maybe I should just say “Go, Open Source!”.

Posted by Nik on December 23rd, 2008 2 Comments

Any Platform, x86, x64… Continued

Further study of the problem on platform target enlightened me that the cause is by using assemblies, compiled with x86 target.  As indicated by Stilgar, these can be any wrappers around 32-bit native code, or COM interops to 32 native COM objects, or any assembly, compiled to target x86.  Examples I know include Managed DirectX assemblies, XNA Framework.  

Similarly, trying to run a x64 assembly on 32-bit Windows will also fail.  Mixing 32-bit and 64-bit code in one application is a “no-no”.

In my case I was referencing a COM object, created in Borland Delphi (obviously native 32-bit code, Delphi still can’t compile 64-bit native).

In short, when you migrate your code to x64, or you want to be sure it will run on 64-bit Windows, please check carefully your dependancies, and pick your platform target accordingly.

Further problems you can have when moving to 64-bit code is the size of datatypes (for example, IntPtr is now 8 bytes instead of 4).  I suggest you read this article at MSDN.

Posted by Nik on December 16th, 2008 No Comments

NET Framework Platform - Any CPU? Well….

Recently I tried to run a WinForms project, developed for NET Framework 2.0, but in Visual Studio 2008, under Vista Ultimate 64-bit.  To my surprise, it failed with “Win32 error - Bad executable format”.

The project was configured for “Any CPU” platform, which comes by default.  However, the EXE would not start on 64-bit Vista, but would run just fine on any 32 XP or Vista around.  Rebuilding the project under Vista 64 did no good.

Finally, I built it explicitly for x86 platform and it ran without a glitch!

I still have to inestigate why this happened, and what native 32-bit components is the project loading, or is it some misconfiguration.

For the time being, just take the warning - don’t put your life in the hands of “Any CPU” option.  If you want compatibility accross both 32 and 64-bit worlds, just state x86 explicitly.

Posted by Nik on December 11th, 2008 1 Comment

Meeting of Plovdiv .NET UG Scheduled

The first self-organized meeting of the Plovdiv NET User Group will be held on Tuesday, 25th November 2008, starting at 18:30.  The meeting will take place at the Faculty of Mathematics and Informatics of the Plovdiv University.

If you have any questions, just contact me.

Looking forward to meet you there!

Posted by Nik on November 17th, 2008 2 Comments

Plovdiv .NET User Group

It’s been quiet for a while here.  Busy with lots of things both at the office and at home, I found no time to post something worthy.

The news is that I will become in charge for organizing the Plovdiv .NET User Group.  The first meeting is due in November, the date will be set in a week or so.  The first meeting with open with a lecture on C# 3.0 by Stilgar.

I will post the date and time of the meeting immediately when set in stone, so stay tuned!

Posted by Nik on October 27th, 2008 1 Comment

Music Will Never Be The Same

Yesterday, 15th September, Richard Wright left us.  Farewell, and thank you for the music.

 

Posted by Nik on September 16th, 2008 2 Comments

Delphi 2009 Tiburon - Why?

CodeGear published recently two video previews about the upcoming Delphi 2009, a.k.a. Tiburon:

http://video.codegear.com/Tiburon/WhatsNewInTheVCL_English/WhatsNewInTheVCL_English.html

http://video.codegear.com/Tiburon/WhatsNewInTheDelphiIDE_English/WhatsNewInTheDelphiIDE.htm

Both videos are hosted by David I.  He sounds really cheerful and enthusiastic about the value Tiburon is going to bring Win32 developers.  I already ranted about the unfortunate attempts of Delphi to emprace NET Framework, and now I was somewhat optimisic that Tiburon is focused on Win32.  First video is about the enhancements in VCL, and the other - about the IDE.

The two videos brought huge disappointment to me. 

Read the rest of this entry »

Posted by Nik on August 17th, 2008 No Comments

Multiple Threads in the UI - Please, Don’t Do This at Home!

I was having a nice chat with a friend of mine - a .NET Framework and Windows Forms programmer, and he said:

“It is not possible to access and modify the contents of a window from a thread other than the main one of the application.  And this is how Windows is designed”.

Well, he was wrong.  His mistake comes obviously from the fact that Windows Forms in .NET Framework does not allow you to do so.  If you try to access a control (i.e. a window in Win32 terminology) from another thread, you get a slap on your hands with a nice exception.

In Win32 you definitely can modify from another thread the contents of a window, created in another one.  But believe me, you should not do it.

Read the rest of this entry »

Posted by Nik on August 5th, 2008 2 Comments

The Look of Windows API Controls

You can tell that a standard push button in Windows XP looks different than a standard push button in WIndows 2000, and such one in Windows Vista.  On top of it, since XP Windows supports visual themes.  The interesting thing is that sometimes one and the same application keeps its Windows 2000 look, but sometimes standard push buttons follow the way of XP, or the Vista, depending on where you run it. 

In this article I explain why.

Read the rest of this entry »

Posted by Nik on July 31st, 2008 1 Comment

A New IDE for Delphi

I just came accross Visual Studio Shell.  This is a very interesting framework for creating development IDEs.

All versions of Borland Delphi starting with 2005, a.k.a 8, feature a really bad IDE.  Really bad.  Slow and buggy.  I already ranted about the fact that Borland decided to catch up with the interface and layout of Visual Studio, than simply evolve the features of their already good IDE.  Since Delphi 7 Borland gave us all a good proof that they cannot create a decent IDE anymore.  Both Visual Studio and Eclipse surpass it by miles.

Well, here is my idea.  Embarcadero, why don’t you just build up your IDE around Visual Studio Shell?

Posted by Nik on July 29th, 2008 1 Comment