(PHP) Code snippet #6

Posted May 26th, 2016 in Snippets by Metod

A while ago I encountered thle following one-liner snippet in a legacy codebase.

Yes, it was a one-liner! Alarming. So, what does it do? Let’s tear it apart and see.

Hint: it displays the name of the first element in the array. But it still iterates through the rest of the array. Which might be ok if the array is of length 1. But still there are some issues left:

  • The complexity.
  • The unreadability.
  • Wrong structure (array instead of string) for the job.

All of the above can be solved by moving the logic somewhere else:

Keep it simple, stupid!