Work and not work

An event jogged my memory a couple of days ago and provided the inspiration for this blog article. A number of years ago whilst freelancing I rang a web agency to see if they hired freelancers and whether they had a project I could work on.

I told the person in the conversation about my education, my previous work, my company but I also mentioned that I study programming as a hobby also. Immedietly in a raised voice “we are not doing this as a hobby, we do this as a profession, this is not just some hobby!”. At the time I was shocked, what kind of response is this?

I came to the conclusion that this person obviously cannot conceive that programming can be studied as a profession “and” as a hobby i.e. studied in spare time outside of working hours. The reason the person cannot conceive of this is because this is something the person has never done. This person has never worked on a programming project in his spare time outside of work.

The idea that someone could dedicate extra time outside of professional work to programming something they enjoy is inconceivable to him because he does not enjoy it. Surely anybody who enjoys programming and the greatest programmers out there will know that the programmers who also study programming as a hobby are more likely to be more committed to programming than those who only do it for their profession.

This sort of response can be seen across the whole working spectrum, the more the person takes the work “seriously” the less he is interested in doing work. He takes work “seriously” because he does not really want to work, he gets no enjoyment from doing the work so he cannot conceive that anyone else can enjoy doing work.

In reality he does not work take work seriously at all … “we are not doing this as a hobby, we do this as a profession, this is not just some hobby!” has no real meaning. It says nothing as to the quality of the work he is producing, supposing my code as a hobby is of a greater quality than his code as a profession? His statement says nothing, it is just an empty statement stated with the tone of someone trying to be serious but cannot be taken seriously.

Another situation might be the boss who does not let his employees work from home. This can be well rationalized “we do not like our code on other peoples computers it is a security risk” etc. In reality the boss does not let his employees work from home because the boss cannot concieve of an employee who can actually do work when the boss is not watching. You have to be there so the boss can see that the work is being done.

Again this ties into the enjoyment aspect of it. You cannot possibly enjoy work that much that you would go home and work when he is not watching because he does not enjoy doing the work that much and it is not something he would do. It is the joy aspect that is being targetted.

There have been a couple of occasions in the past where I have been working on a programming task in an office and I have looked up to notice a collegue staring at me. One of the collegues was staring at me in joy with a smile on his face, one of the collegues was also staring at me in joy with a smile on his face and he made the statement “you thought you where being clever there didnt you”.

I never thought much of this at the time but the more I thought about it the more it made sense, in those collegues eyes I must have looked similar to this …

https://www.youtube.com/watch?v=laG5XawvtqA&t=980

No I am not claiming to be some super genius, but what they saw was probably something similar to that in their own eyes. What they where actually seeing was the “aliveness” of myself actually enjoying working on a programming task.

This can also be seen when asked to struggle with a programming task. Say a new developer is being introduced to a new codebase …

Myself …

This is how I implemented file uploads.
This is how I implemented error handling.
I put these methods in this Model here and they do this and help with that.
I created this helper class to deal with this.

General developer in the industry …

Yea, just look through the code and struggle with it, mate.

A little bit of struggle is not wrong. Sometimes I already more-a-less knew the answer I was asking I just needed confirmation. Sometimes the struggle can create the “fire” to actually do the work.

I am not saying there should be no struggle I am just saying there should be a balance. In this industry I see far more of the “struggle” path than the opposite, even to the point of asking a question no longer gets an answer but the developer is expected to struggle for 2 hours to get an answer to a question that would have taken 30 seconds to answer. I mean how is this sort of struggle productive? In my opinion, it is counter productive.

The “struggle” path developer rationalization to this is always, “well if I had given you the answer I may as well of been doing the work for you”. Yes in some situations this could be valid but in a lot of the situations I have encountered this it was clearly not valid. It is just an excuse for the “struggle” path developers own lack of effort.

In my own opinion all questions should be answered no matter how simple the answer may be. It may not be simple for the developer who is just starting out. Answering the question helps relieve a blockage in the production.

The next arguement could be “how I am suppose to get my own work done if I am answering questions all day” this could be valid, but then it is a management problem, the manager should account for the fact that the more settled in developers will need to answer the new developers questions.

I personally think all “lead developers” job roles should consist of helping / answering developers questions and maintaining the consistency of the codebase / architecture of the code.

That is all the lead developer needs to be doing, the lead developer should act as a “leader” to the none lead developers and as a person who ensures the cohesion of the codebase as a whole releaving blockages in the production by helping the none lead developers.

The lead developer should not have a huge number (if any) tasks on his plate at all and his / her sole focus should be on the none lead developers, leading them. In reality what generally happens is the lead developer has just as much work as the none lead developers have to do.

What happens? No help is given to the none lead developers, tasks take longer by not getting answers to questions, none lead developers look incompetent, lead developers looks good because his work is getting done and generally everything just takes longer.

Developer asks a lead developer a question, answer it. Developer is stuck on writing a block of code, pair programmme with the developer for 30 minutes, relieve the blockage. Lead developer checks the code commits for the day and finds mistakes, point out the mistakes and ask the developer to correct them. Lead developer checks the code commits for the day and finds inconsistencies in the code which would not fit well with the rest of the code, architectural inconsistencies, point out the inconsistencies and ask the developer to amend them.

The lead developers role in my ideal basically consists of leading and empowering the none lead developers. In most companies the lead developer is merely the developer who has been at the company the longest, not really leading anyone at all. The lead developer does the same job as the none lead developers and to some extent is expected to do more than the none lead developers.

What is worse is that the lead developer is given “first dibs” on most projects, this means the lead developers code 90% of the codebase and then it is passed onto the none lead developers to amend and maintain.

Manager …

Ohh I will give Lead Developer X this project to do, like I did with the last one and the one before that because he knows what he is doing and he gets the work done within the deadlines. Of course he knows what he is doing, he started from a blank canvas. He makes the manager look good.

Once the project completed and passed on the lead developer is then working on his next project which he got “first dibs” on, so he is now “too busy” to answer the questions of the none lead developers even though he is the only person who can really answer the questions as he wrote most of the code in the whole business.

This kind of position is somewhat of a nice setup for a lead developer, even though the lead developer may be writing more code than the average none lead developer.

A solution? Each developer is given his / her own project or set of projects to build and maintain. If the project is absolutely a huge project that requires multiple developers, then the lead developer is the “leader” and the none lead developers are the “developers” doing most of the work.

Solution for using DatePickers on forms in Yii2

Sometimes in web software applications we need to collect dates such as a start date or end date using forms. In this article I will be using the Kartik DatePicker widget which can be found here …

http://demos.krajee.com/widget-details/datepicker

For the purposes of this article a master Model will need to be created. I discussed how to create master Models and Controllers in the following article …

Create a master Model and Controller in Yii2

So based on the master Model now being created the article can continue. Add the field to the form using code similar to this. Include the plugin in the form …

Add the field to the form …

In the master Model 2 methods needed to be created …

And …

These methods can be called to save the “date” data to the database from any of the child Models. The best place to call the methods would be in the “beforeSave” and “afterFind” methods as follows …

And …

Depending on how you have setup the validations for the “date” attribute you may need to run the “saveDateFormat” and “dateFormat” methods in the “beforeValidate” and “afterValidate” methods as well as the “beforeSave” and “afterFind”.

This depends on whether your validations are formatting the date as the date would be saved to the database or validating against the format the date appears on the form.

If your validations validate against the format the date saves to the database then calling “saveDateFormat” and “dateFormat” methods in the “beforeValidate” and “afterValidate” methods will not be needed.

I generally setup my validations so I do not need to run the “saveDateFormat” and “dateFormat” methods in the “beforeValidate” and “afterValidate” methods only through the “beforeSave” and “afterFind” methods.

Using the above code you can implement multiple datepickers on the same form or across different forms. Follow the same logic, add the field or fields to the form and call the “saveDateFormat” and “dateFormat” methods from the child Model.

Populating standard columns in a database table using Yii 2

When creating web based software applications we sometimes want to have a standard way of populating a variety of database table columns which we intend to appear in multiple database tables within our software application. These columns may generally include the following …

  1. userID – for storing the user ID of the user who owns the record
  2. createdDate – for storing the date the record was created
  3. createdBy – for storing the ID of the user who created the record
  4. updatedDate – for storing the date the record was updated
  5. updatedBy – for storing the ID of the user who updated the record

The above are the standard columns I normally use within my database tables when creating web applications. I deliberately separate the userID from the createdBy and updatedBy. As I look at the createdBy and updatedBy as more of a audit trail and the userID as the actual owner of the record and the foreign key which connects that record to the users table.

In this article I am going to show you a method to populate these values automatically. These fields will be populated whenever you save a record within the web application.

As I presume you are going to want this method exposed to more than 1 Model within the web application I advise you first read the earlier article I wrote about creating master Models and Controllers …

Create a master Model and Controller in Yii2

Master Model and Controller classes are Model and Controller classes which are a parent class to all Model and Controller classes within your web application. All of the methods within the master Model and Controller classes are exposed to the Model and Controller classes within your web application that extend from those master Model and Controller classes.

Now that you have created a master Model make sure you have the following use statements within your master Model …

Now put the following method in your master Model …

The Yii2 beforeSave method will fire whenever a Model is saved within your web application. As the beforeSave method is used in our master Model, the beforeSave method will fire whenever a Model is saved providing that Model extends from our master Model.

The method above is made up of 5 IF statements each setting a variable on the Model. The variables on the Model are those 5 database table columns discussed at the beginning of the article. The above code can summarized as follows …

  1. Line 1 – beforeSave method, $insert variable is true or false depending on whether this is inserting a new record or updating an existing record. Yii2 sets the $insert automatically
  2. Line 3 – IF statement checks to see … userID column exists in table, this record is a new record being inserted, the user is logged in and hence has a userID, the userID variable has not already been set on the Model
  3. Line 4 – Set the userID variable on the Model to be the logged in users user ID
  4. Line 6 – IF statement checks to see … createdDate column exists in table, this record is a new record being inserted
  5. Line 7 – Set the createdDate variable on the Model to be the current datetime
  6. Line 9 – IF statement checks to see … createdBy column exists in table, this record is a new record being inserted, the user is logged in and hence has a userID
  7. Line 10 – Set the createdBy variable on the Model to be the logged in users user ID
  8. Line 12 – IF statement checks to see … updatedDate column exists in table
  9. Line 13 – Set the updatedDate variable on the Model to be the current datetime
  10. Line 15 – IF statement checks to see … updatedBy column exists in table, the user is logged in and hence has a userID
  11. Line 16 – Set the updatedBy variable on the Model to be the logged in users user ID
  12. Line 18 – beforeSave return true, the Model will not save unless this method returns true

If you need to use the beforeSave method in a Model that extends from the master Model and you want to retain the functionality of the beforeSave in the parent model you will need to call the parent beforeSave as follows …

Please note you do not have to use the beforeSave method in a Model that extends from the master Model for the beforeSave method in the Master Model to work. The above example is only shown in case you want to run further code in the extended Model that happens on the beforeSave whilst still retaining the functionality in the master Model. If the extended Model has no beforeSave method that is fine, the code will still run in the master Model and populate the database columns with the data.

It is also worth noting that you can also use the afterFind to format the data when finding Model records …

The above example shows the automatic formatting of the created and updated dates for user interface display purposes within our web application. The examples in this article can be extended to include any data or code you like in the beforeSave and afterFind methods. I personally use these methods in the web software applications to save and format general data that is used through many of the database tables within the web software application.

This method reduces code duplication, reduces chance for errors as the code is not in multiple places, keeps the code neat and tidy by only being in one place. This method frees you up to concentrate on other areas of the web software application.

Please also note Yii 2 does incorporate code to automatically fill in the “createdBy” user ID and “updatedBy” user ID using the following behaviour …

http://www.yiiframework.com/doc-2.0/yii-behaviors-blameablebehavior.html

However the above code I wrote can be used to automatically fill in more fields than just “createdBy” user ID and “updatedBy” user ID.