Friday 20 July 2012

Code Readability

Or why I use ternary operations but never single line if statements.

This is a massive issue on collaborative projects. And since virtually every single piece of code you use will need to be modified / understood by someone else in the future, in my opinion everything you write should emphasise the readability rather than functionality.

Of course there will always be projects where speed is important, and you want to use the data structure that is fastest / most efficient.

As a professional software engineer (even if it is "just" websites) I am constantly working with other people's code. And it frustrates the hell out of me that people write code that is just so hard to understand.

A ternary operation is fairly trivial for any professional to understand. Even there though, the use of a variable name that explains where it comes from would be so handy!

One thing that annoys me though is single line if statements. Most IDE's allow you to format everything properly according to your conventions (we use Zend), and if everyone follows those it makes it much easier to follow the code when you have to fix a bug. But scanning code by eye for a line (I'm lazy like that) is made so much harder with single line if statements. Especially when you're checking for a value like: $_SESSION['carhire.search.dropoff_location'] .

The number of studies that have been done on the width of columns for readability is astonishing. Made your code more readable by using the following format always:

if (statement) {
    then do this
}

And comment your bloody code or I will curse you forever.

Saturday 14 July 2012

Zend Form, and auto height of a textarea

Zend framework has it's uses. But gee it can be a pain at times.

If you don't have access to the decorators of the form, and you want to style your text areas better, this snippet should come in handy. Stupid scroll bars.

var textArea = "";
jQuery('textarea.info').each(function() {
                    textArea = jQuery(this);
                    textArea.removeAttr('rows');
                    textArea.removeAttr('cols');
                    textArea.css('height', this.scrollHeight + 'px');
                });

Pimcore, Helpers and Forms

It's taken me a while to get around to this, but I'm finally getting somewhere with this. Here's the code I've got. Underneath is an explanation of everything.

I've assumed you're using the formbuilder plugin for Pimcore with this code.
    
        
    public function init()
    {        
        parent::init();
        $this->_helper->addPath(PIMCORE_WEBSITE_PATH . '/views/helpers/cru', 'Website_Helper_Cru');
    }
    
    public function formpageAction()
    {
        // Make sure I want to display the form
        // the useForm property is a checkbox, so I always get true/false
        if ($this->document->getProperty('useForm') && !empty($this->document->getProperty('formName'))) {
            // Put everything in a try/catch statement, as there is some buggy code here
            try {
                $this->formHelper = $this->_helper->getHelper('Forms');
                $form = $this->formHelper->getForm($this->document->getProperty('formName'));  

                // Do I have a submitted form to worry about?
                if ($this->getRequest()->isPost()) {
                    if (!$form->isValid($_POST)) {
                    }
                }
                // We are going to display the form
                $this->formHelper->setDecorators($form, 'default');
                $this->view->form = $form;
            } catch (Exception $e) {
                // Log the error if I had one
                Logger::error($e->getMessage());
            }
        }
        $this->enableLayout();
    }


Ok. This is (hopefully) obviously all inside your controller.

When the controller is initialised, add an extra path to the helpers paths. Now, instead of just looking inside Zend, my controller will now look for helpers in the /website/views/helpers/cru/ folder.

Now, in my document that is using the formpage action, I look for 2 properties. A checkbox called useForm (ticked on) and a formName (I have to know which form to load!).

If both of those properties exist and have a value, add on a formHelper to the action so it can do things.

This class is called Website_Helper_Cru_Forms and the filename is Forms.php. I'm sure you can figure out which folder it's in.

I haven't fleshed out the validation of the form. I imagine I'll end up adding something in the formHelper class to do this.

I add on the decorators using my helper. I haven't yet got a good way of doing this, I plan on using .ini files but I'll get around to this later.

And finally, add the form to the view. In the view you simply call echo $this->form to print it out. If you've set up the decorators and validators correctly everything displays in a nice pretty format :)

I've also wrapped the whole thing in a try/catch statement. There are quite a few ways this could break at present as I don't do much error checking yet. That's another thing on the to do list.

Let me know of any obvious errors you can see with this code in the comments below. I hope this helps somebody!