Is University important?

There are lots and lots of developers in the industry who never went to University and some never went to College either. Well I went to both University and College (Sixth Form) and in my opinion it was definitly worth it.

To start with before University I never had any interest in programming and I would for the most part say it is only because of University that I became a programmer. At University I was given a series of lectures and practical lessons. The lectures and practical lessons form the equivalent of a part time week. In the other half of the week you are expected to be self learning the subject discussed in the lectures and practical lessons.

None of this is mandatory, you do not have to turn up to lectures or practical lessons but seen as though you are paying for the lectures and practical lessons it would be a waste of money not to. University treated me more like an adult than any other educational organisation I had been to prior to University. You are actually a customer of the University.

A certain opinion on University in terms of the programming world are that Universities do not prepare you for the “real world” of development. To some extent this is true, University certainly gives you the ideal programming world. A world in which your profession is respected and you are given ample time to conduct your research.

I once worked for a company where one of the developers stated on a task I was working on “its not rocket science is it”. For the most part the developer was quite right, it was not the hardest task in the world to be working on. However that is besides the point, what does it say when a developer makes such a comment? This developer degrades his own profession by making the claim that the work is simple and easy.

You would not hear such comments at University as far as I am aware because the lecturers respect their profession and they create that same feeling of respect in their own students. You are a computer scientist working to solve real world problems.

A lot of companies are formed by people who never went to University and a lot of companies have developers working at the company that never went to University. For anyone who preaches “University does not prepare students for the real world of development” this is probably the reason why.

The people at the above companies never went to University and never had that same sense of respect instilled in them for their profession. Let us not degrade our own profession by debasing ourselves and making comments like everything is simple and easy, even if it is. In other words the degraded “real world” of development is at odds with the Universities respectable “real world” of development or rather there are so many people joining this bandwagon that it has somewhat become the “real world” because of themselves.

When you go to a University you have the fundamentals of programming explained to you in the lectures. The correct terminology is used to explain everything and it is explained in a professional manner. The feeling this creates in you makes you want to be apart of the latest innovations, discoveries and technologies.

Looking back after a number of years in development I would say that University teaches the foundations of each subject on the course you have taken on. I would agree that there is a lot of self learning to do in your own time but that is to be expected of University is it not? Maybe the people who are saying “University does not prepare for the real world” have been dealing with people who never did any self learning in their free time whilst at University.

Looking back there was a lot of people who only seemed to go to the lectures and practical lessons when they could be bothered. Never seeming to learn anything outside of the lectures and practical lessons. It is quite possible that they still managed to scrape a pass on their degree and maybe that is the reason for the skewed perception.

I would argue that the people who did that are probably in the same category as those people who could not be bothered to go to University at all. They are probably the same people now complaining that “University does not prepare you for the real world”. Ultimately though everyone is a individual whether they went to University or not and it is all about the investment they have made in the programming studies.

I think University does introduce programming students to some concepts they may not have readily picked up outside of going to University. Take “recursion” for example. I have worked on codebases that had parts of code written in ways that could have really used recursion and should have used recursion.

It seemed to me that the programmer had struggled to write the piece of code because he was unfamiliar with the concept of recursion. Not that he could not do recursion but that he was not even aware of recursions existence. This would have been covered at University so at least the programmer would know recursion existed. Had the programmer known recursion existed it would have most likely been used.

University gives you the ideal development world view and I assume the very top companies in the programming world share similarities with this world view. I can only assume this is the case because it all depends on the people working for the said companies.

So was University important? From the learning programming standpoint, perhapes not so much. However from instilling respect in your chosen profession, very much so.

King of Adem – progress update – building queue

Progress has been made on the King of Adem project. For those of you not familiar with this project please read the original King of Adem post at …

http://www.jamesbarnsley.com/site/2017/08/13/king-of-adem/

Since the update I have been primarily working on the building queue. The building queue is the queue which buildings get inserted into when a player wishes to construct a building in one of his / her regions.

To get to the construct building screen the user has to be in one of his / her regions so those areas to get to that screen had to be part built.

The areas part built are namely user, game, region and building. Once on the construct building screen the user can select from a list of buildings that can be constructed.

Each building is validated on a case by case basis and if it does not pass the validations the building cannot be constructed, here are some of the validations so far …

Check fund available
The player must have the correct amount of fund available to construct the building. The amount of funds available is the players start of turn funds subtracting any funds that have been used in the current turn.

For example player may have started with 1000 coins, if a player constructs a building for 300 coins then the program gets the players start of turn funds but substracts 300 coins because it knows a building worth 300 coins was requested to be constructed this current turn.

Check building queue full
The building queue may only allow so many buildings to be in the queue at any one time for each region. Currently this number is set at 5 and the number is stored in a config file.

Check building already built
Check if the building has already been constructed in the region. Each building has a “constructionLimitPerRegion” variable which stores the number of times that particular building may be constructed in each region.

Check dependent buildings constructed
Each building can have what are called dependent buildings. These are buildings which need to be constructed first before you can build the current building.

Also the validation knows whether a building is an upgrade of an existing building or a completely different building that just happens to depend on another building being constructed.

All kinds of checking had to be implemented for this validation, whether building is an upgrade / dependant or just dependant, whether advanced tier buildings already exist for lower dependant buildings, whether lower dependant buildings already exist etc.

Check advanced tier building exists
Check if an advanced tier building already exists for the current building wanting to be constructed. If the advanced tier building already exists then there is no reason to build the current building.

Again this needs to take into account that some buildings can be constructed more than once so in some instances it may need to allow the building to be constructed even though the lower building has already been constructed.

The above validations are run everytime a user issues the command to insert a building into the building queue for a particular region. These validations are run from the “Building” Model. Each building is also given its own Model, the hierachy is like as follows …

\yii\db\ActiveRecord -> \app\components\ActiveRecord -> \app\models\Building -> \app\models\BuildingTownBarracks

Or …

\yii\db\ActiveRecord -> \app\components\ActiveRecord -> \app\models\Building -> \app\models\BuildingPort

etc.

I can run special validations for specific buildings by placing those validations in the specific building class. Those validations will only run for the specific building and not all buildings.

That is as far as I have got so far. Still a long, long way to go, more updates to come soon.

King of Adem

The latest personal project I am working on which also happens to be the largest personal project I have worked on so far.

King of Adem is a game for tabletop minature wargamers. Players manage their kingdom through the app. Kingdoms consist of a number of a territories, each player starts with one.

In each territory there is a capital city in which buildings can be constructed. Buildings provide benefits like more money or the ability to recruit armies.

When 2 armies collide by moving into each other the players can then resolve the battle as normal by playing their normal game of tabletop minatures.

The game takes place on an island called Adem. Adem is a recently discovered island where each player is fighting for control. You fight to become the “King of Adem” by conquering the whole island.

Although Adem is stated as “recently” discovered, this only means “recently” in the grand scheme of things. Adem actually has a number of Kings / Factions already on the island and each Kingdom has some infrastructure.

The project is currently divided into 3 parts …

King of Adem Diagram

Part 1 – Game Server
The Game Server is currently in construction and is being developed using the Yii 2 framework written in PHP. The database is MySQL. The server takes view requests and command requests from the client application.

View requests provide data to the client in JSON format. This data has everything needed to render views for the game for example, building, army, territory data.

Command requests provide data to the server about interactions happening in the client application. For example move commands, construct building commands etc.

The decision to use View / Command requests was chosen instead of opening the server up entirely.

Essentially one option could have been for each Data model on the server to have a view, insert, update, delete request to use for either commands, views or both.

This would give the client more freedom at the cost of putting more logic onto the client.

As the first client is being developed using IOS and IOS uses a view per screen type of structure then View requests make sense. Giving the client the exact data the client needs in order to render the view.

Part 2 – Game Test Client
The Game Test Client is currently being developed alongside the Game Server and serves as a testing ground for the development of the server.

The Game Test Client can send View requests to the server and read / present the data from the server in a user friendly way. The Game Test Client can send command requests to the server.

The Game Test Client is being developed using the Yii 2 framework written in PHP. The Game Test Client is my own personal client for testing the server and is not the actual client the end user / player will be using.

Part 3 – IOS Game Client
The IOS Game Client is the actual client the end user / player will be using to play King of Adem. The IOS Game Client as you might have guessed will be an IPhone application. There may also be the ability to run the application on the IPad as well.

I decided to develop the IOS Game Client because I wanted the game to be portable and it cannot get much more portable than having the game on your phone.

I also choose the game to be an IOS application because I want to improve my IOS skills and offer IOS development as part of the everyday services I offer to my clients.

Once these 3 parts are complete the game will be playable and released to the public. Part 1 and 2 are already in construction, Part 3 will be constructed after parts 1 and 2 and completed.

After the game has been released to the public, a number of other client applications could potentially be developed including an Android version and a full game client developed in Unity3D or similar.

The IOS client will be more management like as opposed to a graphical game. The IOS application could include graphics etc, but it will not be at the level of a full game like experience. This is why I suggest a later developed Unity3D or similar game engine development.

I will be keeping everyone informed of the development of this game through the blog so keep checking back for further updates.

Select2 library solution in Yii2 part 4 – retrieving the data

To continue on from the previous article I wrote about saving the Select2 data …

http://www.jamesbarnsley.com/site/2016/11/07/select2-library-solution-in-yii2-part-3-saving-the-data/

I will now talk about how to the retrieve the stored Select2 data so that the data can be displayed on the front-end Select2 field. Following on from the Task / Countries example, place the following “loadSelect2Items” method inside the Master model or Task model if you did not create a Master model …

The code above can be explained as follows …

  1. Line 3 – return the array of Select2 data using the “array_map” function
  2. Line 4 – store the related entity models name into a variable, in this case that would be the Country models name. This uses the Country relationship that was created in “Part 2” of this article series
  3. Line 5 – store the IDs of the related relationship entity model in the return data, in this case that would be the IDs of the Country model records
  4. Line 6 – find all the records for the model / model property in the “select2_data” table, in this case that would be the Task model and “countries” property

The next step is use the “loadSelect2Items” in the Task model to the load the Select2 data into the “countries” property, place the following inside the “afterFind” method of the Task model …

So that concludes the Select2 library solution for Yii2. I have covered the creation of the necessary database table and the read / write methods for reading / writing the data.

After the initial methods have been setup this solution will provide you with a way of easily putting a Select2 field on your forms that allows for many to many relationships between models with just a couple of line of code.

My new way and correct way of coding Controllers

I have been using frameworks for a number of years with the Yii2 framework being my framework of choice at the present time. When it comes to defining how an application should be structured in terms of Controllers and their methods i was always one to put methods wherever I felt it made sense to put one.

For example my UserController would consist of index, view, create, update, delete, login, logout, lostpassword methods. This made sense at the time. Any action methods that relate to user functionality could go in the UserController. However all the time I was doing this I was aware that some developers and articles on the internet point to the fact that Controllers should only have certain methods within them, namely the standard CRUD methods and nothing else.

So one day I decided to follow that train of thought and start writing my controllers to only have the following methods, index, view, create, update, delete. These methods serve the following purpose …

  1. Index – view all records in a grid view
  2. View – view single record in a detail view
  3. Create – create form and submission code for single record
  4. Update – update form and submission code for single record
  5. Delete – delete a single record

I am aware that I could even get rid of the index and just handle the “view all” and “view single record” through the single Controller method of “view” by checking to see whether an ID parameter has been provided to the method however I thought I would keep them separate.

I am quite pleased by how the new structure has turned out in that it makes my Controller less bulky with code and it gives a much more structured neat and clean feeling to my code and software application.

So you might be wondering what happened to my UserController and the extra methods that where described earlier i.e. login, logout, lostpassword etc. Well using the new system they can be broken down as follows …

UserController

  1. Index – view all users in the system
  2. View – view single users details
  3. Create – create a user
  4. Update – update a user
  5. Delete – delete a user

LoginController

  1. Create – log the user in
  2. Delete – log the user out

LostPasswordController

  1. Create – send the user a link to change the users password
  2. Update – process the clicked link and change the users password

So it is possible to do this, basically the Controllers within the software application now take more definition by having the Controllers named after the type of event the user is wanting to make within the software application and placing less emphasis on the Controllers methods providing the definition. I really like my new way of coding my Controllers and I intent to stick to this method for the foreseeable future.

HTML5 Web Storage Implementation

Recently I have been working on a client project which required the ability to store data from a server on the users local machine. In this case the users local machine was a mobile phone, IOS, Android, Windows etc.

It does not really matter on the platform as it will be stored using the mobile phones web browser and most of the browsers are modern enough to have the new HTML5 Web Storage mechanism.

In actual fact the data will be stored using PhoneGaps implementation of HTML5 Web Storage. PhoneGap is a framework that allows the user to write phone software in HTML5, CSS3 and Javascript.

So for all intents and purposes it is like writing a responsive website that works on mobile phone browsers but is actually compiled and runs like a real native software application. PhoneGap interfaces with HTML5 Web Storage for the purposes of using HTML5 Web Storage mechanism.

So to store the data locally I found WebSQL and HTML5 Web Storage to be my options. Although WebSQL looked perfect for storing and querying the data, it is actually being depreciated and will no longer be in use as a standard in future. So my mind was made up for me in that case.

Web Storage on the other hand seems very straightforward, but this would be the case as it does not include the more complex features of querying the data. Web Storage works by storing name / value pairs. So we can say colour = red, or age = 25, name / value.

Like I said this is straightforward but we cannot say give me all persons over the age of 20 for example there is no querying mechanism.

In actual fact there is no mechanism for even storing multiple persons with ages to begin with. The real structured data is left to the programmer to implement and like wise with querying the structured data.

So what approach did I use?

I decided to store the value of the data as a JSON string and the name would be the table name. So for my project this data needed to be fetched from the web server and stored locally using the Web Storage.

I setup a PHP web server that returned a JSON response upon request via the client. The client did this using JQuery standard Ajax request functions. When I got this JSON response from the server which was basically a dump of a particular database table on the server in JSON format.

I stored it in local storage using the particular database table name for the name value. So in the persons / ages example this would be Person = Encoded JSON string of all data in the server Person table.

So now we have all of data for a particular table from the server stored in local storage. I just repeat this process for all the tables I want to store locally. We now have our data.

But what about querying this data?

Well JQuery does not come, out of the box anyway, with functions for querying JSON data. So a developer might think, I know, I will decode the JSON string and write my own functions for querying this data, possibly by putting the data into foreach loops and returning the records when the data matches the developers expectations. Well that is one way to do it however I am not sure how efficient that would be.

Looping through lots of data just to find single records cannot be very efficient however this is JQuery (Javascript) we are talking about, it is not as if it was designed to do this kind of thing anyway, plus Javascript processing power is becoming more powerful as a whole now in modern day browsers.

It is still not a server side language though and does not query a real database like MySQL which was designed for efficiency, but this is not expected of Javascript anyway.

I do not want to talk about efficiency too much as it is easy to slate querying data this way when a lot of internal libraries might just be doing the same thing under the hood anyway. I cannot imagine there being another way to do it with standard Javascript / JQuery when it comes to the rock bottom.

Anyways enough talk of how querying the decoded JSON data might be handled at a lower level. We as developers have libraries for this kind of thing and luckily someone has written a plugin to do this task anyway called json-sql. Here is the link to the library …

www.trentrichardson.com/jsonsql/

The json-sql library allows the developer to query a JSON string using SQL like syntax. Admittedly it only includes a very small subset of the standard SQL functions but it is most certainly good enough for most purposes, selecting, ordering and where clause. So I used this library to query my JSON data.

So that is pretty much it, the project has a web server which sends JSON responses to the client, data is stored in JSON strings in the client local Web Storage and the data is queried using the json-sql library before being displayed to the user.

This is the way I implemented a server / client, DB synchronized, local storage web software application.

Been using PhoneGap

On one of the latest projects I have been working on the client required that I build a software application that would run natively on multiple phone devices, IOS, Android, Windows and Blackberry.

The client required me to build a native software application for each of these devices because the client wanted to publish the software to the Android and IOS app stores and also because the client wanted the software to be able to run without an internet connection.

Up to yet I have mainly built responsive web applications that can run on a mobile phone through the phones web browser. Due to the client specific requirements this software application could not be done in the same way. If publishing to the app store and running without an internet connection was not a requirement, a web based responsive web application could have still been a viable option.

For previous software applications the responsive web application made sense because those projects where really web based systems that needed the convenience of running through a mobile phone and not a pure phone software application with the specific intent to sell the software as a phone application.

So what is PhoneGap? PhoneGap is a framework that allows developers to construct phone software applications using HTML, CSS and Javascript. After the developer has developed the software application using these languages PhoneGap can then build the phone software to run on multiple phone platforms i.e. IOS, Android plus more.

www.phonegap.com

Obviously this saves tons of time as without PhoneGap or similar frameworks developers would have to construct a software application for each platform. Another advantage of PhoneGap is that it allows web developers to build phone software applications in languages they are use to.

Although PhoneGap has made development easier, one of the drawbacks I have noticed with it is that developers still need to install each SDK. An SDK is a toolkit that allows the compiling of the software application to the phones native device. An SDK has to be installed for each device you want the software application to compile to. Installing each SDK can be a real pain in its own right. Most of the SDKs are installed through the command line and in some cases certain dependencies are required.

Another drawback I have noticed with PhoneGap is that the documentation is not very good. It seems like it is good when you are reading it on the main site or when you are reading it on online tutorials. But then you discover that the documentation you where reading is old and does not work for your version or even that it just simply does not work without you having to do something else which was not mentioned in the documentation. A simple task can look simple when reading the documentation, you do everything the documentation says only to discover it does not work in the way you want it to or even does not work at all.

You must be aware of what type of application you are trying to build and do not try to go beyond its limits when the native coding platform would have been a better choice. PhoneGap seems to be able to do everything the native platforms can do, but in practice it is different or rather you may try to do it but it might have been easier just to code it using the native languages, platforms and tools.

Update: PhoneGap does actually include an online builder which allows you to compile to IOS, Android and Windows without having to install the SDKs locally.

Web programming language to learn first

The other day a friend asked if his nephew could borrow one of my programming books as he wants to start learning programming. I was asked which I would recommend for a beginner and this got me thinking. Which programming language did I learn first? What would be good for a beginner?

Well apart from a brief go at C# whilst at University the first programming language I learned was HTML. Yes I know it is a mark up language and not really a programming language but for the purpose of this article we will class it as one.

So here are my recommendations at the time of writing this article. I would recommend learning HTML5 first and at the same time CSS3. The reason for this is that in my opinion they are easier to learn than a programming language and as a beginner the would be programmer is going to want to keep momentum up and learning something too difficult will bring discouragement.

Learning these two languages are basically a prerequisite to the other languages because even if you where to program a web application part of that will involve creating a user interface and that is generally done in HTML5 and CSS3.

When programming in HTML5 and CSS3 the beginner programmer can see practical results in very little time, neat and cool user interfaces can be created bit by bit without any real complexity and to see these bit by bit changes to the program all the programmer has to do is refresh the web browser.

Also HTML5 and CSS3 only require a web browser and a text editor to get started with. Programming languages generally require installing and configuring applications to get them working.

After learning HTML5 and CSS3 the programmer can move onto something more challenging and what many would consider a real programming language. The next step I recommend is learning Javascript, this is not Java, Javascript and Java are two different languages so do not get them confused.

Learning Javascript will require a whole new paradigm as to what was previously learned in the HTML5 and CSS3. Start with learning the basics, variables, conditions, loops then move onto the more advanced topics like functions and classes. Like HTML5 and CSS3, Javascript only requires the use of a modern day web browser to work and you can get pretty much up and running straight away.

After learning Javascript I would move onto learning PHP and MySQL. This will require you setting up a web server and installing PHP. Certain programs like WAMP / MAMP / LAMP can help with this and give you everything you need to get up and running.

Use the skills you have learned in Javascript and bring them over to PHP. In terms of the paradigm they are similar except PHP is a server side language and Javascript is a client side language. If you can get your head around that everything will be fine, I would not say the jump from Javascript to PHP will be as difficult as the previous jump from HTML5 and CSS3 to Javascript.

You will need to learn MySQL to talk to the database and pull data into your PHP web application. After you have learned these 5 languages the sky is the limit. You will be able to produce full blown web applications.

After learning these five languages I would also recommend learning a PHP framework and a Javascript framework as they will make your programming even easier. For quite some time I coded in standard PHP and Javascript because I did not trust moving to a framework but I can definitely it was one of the best programming decisions I ever made.

You will produce better quality web applications in less time. Also whilst the learning the Javascript framework you will most likely run into Ajax, which is another good concept to learn.

Well that is my recommendations for anyone starting out in the web programming world, give it a go.