Mar 19, 2013

A PHP client for TYPO3 TER

A useful piece of software from Elmar Hinz: a command line utility for TER, which can upload TYPO3 CMS extensions to TER and do many more, which you cannot do in the TYPO3 Extension Manager.

Get it here.

How to calculate 1+1 using the “nice code™”


use SimplePHPEasyPlus\Number\NumberCollection;
use SimplePHPEasyPlus\Number\SimpleNumber;
use SimplePHPEasyPlus\Number\CollectionItemNumberProxy;
use SimplePHPEasyPlus\Parser\SimpleNumberStringParser;
use SimplePHPEasyPlus\Iterator\CallbackIterator;
use SimplePHPEasyPlus\Operator\AdditionOperator;
use SimplePHPEasyPlus\Operation\ArithmeticOperation;
use SimplePHPEasyPlus\Operation\OperationStream;
use SimplePHPEasyPlus\Engine;
use SimplePHPEasyPlus\Calcul\Calcul;
use SimplePHPEasyPlus\Calcul\CalculRunner;


$numberCollection = new NumberCollection();

$numberParser = new SimpleNumberStringParser();

$firstParsedNumber = $numberParser->parse('1');
$firstNumber = new SimpleNumber($firstParsedNumber);
$firstNumberProxy = new CollectionItemNumberProxy($firstNumber);

$numberCollection->add($firstNumberProxy);

$secondParsedNumber = $numberParser->parse('1');
$secondNumber = new SimpleNumber($secondParsedNumber);
$secondNumberProxy = new CollectionItemNumberProxy($secondNumber);

$numberCollection->add($secondNumberProxy);

$addition = new AdditionOperator('SimplePHPEasyPlus\Number\SimpleNumber');

$operation = new ArithmeticOperation($addition);

$engine = new Engine($operation);

$calcul = new Calcul($engine, $numberCollection);

$runner = new CalculRunner();

$runner->run($calcul);

$result = $calcul->getResult();
$numericResult = $result->getValue(); // 2


Example from https://github.com/Herzult/SimplePHPEasyPlus, thanks to Mathias Schreiber for the link :)

Conclusions? Do not overdo it. Simplicity will save the world. Just do a "1+1", not the framework for calculating it.

Mar 11, 2013

Bad coding

Never code like this:

function hasContentElements($pageId) {
    $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*',
       'pages', 'uid=' . $pageId ...);
    return $rows;
}

function processContentElements($contentElements) {
    foreach ($contentElements as $contentElement) {
        ...
    } 
}

$hasContentElements = hasContentElements($pageId);
if ($hasContentElements) {
    processContentElements($hasContentElements);
}

Why is this bad? Because:
  • Function name hasContentElements suggests that it only checks that the page has content elements. It should not retrieve them. Imagine if there are 100 elements on the page. How much memory would it take just for a simple check like that?
  • The variable is named improperly too. "has" means a boolean value!
  • Using the variable with such name is misleading here. Unless you see and remember what the function does, you can make wrong assumptions about the logic of the processContentElements function.
When coding, please, choose proper names for all your code elements. If you have to check for existence of database records, do not fetch them, use COUNT(*) instead. That makes a great difference in terms of performance and memory usage.

Showing the current Git branch in a shell prompt

The following commands in ~/.bash_profile will show you on what Git branch you are and if there are dirty files there:

source /opt/local/share/doc/git-core/contrib/completion/git-completion.bash
source /opt/local/share/git-core/git-prompt.sh
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='\h:\w \[\033[1;36m\]$(__git_ps1 "[%s] ")\[\033[1;37m\]\$ '

This will show the branch in the cyan color and only in the directory, where you checked out the project. If there are dirty files, it will show an asterisk after the branch name.

Example:

Atlas:~/Projects/TYPO3/TYPO3_6-0 [issue_46000 *] $

Mar 4, 2013

Quote #1

"There's nothing of any importance in life – except how well you do your work. Nothing. Only that. Whatever else you are, will come from that. It's the only measure of human value."
Francisco d'Anconia