WTF is Bubble Sort ?

Bubble sort is a type of sorting algorithm. Its not the best for everything but it does have its uses. It gets its name since it makes the largest values “bubble” to the top. Bubble sort does its job by using pair comparisons. So it takes the current value and compares it to it’s neighbor directly to the right.

Below you can see what I mean. We first take 5 and compare it 3, then ask the question “is 5 greater then 3 ? ” Then if it is, 5 switches places with 3 and if it isn’t then they stay in the same place, and move on to the next value.

BubbleSortEx1

Here you can see a non optimized implementation of it:

 

Questions People have Asked Me – Part 1

Below are some questions I was recently asked, with my answers. 

Please let me know if any of them are wrong, its a learning opportunity for me 🙂

What is the binary sort algorithm and how does it work? 

The binary sort algorithm (BSA) is used to effectively sort data. It works on the principle of continuously cutting the data set in half, until it finds what it is searching for. However, this algorithm only works on data sets that are already sorted. 

First the BSA checks the middle of the data set and compares the value it is searching for. If this value is the value it is searching for then it stops. However, if it is not equal, it checks if the value it found is bigger or smaller then the value it is trying to find. If the value is smaller, the BSA repeats the process on the left, whereas if the value is bigger it repeats the process on the right. This process is repeated multiple times until the value that the BSA is looking for is found. 

What is recursion and how is it used? 

Recursion in programming is when the program starts calling its self, from inside its self. This programming technique is usually used when a single large program can be solved in smaller parts and has a valid base case. Such as computing the Fibonacci Sequence or traversing a binary search tree. 

What is polymorphism and what is its purpose? 

Polymorphism is an aspect of Object-Oriented Programming where a “object” can take on many different forms, if all the forms are its children.  

Explain when you should use interfaces and when you should use abstract base classes. 

Both interfaces and abstract classes are a type of contract, in the class structures of a software application. Interfaces are a form of contract between two different entities, where you want to separate the functions from the implementations. This is done such that you existing application does not need to change much if a certain part of it is changed. This can be seen in the commonly used repository pattern, which is used to separate data access logic from the business logic. Where as abstract classes are a less extreme version of interfaces, where certain methods defined in it can have real implementations. This allows any child class that inherits from the abstract class to get those method implementations. The difference between the two can be further seen in how their child methods are derived. Since interfaces are “implemented”, where as abstract classes are “extended”. 

When should you use static methods and static variables? And when shouldn’t you use them. 

Static methods and variables can be used from a class without having to instantiate it. This is usually used when, you want to group a set of functionality or utility function together. An example of this is the “Math” class in JAVA, which gives the user all the math related function they need. You wouldn’t want to use them when you would be creating your inheritance-based class structures, most of the time. 

 

Write a SQL statement to create a table called “author” with the columns “id”, “name”, “age” (for MySQL or SQL Server). 

CREATE TABLE author ( 

id int NOT NULL AUTO_INCREMENT, 

name varchar(255) NOT NULL, 

age int, 

PRIMARY KEY (id) 

); 

Write a SQL statement to create a table called “book” with the columns “id”, “title”, “genre”, “author_id” (for MySQL or SQL Server). 

CREATE TABLE book ( 

id int NOT NULL AUTO_INCREMENT, 

title varchar(255) NOT NULL, 

genre varchar(255), 

author_id int, 

PRIMARY KEY (id), 

FOREIGN KEY (`author_id`) REFERENCES `author` (`id`) ON DELETE CASCADE); 

 

For the “author” and “book” tables created above, write a SQL statement to tell you the number of books each author has written, but only for authors who have written 2 or more books. The output should not show authors that have written only 1 book. The output should have the author’s name and the number of books they have written. 

SELECT author.name, COUNT(*) AS ‘# books’ FROM author, book WHERE author.id = book.author_id GROUP BY author.name HAVING COUNT(*) > 1; 

 

In databases, what are indexes used for and how to you decided how to use them effectively. 

Indexes in databases are used to speed up data retrieval. However, they come at the additional cost of space, and added complexity to database maintenance. They should only ever be used when the same type, or group of data is constantly being accessed. If the number of reads get even larger, there should also be some sort of caching layer the application queries, such that it doesn’t need to query the SQL database directly. 

 

What is the value of unit testing and what are some of your strategies for writing good unit tests? 

Unit testing is used to test the functionality of the different parts of an application. Its value lies in the fact that they make the programmer, test their code in a systematic way. And feeds into a workflow where tests are run before anything gets committed to the master branch. I think the best way to write a test case, is to write the test before writing the application logic, since it gets you thinking about what edge/special cases to consider. This is also known as the test-driven development approach. 

Getting a Job as a Developer in the Modern Age

I am gonna just  start making fun of stuff below:

I remember a time when simply listing “Java” as language on your resume, was a sure fire way to land a job … the good old days (1990s). Too bad I wasn’t around during the tech bubble. The days when simply adding “.com” to your company name would shoot up its stock price by 50%.

Now … now its early 2019. A time when no one cares if you can actually make stuff with code. No, people and large corporations realized that there is something much more important. And if you are a developer reading this, you already know what that “something” is. Its writing little programs in front of people you don’t know, to do absolutely nothing useful. Yes that’s right, they don’t actually care if can do the job. They just care if you can find a loop in a linked list, or print out a binary tree in descending order. Cause everyone knows that it is exactly what you need when making your web page, or UI, or implementing  business logic (cause everyone knows that every simple piece of logic needs a tree data structure in there somewhere right ?).

Now I am gonna stop making fun of stuff.

I write to let “people” (who these people are, I will never really know) that I am gonna be  starting a series of set of posts talking about all the stuff, I am currently doing to work for someone else.

 

 

 

 

WTF is are Stacks, Queues, and Deques ?

Stack

A stack is a stack of books, or a stack of sandbags or a stack of elephants or even a stack of unicorns. Basically a stack is anything that follows LIFO (Last In First Out), which means that if the last thing you put onto the stack is the first thing you have to take out, then its a stack.

Lets say you build a tower of blocks, that tower is a stack. Why ? Cause to get to the block at the base you need to take off all the other blocks. Below you will see a visual example of what I am talking about.

stackexample

 

Queue

A Queue in programming is the same thing as the Queue while in line to buy food, or go to a movie, or get into a night club, its a first come first serve basis. Meaning the first thing to get out of a Queue was the first thing to go into the Queue.

You can also think of Queues as pipes that transport things. In the case of plumbing, the steel pipe and the water moving through it the Queue. Since the water that first enters the pipe is the water that first leaves the pipe.

Below is a diagram to show what I am talking about:

queuesexample

 

Deques

Deques are like the cooler older brother of the Queue, since it lets things flow in more then one direction. Now the user of the data structure can choose how they take elements out of it. If you understood how Queues worked, then you should be able to understand the diagram below.

dequesexample

A Blog With Rails

Introduction:

Warning this only works on Mac OS.

Now before you start thinking your going to make the next WordPress, let me just say we are only going to be making the MVP of a blogging site, that’s right a MVP. What is a MVP you may ask ? It stands for Minimum Viable Product, this thing we are about to build is just a web app that has CRUD functionality. What is CRUD you ask ? CRUD is Create Read Update Destroy.

Don’t be afraid of the terminal, the terminal is your friend for life 🙂 you just got to treat it right.

Up and running:

  1. Install Home Brew, is manages the software you need to manage all the software, your going to use to make software. Just follow the instructions on here: https://brew.sh/
  2. Install Bundler, it manages the specific Ruby software libraries for your application. Type the following command “gem install bundler”.
  3. Install Nokogiri, it gives Ruby the ability to understand HTML and CSS among other things. Type the following command “gem install nokogiri”. This is going to take awhile, chill out and take a break you have been working too hard 🙂
  4. Install Rails, its the thing that you build on top of, to make your web application. Ruby the language has libraries for doing different things. Rails is just a really big library for making web based applications.  As you may have guess just type in “gem install rails”.

 

Making a Rails App:

  1. CD into a folder you would like to be in and run the following command “rails new <name of your app here> -T “
  2. Now if you look into the folder you just ran this command in, you will see a folder with the name of your app. Open this with your favorite text editor, VSCode is always a good choice.
  3. CD into the newly created folder, and make your database. You can do this by running “rails db:create” and then running “rails db:migrate”. What this is doing is creating a database, and then populating it with boiler plate info.
  4. Now run “rails s” and go to the URL is gives you. You should see your app, YAY you made your first rails app 🙂 If the URL isn’t working try using http:127.0.0.1/8080 or http:127.0.0.1/3000.

 

Actually making the Blog:

  1. Run “rails g scaffold Blog title:string body:text”. What this command will do is create a Blog object, that has a title and a body and can be stored in our database. Rails knows this since “g” is for generate, “scaffold” is for create everything for me, and the rest is specify what type of things you want there to be, in the object you are creating.
  2. Now you got to let the database know about this blog object you made. To do so simply run “rails db:migrate”
  3. Finally the moment you have been waiting for, run your app with “rails s” and ta-da you go your self a working rails app 🙂

“?” in Angular 5

As you an Angular component has a life cycle, much life us humans. In the beginning we are nothing, we do a bunch in the middle and then we die 🙂 However if you want to call a certain service in your “ngOnInit” to dump some values into a var that appears in your html, your going to have a bad time. Or at least angular is by throwing a lot of errors in your console, I mean it will still work.

To get rid of these errors all you have to do it the following add a “?” at the end of the end of the var that’s in the html, and ta-da errors gone. This works since the “?” tells Angular to chill out there will be a value you there “eventually”. Hmm… but why eventually you may ask ? Its because two functions before “ngOnInit” get called , the very first one being the constructor of the class and the second being the “ngOnChanges” method. So for the execution of the first two methods its asking “WTF where is this thing in the html in the .ts file ?”  which makes throw errors.

Unreal 4 VS Unity

Over the last decade Unity has become the game engine of choose for a good chuck of game developer out there. This is due to many factors, the main factor being that it was designed as a product first. Whereas a number of other engines where brought into existence to make a game. In my opinion this is the key difference between the two engines.

Unreal was originally developed to facilitate the creation of one the most beloved FPS of all time “Unreal Tournament”. Unreal Tournament was designed to be a fast paced shooter, with high graphical fidelity and high performance networking. These things are the key features of the Unreal engine, which is why the engine has the cons and pros that it does.

In Unity’s case it was designed to be nothing but a game engine that would not limit game developers in what they could do, and eliminate the many bottle necks in the game development life cycle. It is simply a product built to make any game, not a particular game.

The differences between the two are seen in the usability of either one the engines. This is most prevalent in the languages that they use. Unity uses C# (aka Microsoft’s Java) whereas Unreal uses C++. For beginners and even intermediate programmers writing effective C++ code it very difficult. Since there is no memory abstraction layer and the syntax is not as initiative to understand. Another point of difference can also be see in the minimum computer specifications needed to effectively use the engines. Unity for the most part does not require much, it could probably run on a potato. Whereas Unreal requires a quad core (i7 preferably), 8GB of Ram (preferably more), and good video card (preferably anything greater then a GTX1060) .

The differences highlighted above also explain where the two engines are usually used. After a while Epic Games started to licences out their engine. The licences were very expensive so naturally the only people that bought it were other game studios, who then made AAA games. Unity on the other hand had a free tier and inexpensive (comparatively speaking)  which lead a lot of indie developers to adopt Unity. Indie developers are usually tiny teams working on shoe string budgets. And they are usually content with their profits if it is just enough to make another game.

The biggest difference has to be the community of either game engine. Unity has one the largest and helpful communities out there. There are a ton of forums, blogs, books, and video tutorials out there on Unity. And then there is the asset store, a market place that allows developers to sell anything and everything one would need to make a game, wither it be art work, scripts, or even music the asset store has it all. The Unreal engine has something simpler however it is  not as robust as  the Unity asset store.

It is in my opinion that if you want to make video games you should use what make your feel comfortable. If your a experienced C++ developer looking to code games and have a few art friends to make high rez 3D models, and you want this to be a PC only game choose Unreal. If your a one man developer that has some coding experience choose Unity.