- Nine inch nails! (Checked in at Rod Laver Arena) facebook.com/pages/Rod-Lava… 7 months ago
- She went on a journey under the back porch instagram.com/p/jS6howhOC9/ 9 months ago
- To Lunch or Not to Lunch, that is the bastardization of a Shakespeare quote. 1 year ago
- Huh? sdrv.ms/TNKDYF 1 year ago
- Ok, so I had to post that in order to download a font. I haven't been hacked or anything. Carry on, nothing to see here. 2 years ago
One year since the last proper post; here we go yet again. I’ve imported everything from my tumblr blog, since I feel like it was more restrictive than what I wanted. Tumblr blogs seem to work great for when you have a very specific topic in mind, but it’s minimalism just doesn’t feel right for the random musings of greatly varying length tht are my forté.
Anyway, enough of this meandering diatribe. I could write about how I intend to maintain this blog properly from now on (for real this time I promise etc) but that idea bores me. For now I will forego any commitments vis a vis this blog, and say simply that I might submit here a few ideas I have floating around in the electric soup of my thoughts, including some short stories and perhaps a few movie reviews.
Until then, well, whatever.
So finally I am back. Where I was is a story for another day. What is important, however, is that I have a tasty new write-up for you.
Rather than try and fill in the gap from where I left off, I have decided to jump into something a little more advanced and conceptual. Keep in mind that to get anything out of this, you will need to have a basic grasp of what classes and objects are.
Inheritance and polymorphism are two of those ideas that I found a little confusing at first. With polymorphism especially, I found myself scratching my head trying to figure out how it would be useful. It is actually one of the cornerstones of object oriented design, and incredibly handy once you get your head around it.
Ok, so what are these things? I will start off with inheritance. Imagine that you are a car manufacturer, and you want to create a number of classes that represent your various models of cars. You have in your lineup a hatchback, a sedan and a 4×4. You could simply code all of them separately and write code for tooting the horn, have instance variables for the fuel level etc., but that would be replicating heaps of code. What if you wanted to change the speed at which the windscreen wipers go across your entire range? You would have to re-write, and debug three different versions of the same code. Sure, you could just copy and paste, but that is just as error prone, and counter productive.
What you really want is to write the windscreen wiper code once, and maintain just one copy of it. What mechanism can you use to do this? You guessed it: Inheritance. Collect your prize at the box office.
Inheritance allows you to have a Base Class with all common code in it that other classes can Derive From. Continuing with the car analogy, you would put your windscreen wiper code, along with your fuel level code and your headlight code along with any other code common amongst your cars in the base class. From there you would have derived classes representing your hatchback, sedan and 4×4.
Inheritance also has an important concept of overriding. This is where you have functionality in your base class that you want to replace in the derived class. In the case of your car, your drive chain code would be common between your hatchback and your sedan, but the 4×4 will be different. Rather than copy and paste between the sedan and the hatchback, you would put that functionality in the base class and override it in your 4×4 class.
So, lets draw a basic class hierarchy:
- Car (Base Class)
The Car class might look a little something like this:
- useWipers (method)
- toggleHeadlights (method)
- getFuelLevel (method)
- changeGear (method)
- odometer (variable)
The 4×4, which has a different gearbox, might look like this:
- 4×4 inherits from Car
- use4WheelDrive (method)
- use2WheelDrive (method)
- changeGears (method, overridden)
The 4×4 will still have the useWipers method from the Car class, along with any other methods declared there, but it rewrites the changeGears method to suit itself. Also, it add functionality that is unique to itself, the use2WheelDrive and the use4WheelDrive methods.
This is an incredibly efficient way of writing code without duplication and redundancy. But what about the code supporting such things? Code that interacts would have to be able to deal with three different classes as though they were totally unrelated.
Or would it? Of course not, and this is where polymorphism comes into play.
Polymorphism sounds incredibly complicated, or like what happens when a parrot turns into a Power Ranger. It actually isn’t. All it really is is a way for you to classes derived from a base class as that base class. If we have some code that calls changeGears, we don’t want to write it for every different type of car. What we can do is instantiate a 4×4 class and place that object into a variable that is of the Car type, and we can then use all methods in the base class. what happens when you have overridden a method? Well, you get the overridden method!
One minor detail which I was trying to avoid was the concept of virtual methods. I’m still going to avoid it, but you can look it up on wikipedia for a nice definition. I don’t want to get into it because it is closely related to the language in which you are coding. In Java for example, you don’t even have to think about it, but in C++ you must explicitly deal with it in your code.
Anyway, I hope that this was of some use to somebody.
I went a little overboard with the post processing on this one. Has a bit of a “with teeth” feel to it.
Yes, I am alive.
If you have been checking back here for more of my programming tutorials, thanks. There should be some more coming soon. Admittedly, I have been a bit slack, and on top of that I found out the hard way that pickled onions can indeed go off, so I am about a week behind my release schedule.
In the first edition in this series, I explained the concepts behind variables, and how they can be used to store values that your program might need to use. In this article, I shall endeavour to explain one of the things variables can be used for.
When writing a program, you need to look at variables and make a decision based upon the values of those variables. Most programming languages provide you with constructs that allow you to jump to a different part of your program based upon the value of a particular variable. The most common one is what is known as the ‘If’ statement. An If statement more often than not has an associated “Else” statement after it. In pseudocode, a simple If statement looks like this:
if Some Condition then Do Something else Do Something else end if
Let’s break that down. The If bit is computing “Some Condition”. “Some Condition” is what’s called an expression. An expression is a piece of code that equates to either true or false. For example, say we have a variable with the current temperature in it, our condition could be something along the lines of (currentTemperature = 22). That is, if the current value is 22, then this expression is true, otherwise it is false. An If statement will look at the value of an expression and “Do Something” if it is true, otherwise it will “Do Something Else”.
Note that although with (currentTemperature = 22 ) I am using the equals (=) operator it has a different meaning here. Here it is used to test whether or not currentTemperature’s value is equal to that of the value 22. It is testing equality. Previously the = operator was used for assignment, to assign a value to a variable. These two concepts are quite different. Think of it as the difference between flipping a switch and checking to see what position the switch is in. Many languages actually have different operators for these operations, as the confusion between the two can cause serious problems in complicated programs.
Let’s make a more interesting example. Imagine you want to write a program that controls a robotic bartender that serves alcohol responsibly. For the sake of this example, “responsible” means only serving 4 drinks to any one person. We will keeps tabs of how many drinks the current person has been served in a variable called “numberOfDrinksServed”. If “numberOfDrinksServed” is less than 4, we will execute the code to serve another drink, otherwise if it is 4 or greater, we will execute the code to call a taxi.
if numberOfDrinksServed < 4 then Serve Beer numberOfDrinksServed = numberOfDrinksServed + 1 else Call taxi end if
Seems fairly straight forward, doesn’t it. Our expression is numberOfDrinksServed < 4. The < operator, as many will recognise, simply means “is less than”. If numberOfDrinksServed is less than 4, we will serve another beer, and then increment numberOfDrinksServed by one. Otherwise, we will call a taxi.
Let’s change things up a bit. Say we can make our robot bar tender a little less harsh by seeing if we have already served 4 drinks, and if we have, serve a light beer. We will still call a taxi though if the total number of drinks is above 4, or serve a normal beer if we are below 4. In pseudocode:
if numberOfDrinksServed < 4 then Serve Beer numberOfDrinksServed = numberOfDrinksServed + 1 else if numberOfDrinksServed = 4 then Serve Light Beer numberOfDrinksServed = numberOfDrinksServed + 1 else Call taxi end if
Now we will step through this example. When we get to the first if statement, we check whether numberOfDrinksServed < 4. If it is, then we serve another beer, and increment numberOfDrinksServed by one. Then we exit the if statement. IE, we don’t bother checking any of the other conditions.
If numberOfDrinksServed is 4, the first statement is false, since we are checking if it is less than 4. We then check “else if” part, and see that yes, numberOfDrinksServed = 4, so we take pity on our customer and serve a light beer, and also increment numberOfDrinksServed.
If we set numberOfDrinksServed to 5, then the first two expressions equate to false, so we go with the default option of calling a taxi.
One neat thing to note about if statements is that you can have any number of “else if” statements in the middle of your block of code, so you can make quite complicated decisions. “If” statements can also be nested within each other. You would do this if you only want to check something after something else has been checked.
As an example, for our robot bartender to be responsible, it needs to check if the customer is 18 or over. If they are less than 18, throw them out. We don’t need to check how many drinks our under-18 customer has had, because we wont be serving any beer to them anyway so we will nest our previous example inside of another if statement that checks the customers age. We will create a variable customersAge to represent their age.
if customersAge < 18 then Tell bouncer robot to throw customer out else if numberOfDrinksServed < 4 then Serve Beer numberOfDrinksServed = numberOfDrinksServed + 1 else if numberOfDrinksServed = 4 then Serve Light Beer numberOfDrinksServed = numberOfDrinksServed + 1 else Call taxi end if
It should be pretty easy to follow this. Note that this is not the only way to write this, but generally you want any ‘Dealbreaker’ conditions to contain any of the conditions which concentrate on finer details. This is primarily because it is closer to how people decide on things. We look at the most critical issue, and then based upon that, use more detailed analysis to make our decisions. If we just sequentially went through all of the finer details we would be wasting our time when we could have done one check to come to our conclusion.
Well, hopefully this has been a good explanation of the basic ideas behind if statements. As always, I would love to hear your opinions on this article.
I figured that perhaps if I have a specific topic to blog about I might be able to sustain my interest in writing. When your subject matter is Everything it can be hard to determine exactly what you should post about.
Which is why I’m planning on writing a series of basic concepts of programming posts aimed at people just getting into coding. I chose to stay on the conceptual side to try and stay language agnostic. This is because I am hoping to help people in my programming class (not specifically, just people at that level), and my current class’ language of choice is VB.net, of which I am not really a fan. So my examples will all be in pseudocode.
But enough waffling, lets get into it.
What is a variable? A variable is something you can declare in your program to hold something. A variable has three things: a name, a value, and a type.
What’s in a name?
A variables name is what you use to access it’s value. Say your program needs to store the current temperature. You might create a variable called ‘currentTemperature’. What you actually call a variable is up to you, although it is good practise to call it something that represents it’s value. You could have called it ‘sheep’, or ‘kitteh’, or ‘flimFlamManitoba’, but once you have a number of variables, your program could get incredibly confusing.
Also, giving it a short name, such as ‘cT’ or even just ‘t’ might seem like a good idea at the time, and may work just fine if your program is fairly small, and you are the only one working on it, but if someone else has to maintain your code or even if you have to revisit your code some time in the future, it will be a right royal pain in the arse to figure out what t, or q, or z represent.
One final note, you may have noticed the capitalisation I used. Again, it’s up to you, but I personally like to name multi-word variables in what is called Camel Case. This is where you leave the first word lowercase, and the words after that capitalised, for example ‘camelCase’, or ‘slumDogMillionaire’. I won’t go into why the first word is not capitalised, and when it is recommended to be uppercase, because it is beyond the scope of this article. I will cover this later when I talk about Object Oriented Programming.
A variables ‘Value’ is the important bit. To set the value of a variable is to assign it a value. That value gets stored somewhere in memory, and you don’t have to care where because all you need is the name of your variable. Say you are writing a program that gets the current temperature from a temperature sensor attached to your computer. You would declare a variable, perhaps called ‘currentTemperature’, and you can assign the value you got from the sensor, which is for example 22, to your variable. Fairly simple stuff. I’ll give you an example in pseudocode:
currentTemperature = 22
You can also assign a value to a variable from another variable. Let’s say that we now want to move currentTemperature to a variable called oldTemperature (Which, for the sake of this example we have already declared).
oldTemperature = currentTemperature
Really pretty logical. However, there is one final hurdle to variables that can be the trickiest to get ones head around, and that is:
A type, when talking about variables, is talking about the kind of data you can put into a variable.
There are a number of types of data. 22 is example of an Integer. Put simply, an integer is characterised by a number without any decimal points.
There are also floating point numbers, or floats for short, that do have a decimal point. For example, 22.93 is a float. 22 could also be a float. All integers can be floats, but not the other way around unless you either round it to the nearest integer or strip of everything after the decimal point.
A string is a series of values that normally represent text. For example, “Hello world” is a string. Note: Strings are usually shown in double or single quotes to show where the string begins and ends.
Why does this matter? When it comes to computer languages, they can be split into two (for the most part) categories in regards to type: Statically typed and dynamically typed.
If you look at the meaning of the word ‘dynamic’, you will see that it means something that is undergoing change or motion. This means for our variable, currentTemperature that the type of value we put in there could me a string, a float or anything really.
Statically typed languages on the other hand, require you to state what kind of data you put into a variable when you declare it, and stick to it. For example, with our previous example, currentTemperature might be declared as follows:
declare currentTemperature as Integer
Why should we have to manage types ourselves? A few reasons. For the computer, an integer is stored in memory in a different way to a string, or a float. Another reason is that different variables take up more memory than others, and for greater efficiency, you should pick the type that is sufficient for any values you expect to put in it, but no bigger. A few bytes here or there is usually not of any importance in simple programs, but in very complex programs, it can have a huge impact on how much memory your program requires.
So what if you need to change from one type to another if you use a statically typed language? In a dynamically typed language, if you need to convert, or cast from one type to another, the computer language handles all of that for you in the background without you having to worry about it. In a statically typed language, you need to manually convert these values, usually using built in functions that the language provides.
After that, it sounds like you would never want to use a statically typed language unless you were a masochist. It’s true, statically typed languages can be a pain. The programmer has to do a bit more work with static variables as opposed to dynamic variables. However, there is a catch. Even though they are generally easier to work with, and often allow faster development, dynamically typed languages give you enough rope to shoot yourself in the foot with, so to speak.
Well, I’ve covered many more topics here than I wanted to, in greater detail than I would have preferred, but hey, shit happens. Tune in next time for the next semi-exciting instalment in this series, which will be based on a topic I have yet to decide on.
So why start again, considering the last blog I attempted to run simply got boring to myself and I’m certain to everyone else as well. To be honest, I’m not entirely sure. Perhaps it is because my life has changed direction somewhat since I started that blog all that time ago, maybe it’s my undiagnosed a.d.d. Who knows.
The point is, more now than before, I want to have a central place for my ‘stuff’ that I create. My output if you will, be it a neurotic rant on something only I care about, some photos I’ve taken, or a song I made.
Why put these things out there? I can’t answer that either, not even with some witty response such as I want to sue anyone who downloads my stuff for copyright infringement.
My creativity is not something I have no control of. It seems to come and go as it likes, taking any for that it likes. Right now I feel like I’m about to have a creative period, but who knows, It could just be what I had for lunch.
Last week I had some time off, and decided to do something a little different to keep myself amused. I had always planned on going out and taking a lot of photographs, but I figured I might make a coffee-tale book of some of the more interesting ones. And here it is, free for your downloading pleasure. I humbly present, One Week Holiday (17mb PDF) or One Week Holiday (1.6mb PDF)
I encourage you to check out the small version, which is still quite good quality, but if you wish to print it (Why the hell you would I’m not sure, but hey, whatever floats your boat) download the big one.