Tuesday, April 29, 2014

Things that you should do in order to become a better programmer

All of us try to become better at what we do, as a programmer I will try to give you a small list of things that will help you to become a better programmer.

#1 Always try to help others

Its always a good thing to help others when they are in trouble, when they need to learn new things or they just need an advice!

First of all helping others will help you understand better a concept or that something that they need explained. Your knowledge regarding that technology or whatever you help them with will get better just by trying to explain it. Being able to explain it in simple ways and giving others simple examples denotes the fact that you can master it.

For example helping others understand a design pattern, explaining them when and how to use it might very well refresh your memory regarding what you know about it. This would be another reason to help others. Helping others will make you feel better however its also important to not neglect your work!

#2 "Learning from the big fish"

This is a term that I often use when I am trying to say that you should always try to learn new things from more experienced programmers. For example I always try to see how more experience programmers act in different situations, how they solve different problems and what practices they use.

Its very important to also understand why more experienced programmers use a tool instead of another or why they are applying a design a pattern instead of another. Just copying what the "big fish" do is not enough!

"Learning from the big fish" can be accomplished in many other ways than meeting them face to face, I suggest reading books, watching videos or tutorials, watching(or even better going) to conferences hold by other programmers, reading articles or by just asking more experienced friends how to solve a particular problem or what would be a common solution to a general problem.

#3 Training

In my opinion its very important to train your programming skills. Even if you work 8 h a day I suggest that you should spend some time improving your skills. Programming is a skill and every skill is lost if you are not continuously training. 

This might be consisted of just working on a personal project in which you use another programming language than the one you use at work. Yes, learning a new programming language will improve your way you are programming in your favorite language just by understanding new concepts, existing problems solved in another manner than you were used to or just getting a fresh perspective.

It might be just you trying to understand concepts that you did not quite understand during work. It might be something new that you will encounter at work and you are trying to get a perspective about it. Or it can just be you write some small program to sharpen your coding speed or improve your analytic thinking.

Good luck!
Robert Rusu

Monday, April 14, 2014

Compile error Easy Mock

Easy Mock is a mocking library for Java which can help you to test your code isolated from some other components.

If you ever get the next compile error:
"expected (java.lang.void) in easymock cannot be applied to (void)"
It may be a result of you trying to call EasyMock.expect(mock.methodWhichReturnsVoid()), instead of doing that you should use expectLastCall method, you can use it like:
mock.methodWhichReturnsVoid(); 
EasyMock.expectLastCall();

I posted this tip because it took me several minutes to figure out what I was doing wrong, by the way, I was really tired!

Robert Rusu

Saturday, April 5, 2014

Symfony2.3 form, grandchildren forms are not validated

Recently I had some issues with Symfony2.3 grandchildren forms not being validated even if I used as default option 'cascade_validation' => true. After doing some research I found other people having same issue and I found out that the solution is that besides putting 'cascade_validation' to true as default option to the form I also had to 'cascade_validation' => true in the collection of children and grandchildren forms I added.

Check the code sample below
Father form
class FatherType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'children',
            'collection',
            array(
                'type' => new ChildFormType(),
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                'required' => false,
                'cascade_validation' => true //important to be added
            )
        );
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'Company/SomeBundle/Form/Entity/FatherFormEntity',
                'cascade_validation' => true, //important to be added
            )
        );
    }

    public function getName()
    {
        return 'father';
    }
}
Child form
class ChildType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'grandchildren',
            'collection',
            array(
                'type' => new GrandchildFormType(),
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                'required' => false,
                'cascade_validation' => true //important to be added
            )
        );
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'Company/SomeBundle/Form/Entity/ChildFormEntity',
                'cascade_validation' => true, //important to be added
            )
        );
    }

    public function getName()
    {
        return 'child';
    }
}
Grandchild form
class GrandchildType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'attribute',
            'text'
        );
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'Company/SomeBundle/Form/Entity/GrandchildFormEntity',
                'cascade_validation' => true, 
            )
        );
    }

    public function getName()
    {
        return 'grandchild';
    }
}

References: https://github.com/symfony/symfony/issues/5204


I hope it helped you!
Robert Rusu