You are here: Re: Code readability vs. execution time « PHP Programming Language « IT news, forums, messages
Re: Code readability vs. execution time

Posted by Jerry Stuckle on 09/04/07 11:28

Marijn wrote:
> Hi there,
>
> Again a question with no simple yes or no answer at least, opinions
> will probably be scattered so no unanimous yes or no answer ;)
>
> What would you think is the best of the following to options given
> that it is more likely that the databaseConnection is set?
>
>
> SomeClass {
>
> [.............]
>
> /**
> * Sets the connection to the database in use.
> *
> * @return DatabaseFactory
> */
> private function __getDatabaseConnection(){
> if (isset($this->__databaseConnection)) {
> return($this->__databaseConnection);
> } else {
> $this->__databaseConnection = new DatabaseFactory();
> $this->__databaseConnection->create();
> return($this->__databaseConnection);
> };
> }
> }
>
> Or
>
> SomeClass {
>
> [.............]
>
>
> /**
> * Sets the connection to the database in use.
> *
> * @return DatabaseFactory
> */
> private function __getDatabaseConnection(){
> if (!$this->__databaseConnection) {
> $this->__databaseConnection = new DatabaseFactory();
> };
> return($this->__databaseConnection);
> }
> }
>
> The second option definitely has less repetitive code but does that
> really make it the better option given the fact that the
> databaseConnection property is probably set?
>
> By the way, another small convention question: Should you terminate
> control structures with a semicolon?
>
> Thanks
>
> Marijn
>

I agree with the others - the first one. Why would you be creating a
DatabaseFactory if you aren't going to create a DatabaseConnection?

Now if you needed to do some things before creating the connection (and
couldn't do them in the constructor for the factory), you need to use
the second version.

But if you don't need this, let the factory create the connection
without unnecessary calls.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация