Archive for the ‘thought’ Category
Variable declaration in PHP
Today while playing around PHP and stuff I have been thinking of patterns of handling optionally existing variables.
The 2 most commonly used ones are:
if (isset($var) && $var != null) doSomething($var); // or if (isset($var) && $var != 0) doSomething($var); // or if (isset($var) && ($var != '') doSomething($var); if (!isset($var)) $var = something();
However my experience points me that in the majority of the cases it’s enough to check the boolean value of the variable to proceed:
if ($var) doSomething($var); if (!$var) $var = something();
Which reads very kosher. But then we have a problem of getting notice that the variable is undefined, which is not kosher.
So the way kosherize the snippet is to enforce variable definition before usage. Meaning we would like to either use its value if it’s defined, or to initialize it with null. But of course I don’t like to use isset() for that. So what to do? I found an answer!
$var = &$var;
As you can see in this way you declare further usage of the variable. If the variable exists – it does nothing. If it’s not it is silently initialized with null. No notices are yielded. Also it’s better than if(isset()) combination because of readability – the only alphanumeric tokens on the line are variable names.
Isn’t it nice?
Iterators in PHP
Recently, while playing with Scala/Python/PHP comparative implementation of lazy algorithms (I will hopefully describe them some days later) with colleague and friend of mine Michael Fuks, I sorrowfully discovered that, despite there are several SPL units, which define basic Iterator interfaces and functionality, most of the functionality available for arrays is missing for Iterators.
So I decided to fill the gap and started to reimplement for Iterators the applicable non-trivial array functions and also started trying to simulate iterator comprehension (generator routines) in PHP which is missing there too. For this reason I opened a GoogleCode project of php-iterator-utils – please observe my first experiments here.
In addition I need to notice that latest NetBeans build (6.8) branch has great PHP 5.3 support and feels more stable, fast and functional than Eclipse PDT
Programming in Scala
Got the Programming in Scala eBook. I hope to get time and build an experimental Eclipse Plugin with it.
Speaking at …
Reading blog rolls for years, I rather often meet posts of different “socially recognized” ones, which proudly inform us, that they are going to speak at some conference. And what’s very common is that the subject of their messages are literally that they are “speaking”.
What does that mean? That the main fact they provide us is that “they are speaking”, and not that “some important subject will be explained in their speeches”. Otherwise they would say that first.
What can I conclude? That most of them are just egoistic monkeys that in fact using the corresponding issues to promote themselves and collect more bananas, rather than they are really interested in sharing their information, which is (according to my understanding) the purpose of the conferences.
Other monkeys use the chance to participate in such sessions to promote themselves by asking “interesting” questions in these sessions…
It’s funny that some people thought that socializing the network may boost building selfless society. As I see it, it’s just a way for offline losers to find the bunch of other losers of the same kind and to gain respect among them to feed their ill and hungry ego with fast junk.
Well, personally I don’t mind. What I’m truly happy about is that people I respect for real are trying to spread the good by deed, rather by dissimulating word.
Design Patterns
Recently I’ve seen the inter-blog discussion by some PHP guys I don’t know which touches among other things the issue of design patterns. And when they define design patterns they use some canonical or less definitions which seem a bit fuzzy to me.
When I think of design patterns, I always think about patterns of design, thus to define them you first need to define both of them well. With design it’s pretty easy. So let’s talk about patterns.
Pattern, as you surely know, my dear friend, is nothing but a recurring event. But how would you detect recurring events? To do that you need first to detect events, which are no more than time objects. And detecting recurring objects requires memory and ability to compare, which in one word is “intelligence”. Now, knowing the known patterns is nothing but remembering them, which doesn’t require intelligence, while detection of them actually is their usage, since using patterns is already a pattern.
Therefore, design patterning is a way of making designing and observing the design to be more harmonious, or a way of saving designer’s and observer’s intelligence. However, it’s very simple to save intelligence by just not designing the creation. But bad design is nothing but just a very complex design. A design which is created unconsciously is actually a noise and is not harmonious. So using/creation of design patterns is an investment of intelligence in order to save it in the future.
And when not to use patterns then? Only when you:
1. Know to do it perfectly.
2. And you don’t really care if anyone would observe the done.
3. And you will never need to make a similar deed.
I know only one such a task: physiological dying.
Blog RSS feed