Thursday 27 January 2011

Frameworks, Toolkits and Libraries

Frameworks, Toolkits & Libraries.

I had an interesting question from some inquisitive 3rd years today and it was on the verge of dominating my afternoon so, since I spent a fair bit of my afternoon having a good think about it, I thought it only fair to share my views. NOTE: These are my views, not gospel. I have found that, especially in computing, terminology is easily (and incorrectly) substituted for other words that sound similar but are in fact different.

I was asked "What is the difference between a toolkit and library?" I answered, to the best of my knowledge and, of course within a programming context "A library is a set of related but non-linked programming classes. Now, just to be clear I mean non-linked from a programming perspective". The classes may be related but the bottom line is they do not rely on other classes to perform their function.

Libraries

A library. How to explain a library. Well, imagine a real-life library. If you walked into Dundee Central Library you'd find tens of thousands of books on thousands of subjects. A computer "library" is no different. Putting libraries into a programming context, a single book could be related to a single class

public class TreasureIsland
{
}

Again, if we go back to the real-life library scenario, most books are organised into subject area and, using the subject area is probably more similar to what you would find in a computer programming library.

Perhaps an example might explain it better and for this example I will choose the subject area of mathematics (don't worry, very simple mathematics!). I might have a class that looks like this (if you're still on the real-life library, this would be a book that describes how to add and subtract):


public static class MyMathClass
{
public static int Add(int num1, int num2)
{
int addedNum = num1 + num2;
return addedNum;
}
public static int Subtract(int num1,int num2)
{
int subtractedNum = num1 - num2;
return subtractedNum;
}
}

OK, so you may (or may not have) come across the static keyword before but now seems like a good time to briefly go over what it means and what it actually does.

The static keyword indicates that the class or the method does not need to be instantiated. In the case above, the class is MyMathClass and the methods are Add and Subtract. Now, I know the word instantiate sends shivers down the spines of programming students but if you don't know the difference have a look below. I will use a JME example because I know most of the students reading this blog are my JME students and if you're looking for a point of reference I am lifting this straight out of my Skeleton example.

...
public void MoveToGameScreen()
{
GameScreen myGameScreen=new GameScreen(this);
display.setCurrent(gameScreen);
}
...

Now, in the above snippet, the variable name is called myGameScreen and the class it relates to is called GameScreen but it is not instantiated until the = new GameScreen(this). We can tell a few things just by looking at the code.

1) We know that GameScreen is an instantiated class (and therefore NOT a static class) because we use the new keyword to create an instance of the GameScreen class called myGameScreen.

2) We know that GameScreen is an instantiated class because we use the new keyword and we supply it with a parameter. In this example we supply (this) which, if you look through your skeleton program is the midlet. Static classes cannot be instantiated and therefore cannot accept constructor parameters.

If you don't believe me (or still not quite grasping it) look at the original code snippet with MyMathClass. There is no constructor, only the class declaration

public class MyMathClass

and two methods

public static int Add(int num1, int num2)
{
int addedNum = num1 + num2;
return addedNum;
}
public static int Subtract(int num1,int num2)
{
int subtractedNum = num1 - num2;
return subtractedNum;
}

In fact, if you were to try to add a constructor to this class it would give you a compiler error telling you it is impossible to add a constructor to a static class because, well, that's the point of a static class.

Just before I move back to the original point of this post and just in case some are still struggling with instantiated classes (it's important, I assure you) I want you to have a think about any time you have had to put a debugging println() statement in your JME program.

System.out.Println("Prints to the console");

Look through any of your JME programs, at no point do you ever have to create a System class by typing System = new System(); That's because System is a static class provided by JME.

A library is (usually) a list of classes that provide functionality for very specific subjects. I say usually because there is no law on creating a programming library but you will find, through your own exploration of libraries available, this is a standard they try to follow.
NOTE: It isn't always possible to follow but if we considered the above example and wanted to add the ability to divide and then subtract a number to the MyMathClass above we could do something like

public static MyMathClass
{
.....
public static int DivideAndSubtract(int num1, int num2, int num3)
{
int divide = num1/num2;
int result = MyMathClass.Subtract(divide, num3);
return result;
}
.....
}

See how we take num1 and num2, divide them and then call the method Subtract. No need to instantiate the MyMathClass, we just call the subtract method using MyMathClass.Subtract(divide, num3);

So, back to the original point, a library is a set of routines that accomplishes many similar tasks that can be programmed independently of each other.

Now that we're done with libraries you'll be happy to hear that there wont be many code examples from here on in simply because toolkits and frameworks are too large to programmatically show.

Toolkits

Again, imagine a toolkit. You could imagine any sort of toolkit I suppose so let me contextualise it into a real-life scenario. Imagine being a carpenter, owning many planks of wood, a hammer and some nails. Now, I could ask you to build me a house but, given the toolkit you have, I'd be better off asking you for a kitchen unit. It should be noted that a carpenter would have had (and still would) have access to large amounts of study material in order to build a kitchen unit, like a library of books or the knowledge of the head woodworker and therefore it is safe to assume that most toolkits will contain libraries of some sort, one or many.

The challenge with toolkits is the that the onus is on the user to use their tools in the correct way. If you attempt to put a nail into your hammer your going to have issues. As a carpenter there would be an expectation that you understood why you hammer the nail and not the other way around! Similarly, for a programmer, a toolkit will provide you with a greater set of options, a greater freedom in how you construct your program. "Yes, you can build a kitchen unit but only if you tell me the correct way to do it. I mean, you're setting my angles wrong to my adjoining unit but if you want the dishwasher in the living room then I wont argue . . .

From a personal programming point of view, I have, on a few occasions, turned large libraries into small toolkits. For my PhD project, I did not have the luxury of a GUI manager that you might find in Windows Forms in C# or even Java layouts, I had to create all my user interface components by myself.

At first it was fine, but once I realised I had added 21 buttons across the top of my screen they started to act strange. They wouldn't automatically reposition themselves depending on my screen width, I had to constantly change the screen position of my buttons in a vain attempt to keep them on screen.

Originally I had my buttons positioned based on what my GUI library told me it should be and it was based on a List of how many buttons I had on screen. I soon realised that I needed to expand it to encompass more than just the number of buttons I had on screen, I needed to account for screen width. It wasn't just my screen width I had to account for either. Once I started testing on other machines I realised not everyone has access to a full HD-compatible screen resolution. So, rather than extend my already large library of static classes and methods I enclosed all the functionality I wanted into a custom programmed ButtonManager. My ButtonManager was great, all I needed to do was something similar to

public void Init()
{
Button startButton = new Button("Start",upTexture,downTexture,"Up",Down");
AddButtonToGUI(startButton);
}

I completely understand that means very little to anyone, since it is my program, but I can give you an example of what it looked like before I created my ButtonManager Toolkit.

Button startButton = newButton();
startButton.SetUpTexture(upTexture);
startButton.SetDownTexture(downTexture);
startButton.SetUpTextureTooltip("Up");
startButton.SetDownTextureTooltip("Down");
Vector2 tempVec = startButton.GetParentPosition();
this.theGUI.AddButton(startButton, tempVec);

I know you can't see the underlying code for either example but surely we can agree that placing a button on screen is much more code friendly in the first example than the second.

I guess the bottom line is that once you can no longer call static methods of your library that will solve your task it's time to create a cleaner solution and that solution would be called a toolkit. While creating a toolkit may sound daunting it's simply the ability to identify when one type of programming solution is no longer sufficient for your needs and upgrading to a new one.

Frameworks

I am going to spend one paragraph on frameworks and one only simply because I have a vague understanding of your coursework and can tell you that a "true" framework is not something you will ever be asked to develop at any university. Developing a full toolkit would usually involve a team of programmers, with UML diagrams and wage of 30k+, well, frameworks are more complicated. Not more complicated to understand, just more complicated to implement. In the previous examples of toolkits I used a carpenter analogy so I will continue on that train of thought with the framework. A "real-life" toolkit might allow you to build a kitchen unit, it would probably let you build a room. Using this scenario, a framework would be more like the architectural plans. "This framework says that these are your specifications". Your house cannot be bigger than x,y,z. Your house cannot have more than x amount of rooms, these rooms cannot have ceiling's higher than y amount . . . . You get the idea. A framework, in actual fact, doesn't tell you what you can do, it tells you what you can't do. For anyone who thinks it's an easy task, try it. Write a simple class that doesn't allow you to send negative numbers to the console screen using System.out.println(). Now imagine doing that for every possible data type as well as user defined classes. To give some examples of the enormity of task in developing a framework I list a few well-known frameworks below.
  • The .NET language is a framework
  • The PHP language is a framework
  • and HTML is a framework.

Closing Thoughts

The first thing that comes to mind is the vast vocabulary that programmers now use to describe programming features. Mixing and matching the words "libraries", "toolkits" and "frameworks" as a form of verbal description is not a sin, it doesn't make you a bad programmer and doesn't mean that you mis-understand the meaning of certain programming "catchphrases". The truth is they are "catchphrases" and "buzzwords" and as long as you communicate what you want done or you can identify what a client communicates to you, you should be in a better position to deal with what is being asked of you. In fact, there has never been (and probably never will be) an International definition for these terms. The important thing is that YOU, the programmer, can define between what they mean. I am simply trying to give you something for your brain to chew on.

Finally, and probably most importantly, certainly for Abertay students. I am not the person who sets your coursework. In this post I have been talking about the 3rd year, 2nd semester module Allan Milne has set. Any pertinent questions about your coursework should be directed to him. Not only will Allan know what is expected of you better than I will, Allan has a vast amount of knowledge that will not be rivalled within the University. I would encourage anyone with questions to make an appointment to discuss those questions with him. From my under-graduate experience he was always one of the most approachable and thoroughly decent lecturers.

Anyone interested in further reading should direct themselves to the links below.

Chris

Yes, a wiki link, yuk
Stack Overflow link.
Perhaps a slightly biased story but some good comments at the bottom
Specifically the post by Stanimir Stoyanov

No comments: