Thursday, June 12, 2014

Install APCu on Windows

Assumptions

-I assume that you know what is APC - Alternative PHP cache 
-You want to install APCu because APC is not compatible anymore with PHP 5.5.x
-You want to install APCu for wamp, xampp. Mostly windows web development platforms for PHP

Instructions

Pre: All directory locations might be different for you depending on your wamp installation folder and your PHP/apache versions.

1. Go to http://pecl.php.net/package/APCu, there is a table with available releases
2.Choose whatever release suits you better(I chose 4.0.5 DLL)  
3. Choose package from DLL list, depending on what Windows you are using(32 bits/64 bits) and PHP version. In my case I chose 5.5 Thread Safe (TS) x86
4. Unzip the archive, copy php_apcu.dll in C:\wamp\bin\php\php5.5.12\ext.
5. Go to C:\wamp\bin\apache\apache2.4.9\bin open php.ini  and add the following lines(I just added them at the end of the file):
[apcu]
extension="C:\wamp\bin\php\php5.5.12\ext\php_apcu.dll"
apc.enabled=1
apc.shm_size=32M
apc.ttl=7200
apc.enable_cli=1
apc.serializer=php

This are recommended configurations located in INSTALL file from the php_apcu archive, excepting the location of the DLL file.

6. Restart wamp
7. Go to http://localhost/phpinfo.php and check if apcu configuration table appears and apcu is enabled
8. If you also want to use apcu for PHP CLI then you only need to add in C:\wamp\bin\php\php5.5.12\bin\php.ini the config lines you added at step 5 in apache's php.ini.

The end!

Now you should be ready to start developing faster applications! I hope this helped everyone out there who did not find a tutorial on how to install APCu for windows. I also encourage you to leave me some feedback!

Robert Rusu

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

Tuesday, February 18, 2014

Writing clean code and following the standards

What does "writing clean code" and "following the standards" mean?

You might wonder what does "writing clean code" mean, well it means that as a programmer you write code that can be easy to read, understand, modify and test. Also what I mean by "following the standards" is that we should learn and apply the rules and conventions of a programming language, team and community.

For example Oracle offers the coding conventions for Java or Microsoft offers the coding conventions for C#. Another example would be that if you would like to contribute for developing a framework like Symfony2 you must also follow its conding standards.

Why should we write clean code and follow the standards?

In our days most of the real world applications need continuous development and even small programs require to be changed or improved after a while. Sometimes we often share pieces of code or entire applications to help others. Also from time to time we need to update or fix code we wrote a long time ago. As a result of all this we often spend our time reading code, understand what does it do and we will be required to modify it. That is why writing clean code will help us to deliver quality code which others can understand and can update it to their needs, we will be able to fix bugs and add new features or update the code faster.

Examples of why it's important to write clean code


Nowadays applications get bigger and are used by a lot of users everyday, many of them offer a lot of features and rarely you are the only programmer working on an application, often you will work in a team or even sometimes several teams work on an application, each working at one of its module. You will often need to read and fix a piece of code wrote by one of the other team members which is in vacation, imagine how hard it would be if the code would have been written in chaotically? Maybe you would think that it would have been faster to rewrite the whole thing again? But no investor is willing to pay you to rewrite a piece of functionality again, maybe you would not write it better than the previous team member. Now imagine how easy it would be for you to change the code if it was written clean and easy to understand, even more it was written by following the coding style and standards on which the team agreed at the beginning of the project? It would be a lot easier and you would not need to suffer just because you need to do a bug fix.

Another example of why we should write clean code and follow the standards which I've experienced was that I was assigned a job to update a relative small PHP script designed for interacting with twitter, even if I am experienced with PHP I was unable to understand what did the code do because the code was written chaotically, half of its naming was in the native language of the previous programmer, it was not in English which is the common language used for naming variables, methods, classes, folders etc. The files had between five hundred and one thousand lines of code, variables were not suggestive(at least ones which were named in English). It would have been easy for me to update the script if it was written clean, had meaningful names, if it had been divided logically into modules.

Resources

There are a lot of resources out there which will help you start writing clean code, one of my favorites is a book called Clean Code: A Handbook of Agile Software Craftsmanship written by Robert C. Martin in who is a promoter of writing clean code. 

For following the standards of a programming language in which you write code you should consult the official coding conventions, also if you use a framework you should check out what is the standard coding style on top of which it was built, in general it should be the same of it's related programming language. Besides that a framework often has a standard way to organize classes and other resources, this is one of the many advantages of using a framework, that other programmers can understand the code easier.

Tuesday, February 11, 2014

Important rules when writing tests for your application

Today I want to share some of the most important rules I follow when writing tests for an application. These are some rules I found useful to follow so that tests remain a part of my job which improves on the long term the quality of the code I deliver and makes my life easier. In my opinion same rules apply whether you are doing test driven development or just writing tests after you completed a feature, of course that its preferable to do test driven development as it brings more value to your code quality and your application.


  1. Tests should be small! What does this mean? Well tests should be easy to read, easy to understand and the test should not have hundreds of lines of code. When writing tests you should follow the same rules as you do when writing clean code. Often after several days, weeks, months or even a year you will need to update a feature, which means the tests should be updated, but how will you update the test if it was written such that you cant understand it?
  2. As you might know there are functional tests and unit tests, the purpose of a test should be to verify that a single functionality works in case of functional tests and in case of unit tests the purpose should be to verify that a method has the expected behavior which can be to return a result or to modify the state of an object.
  3. Tests should be independent from each other! One or more tests should not fail because one other test fails. If a test failed it should be because the feature or the method has some issues in its implementation. Also the order of running the tests should not matter, running the tests should output the same result. An example of dependent tests is when first test creates a user and tests if it was saved in the database and then the second tests uses that saved user from database to test the login functionality.
  4. Tests should be independent of the existing data in your database. What I mean by that is that as long as the database schema is the same every time you run your tests the same output should appear. For example you cant suppose when you test a login feature that the same username and password will exist in your database. In your test you should always either use mock objects or insert a user in the database, test login and then remove the user from database. Of course that most of the time its preferable to use mock objects instead of using the database. Same applies for resources used by the application, resources like log files or just simple files in which you store information. Usually when you have a really small application its not worth to use a database and you can use a text file to store information, so when writing a test make sure that in that test you also create a mock text file and delete it after your asserts finished.
  5. Its very important how fast your tests are. It's a waste of time to wait dozens of minutes to run all tests for your application each time you finished a feature, sometimes you will need to run tests after each couple of minutes of writing code. I have seen very big applications(put in production but continuously improved during several years) for which running tests takes even few hours on a a high performance machine. In general to make your tests faster you can decouple your application from the database and use mock objects in your tests and you can not test the GUI of an application each time but that would mean that the "view" part of your application has very small amount of logic, which is a good practice. See how testing an application helps us to write better code? 
There a lot of other rules I might have missed, but consider this just a starting point!

Sunday, February 9, 2014

Maintainability of the code by constant refactoring and unit testing

How often, as a programmer, you delivered new features in the beginning of the project so fast that clients could not believe it, but later, when the project got bigger, the number of lines of code grew and the number of features became larger, you started delivering features very slow? Often that was because the bad architecture of the application but mostly it was because the ugly code you were writing!

What did you do next? Well I suppose that you tried to refactor the code but you might have not succeeded because you were scared that you would introduce bugs. But keeping the code as it is wouldnt be a solution because as you know code rots as time passes, the code becomes uglier and uglier with every new feature or small change if you do not clean it. The solution would be to have unit tests, right?
If there would be tests and you would not be scared to do the refactoring of the code. But then you would need to convince your clients to spend money on refactoring working code. Most of them will not understand that refactoring will mean less money and less time spent on the long run.

I think that the best solution to this situation would be that whenever you work on a new feature or change an existing one because requirements change you should always leave the code cleaner that it was before. When see a piece of ugly written code apply one of the appropriate refactoring pattern without being scared that you might break the behaviour of the application because you are backed up by your unit tests.

Following this rule "leaving code cleaner that it was before you started working on it" and having unit tests will help you have a constant delivering time of new features, no delays because of the complexity of existing code, no bugs introduced by small changes and it will make you happier while working because you know you are delivering high quality software.

Sunday, February 2, 2014

Monday, January 27, 2014

SymfonyLive Portland 2013 - Alexander Mols - Taking caching to the next ...



I recommend you to watch the video because Alexander presented a lot of tips and explained a lot of things regarding caching.
Topics discussed:

  1. Caching for Routing, Container component of Symfony2
  2. Caching in Twig
  3. Doctrine Caching
  4. Varnish
  5. Tricks to prevent making a request to the Symfony2app for misc stuff like showing an html edit anchor

Sunday, January 26, 2014

Wordpress Auto Update Feature

CodeIgniter is Out and Laravel is In!



While working on some projects developed codeigniter more than a year ago I also had issues with the session component not working properly on IE, not sure if they solved that issue. Too bad that codeigniter framework is not developed anymore because as far as I know there are a lot of PHP web applications using codeigniter.

Sunday, January 19, 2014

Thursday, January 2, 2014

Git stash

Using git stash

Sometimes we are working on a new feature on a project using the git version control and we are requested to fix a bug or just check some piece of code on another branch. We would not want to commit a half way done feature, also we would not like to delete the changes to the branch. Well we can use git stash, by just running this command the changes are stashed until we later need them, if we run git status we can see that no changes appear to the branch and we can change branches without the need to commit unstable code and messing our project history. After we finished fixing the bug we checkout the original branch at which we started the feature and we can run git stash apply to add back the changes to the branch.

Other scenarios

There are other scenarios when we might use git stash. For example we started to implement a new feature on a wrong branch. Again git stash helps us, we can run this command and create a new branch using git checkout -b some_new_branch or on an existing branch using git checkout some_existing_branch, now we can run git stash apply and the code which was not commit on the previous branch appears now on the new one.

We can also stash multiple set of changes and you can see them using git stash list, if we want to apply changes from a specific stash we can do git stash apply stash@{stash number}