Saturday, June 8, 2013

Learning Java Part 0

This is going to be part 0 of a three part series. I hope everyone learns why I chose to start with 0 rather than 1 by the end of the series.

Java. It's everywhere. In phones, robots, and toasters. Most of the challenge with learning a language when you've never programmed before is the underlying logic and the strange subtleties of the syntax. That's why I'm not going to teach general syntax first. Instead, I'm just going to go over a few language constructs that are commonly seen in nearly any language.

Variable - something that stores a piece of data. Variables often come with types that tell the computer what kind of data that you are storing. Some of the types include

int - for most intents and purposes, treat this just like you would if it were a regular integer
double - it can store values with a decimal point. 123 or 12.3
boolean - only has two values: true or false
Object - stores a object reference. We will look at objects more in depth on another day

These are the four data types that you will work with the most when programming. There are a lot more that I have not mentioned but they are not hard to learn. Generally speaking, you can do math on these operators such as + - * /. You can also find the remainder using %, which you will hear some people call mod. The order of these operations follows PEMDAS, so use parenthesis if you are unsure that your ordering is correct

Here are a few examples of variables...

int a; // The '//' tells the computer to ignore what goes after it on that particular line. This way you can comment your code.
/* both of these are commenting styles
This one can span multiple lines */
double b = 1.7; // we can also set values to these variables
boolean c;
Object d;// Notice the semi-colon at the end of each line, this tells the computer where one instruction ends and where the next one begins

Great, now that we have figured out variables, lets go on to two basic constructs: ifs and loops

Before we can start talking about ifs and loops, there are a few things we need to clear away first. Remember how we talked about boolean? Well, it turns out that boolean is actually pretty important. There are times when we want to test whether something is true or false, such as testing if int a is equal to 5.
The most important boolean operators you will need to know are these:

Equals ==, it has two equal signs because the single equal sign = is used for setting a variable.
Not equals !=
Less than a < b This returns true if a is less than b
Greater than a > b This returns true if a is greater than b
Greater than or equal to >= same idea
Less than or equal to <= you get the point.

AND &&, this returns true if both conditions are true
OR || this returns true as long as either condition is true
NOT ! changes from true to false and vice versa

Here's how if's work.
if (something here is true) {run this code}

Sometimes there might be else attached. This is optional and run when the condition is false
if (something here is true) {run this code} else {run this code instead}

Here are some examples:

 int j = 2;
 if (j == 3) {
 // this code will not get run because j is not equal to 3
 } else {
  // this code will run!
 }
 // carry on with rest of program
 ------------------------------
 int d = 5;
 if (d == 5) {
 // d is equal to 5, so this code runs!
 }
 // carry on with rest of program

Hopefully you can now see how easy this is. Lets move on to loops..

There are two kinds of loops that I want you to be familar with: for and while use them when you have code that repeats itself

I've gotten addicted to this song


Wednesday, May 29, 2013

Why Recursion is Bad (shorter version)

Recursion is where you try to attempt a problem, give up, and let it try again...

Sunday, May 19, 2013

Recursion is bad

Before crazy people start sending me threatening emails, I agree there is a time and place where recursion is probably ideal (Quicksort?) That being said, its better to use other options where possible. (Please do not do binary tree problems iteratively. Ok Carry on)

Repeat after me. I will never use recursion unless there is no better alternative. There are times when recursion is a necessary evil but it's an evil that should be avoided wherever possible.

Recursion eats up memory on the call stack. Every time a function is called there is going to be an overhead associated with saving register values into memory. This adds up fairly quickly when doing recursion. Since the compiler cannot really optimize recursion since it can't predict how many times it will recurse that also will contribute to the performance hit. Once recursion goes too far it causes Stack Overflow, and life just sucks.

There are people out there who call recursion 'beautiful'. I personally think loops make more sense since they don't seem as weird. Making recursive code requires a leap of faith assuming that your code is doing the correct thing its supposed to do even though your not done writing it yet. (What did I just say??)

Basically recursion is bad. But if you have to use it wash your hands afterwards. You can do recursive questions with your own stack, but I don't see the point.

Wednesday, May 1, 2013

How to type faster

Once of the most common questions i have ever gotten is: Ben how do you type so fast. I generally make a joke that most of what I write is full of typos (which is true) but there are a few ways that you can type faster

  1. Keep in mind that you are trying to type faster. Often I type at only 30 wpm due to the fact that I am not really in a rush or trying to type faster. If you try to type faster, generally you will be able to type faster.
  2. Type hard to reach keys properly. It wasn't until a few years ago before I realized how much faster I could type if I simply hit the period and comma keys with my fingers in the home position rather than have my hand move down to the lower part of the keyboard and hit the key that I wanted. Other keys that are commonly cheated are the z and the x at the bottom left of the keyboard. Do those in the same manner. It will take some time to get used to, but it really is worth it.
  3. Practice. For the people who are reading this blog, this shouldn't be an issue for you. You should have a lot of opportunities to practice your typing. Type in search queries as fast as you can. Write anything - emails, essays whatever using proper typing techniques.
  4. Play some games! My personal favorite is typingtest.com They have games and typing tests alike. I usually do some typing tests from time to time to make sure that my typing speeds have not gone down and then I play a few games (my favorite is the ones with the bubbles)
Over and out

Ben

Friday, April 26, 2013

When days are insanely busy

As the number of things to do each day increases faster than you can hope to get them done, the only hope is that better time organization will help you survive those days. Unfortunatly, there is only a limited amount of time organization possible. At a certain point, if there isn't any time, then there isn't any time.

Here are some things to do if organizing time does not work
  • Push it back. If it does not need to be done immediately (and there are other tasks with greater priority) then there is no point in getting it done early.
  • Use google calendar. Place every task you need to do as a calendar event. Set a time for each task and it's approximate length. To avoid a domino effect if one task takes longer than expected, give every task slightly more time than you hope to complete it with. Follow your calendar as closely as possible. Try to multitask. For example, if you have to read, do it on the bus rather than at a desk, when you could be working on something that might require a desk.
  • Delegate. There are other people out there. Don't do the work of 5 people on your own.
  • Double check. Are you sure you have to do all of this? Make sure that there was no miscommunication before wasting hours of your precious time.

Friday, April 19, 2013

April 2013

I have nothing to say.