Language Idioms, Yearnings For A Simpler Time Posted on April 12th, 2016
I can distinctly remember my first wtf
moment in programming. I was taking my first programming class during my freshman year at college. Even before beginning the class I'd already figured out how to make a webpage and had copied my algebra teacher's screen to make a program that could calculate parabola intersections. However, anything that was more complex appeared to be rocket science. I was sitting in my dorm room doing some homework when I came across i++
. I was so confused -- what could i++
possibly do? Eventually, I figured out that attaching ++
to i
incremented the value of i
by 1. While this syntax may save some CPU cycles, it's certainly not as explicit as i = i + 1
. On the other hand it was easy to remember and paired nicely with i--
. I filed that away and was happy to keep learning.
I am not one to shy away from abbreviated syntaxes. Fast forward 7 years to my next wtf
, which ended up being slightly different. Rather than wondering what code was doing, my boss was wondering what I was thinking. I had done something akin to $this->log && $this->log->info('Something happened')
. While this is a fairly common short hand in Javascript, it's fairly uncommon in the php community. I was overruled by the engineering team and despondently wrapped the sanity check with an if
statement.
After graduating college, I found work as a full stack php engineer. I loved php; I often described it as "C with training wheels". It had all of the same functions with none of the fun segfaults. At that time php was a simple language and was easy to pick up. In fact, that was one of language's biggest criticisms; too many people who didn't know what they were doing were using php to build interactive websites. It was the democratization of programming. In recent years, the language and the community have started evolving. Recently php passed the RFC: Null Coalescing Assignment Operator. If I was a voting member, I would have voted yes. However, I think that it is important to ask if the language is getting away from its roots as a simple language to learn. If so, is this a good thing?
5.4 ushered in the ['short', 'array', 'syntax']
and function()['dereferencing']
. Class member access was (new \DateTime())->now()
available. 5.5 finally
brought yield
to php. 5.6 brought argument packing(add(1, ...$addends)
), variadic functions(function add(...$args)
), and exponentials (**
). After that, the flood gates really opened. php 7 brought scalar type hints (function add(int $addend)
), return type declarations (function add(int $addened) : int
), null coalescing ($username = $_GET['user'] ?? 'nobody'
), and the spaceship operator (<=>
). To say php isn't growing is an outright lie. Each of these new keywords, operators, and changes is an improvement from what existed before. However, what existed before was simple. It was easy to understand. It made the web accessible. I often wonder if new programmers are overwhelmed by the Javafication of php engineering. Why did function doSomething($withSomething)
need to become public function doSomething(...$withSomething) : array
? While each incremental change is a simple improvement, when taken in totality it can be quite overwhelming.
As we continue to develop the language, we should take a second before voting for some new wizbang operator to reflect on what we want php to be. Should it be a simple and easy to understand language with a few operators? Should it have a steep learning curve and appear like hieroglyphics to the unindoctrinated user? Maybe there's a happy medium. The ranks of php engineers is swollen with people who had little to no prior programming expierience and I'd hate to lose other similar engineers in an attempt to combine some 2 line function into an operator.