Saturday, April 1, 2017

I moved my blog (again) :)

I moved my blog again. This time to GitHub Pages.

I have written the reasons here. Check it out if you want to know why. :)

Happy coding!!

And enjoy doing the other good things that you love doing. (And let's already stop doing the bad things we are doing :) )

Sunday, February 19, 2017

Substituting polymorphism for if/else or switch/case - Example #1 - Shapes

This is Example #1 of the series of examples I'm going to give on "Substituting polymorphism for if/else or switch/case statements".


You have a new task


It's your first day at work. You are very excited to be involved in a real-world project.

Your team lead tried to explain to you the purpose of the project you are involved in.
"Here at Hugis Inc., we strive to be the best drawer of shapes."
After an hour, he gave you your first task.

You have to implement this method:

(Because I used C# for many years, I'm going to display C# in these posts. But I will also write these examples in Python and maybe other OO languages in the future. You can view them here.)

The codebase of the project you are involved in already have these classes:
  • a base Shape class,
  • classes for Circle, Square, and Triangle that derives from the Shape class.


The Evil code

This task is so easy!!! (you say to yourself)
You then started typing on the keyboard. Click! Click! Click!



Then after a few hours of hard work, you pushed your code to the repository, waiting for your team lead's comments.

The Good Code


Your team lead's comment came in.
"Good job!"
Wow!
"But there is a better way of doing this.

"We want to put the function drawCircle() inside the Circle class, and the function drawSquare() inside the Square class, because we want to have an easy access them anywhere in the system.

"Come over here. I'm going to show you how I will do the changes."
You then went to his table and watched him refactor your code.
"Because we want to be able to draw each Shape that we have, we will create an abstract method named draw() in our base Shape class.

     abstract public void Draw();

"Do you know what an abstract method is?"
No.
"An abstract method is a method definition that has no implementation -- like the getArea() method in the Shape class.

"Notice that getArea() does not have an implementation inside the Shape class. But any class that inherits from the Shape class is required to provide an implementation for getArea()"
Ahh! The Shape class did not provide an implementation for getArea() because different shapes has different formulas for getting the area! Great!

Now I understand what you want me to do with drawing shapes.

Let me be the one to refactor my code, please. I think I already know what to do.
"Ok! Go Ahead!"
You went back to your machine and started refactoring your code.

(Please go to the GitHub repository to view the complete refactoring.)



MTGOTB, TCOTU, BGWML

Saturday, February 18, 2017

Substituting polymorphism for if/else or switch/case - Introduction

A few weeks ago I saw this post from a facebook friend:


He is saying jokingly something like "Programming is like love... don't switch() because it will end up with a break... LOL"

I then asked him if his alternative to switch/case is polymorphism. He said no; and that he does not yet know what polymorphism is.

So after a few days, I thought of creating a blog post where I will teach how to replace if/else or switch/case statements with polymorphism.

I'm going to try to teach you how to replace your if/else with polymorphism

Have you seen code like this one below?
      If (typeOfShape is Circle)
         drawCircle()
      Elseif (typeOfShape is Square)
         drawSquare()
      Elseif (typeOfShape is Triangle)
         drawTriangle()
Or this one?
      Switch (typeOfShape)
         Case Circle:
            drawCircle()
         Case Square:
            drawSquare()
         Case Triangle:
            drawTriangle()
If you have been programming for a few years already, I believe you have seen lots of code that looks like those.

But did you know that, if you are using an object-oriented language such as Java, C# or Python, codes like those ones above are sometimes considered evil?
Evil? (you might say).
Yes! EVIL!

But I did not say that they are always evil. I said that they are sometimes evil. ("Sometimes evil" because I only have a few years of programming experience. "Often evil" when I have more :D . I'm just joking.)

They are evil because they might make modifications in the future much harder than they should be.
(you might be asking…) How else am I going to do conditional constructs like that? If/else and switch/case are the only ones available!
There is actually another method available to us on how to do conditions in OO languages. It is called polymorphism.

I found this great explanation by Steve Guidetti about polymorphism from code.tutsplus.com:
Polymorphism is a long word for a very simple concept. 

Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface. 

The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they're all used the same way. 

A real world analogy for polymorphism is a button. Everyone knows how to use a button: you simply apply pressure to it. What a button "does," however, depends on what it is connected to and the context in which it is used -- but the result does not affect how it is used. If your boss tells you to press a button, you already have all the information needed to perform the task.
There are already lots of available resources on the web about polymorphism. [The] Google [search engine] is your friend. Ask your friend to teach you polymorphism. But I recommend you to start with that explanation from code.tutsplus.com I linked to above, or from stackoverflow.

Then after that, go back here because I'm going to give you a number of examples on how to convert your if/else and switch/case statements into polymorphic code.

(Another reason why I delegate the explanation to others is because I'm not very good at explaining things using the English language :) )
Why are you going to give me lots of examples? I think one example will do. 'You think I'm dumb?
No. I do not think you are dumb.

It's only that I read an article a few months ago which claims that giving students lots of examples of a problem and their solutions can help them easily spot similar occurrences of that problem and can help them easily solve those kinds of problems. (But I already forgot where that article is located. Maybe I took a note of it somewhere but I'm not able to find it. I will just update this post when I will find it in the future)

Here are the examples:

1. Drawing Shapes
2. to be posted
3. to be posted
4. to be posted

Examples from other people:
https://sourcemaking.com/refactoring/replace-conditional-with-polymorphism

Have fun coding!!!


MTGOTB TCOTU BGWML

Saturday, February 4, 2017

Hints for Nand2Tetris Chapter 1 Exercises

I have already done Chapters 1 to 8 of Nand2Tetris last 2013. But I want to finish it. I have decided to do everything all over again.

I am not allowed to put online my solutions to the Nand2Tetris exercises. So what I'm going to do instead is to give some hints on how to solve them.


Here are the hints for the Chapter 1 Exercises:

1. Not gate - look at the Nand truth table; notice the output when both a and b are the same

2. And gate - use your newly created Not gate (and the Nand gate of course)

3. Or gate - we can use the idea from the Canonical Representation presented on page 9. But instead of focusing on the 1 outputs of the Or function, let's focus instead on the 0 output.

The Or function has only one 0 output.


This means that we can be able to create an Or gate without using an Or gate. haha
We can solve this using the And and Not gates

"We will Not the And."


4. Xor gate - we can now use the Canonical Representation from page 9.

5. Multiplexor (Mux) - same as #4; use the Canonical Representation from page 9

6. Demultiplexor (DMux) - use truth table to determine when a and b will be 1; then use the Canonical Representation



7 to 10. Multi-Bit gates (Not16, And16, Or16, Mux16) - read "A.5.3 Busses" of the Appendix to know more about how to solve these.

Do something like this, for example, for the Multibit Not gate:

    Not(in=in[0], out=out[0]);
    Not(in=in[1], out=out[1]);
    ...

    Not(in=in[9], out=out[9]);
    ...

11. Multi-Way Or (Or8Way) - use Or gates and internal pins (see A.5.2 of Appendix A for an explanation for internal pins)

12 Multi-Way/Multi-Bit Multiplexor

       12.a (Mux4Way16) - use Mux16 and temporary internal pins



       12.b Mux8Way16 - similar to 12.a but also use Mux4Way16

13. Multi-Way/Multi-Bit Demultiplexor - quite similar to 12.a and 12.b

       13.a DMux4Way - Use DMux and internal pins



       13.b DMux8Way - same to 13.b but also use DMux4Way 


If you have any comments or corrections or ideas on how to make the hints better, please tell me. Thanks!




Monday, August 8, 2016

I'm reviewing data structures and some graph algorithms using Stanford's CS106X

A few weeks ago, I wanted to be able to solve some problems on UVA Online Judge that requires some knowledge of graph algorithms. So I thought of trying to do again the assignments for Stanford's CS106B that I did last 2011.

The problem was that the assignment files that I have requires Visual Studio 2005. I tried to find ways to make it work on VS 2015 but I failed.

So I went to the latest CS106B website to download their latest assignment files (maybe they are using Visual Studio 2015 now?).

... Toinks! I needed to login to download the files. But I'm not a Stanford student! So I was not able to download the files.

Luckily, I found the latest website of CS106X and the files are downloadable. I thought to myself that the problems for CS106X might be more difficult than CS106B's but I think I can be able to do this now because I already experienced solving problems similar to these before.

The handout on the Course Information says that uploading my code to GitHub is a violation to the Honor Code...

The following four activities are among the many considered to be Honor Code violations in this course:
1. Looking at another student's code.
2. Showing another student your code, or making your code public so that it's searchable and easily discovered online or elsewhere.
3. Discussing assignments in such detail that you duplicate a portion of someone else's code in your own program.
4. Uploading your code to a public repository (e.g. github.com or bitbucket.com) so that others can easily discover it via word of mouth or search engines. If you'd like to upload your code to a private repository, you can do so on bitbucket or some other hosting service that provides free-of-charge private hosting.

... so I'm not going to put my code on GitHub or anywhere where anyone can download it. (I placed my solutions online last 2011 when I did CS106B but I already removed them.)

What I'm going to do instead is to put screenshots and the finished executable files of my solutions online just to give proof that I solved the exercises.

You can see the screenshots and executable files of my solutions on my GitHub.

Here is the screenshot of the first problem that I did:



Thursday, August 4, 2016

The Angular 2 "Tour of Heroes" tutorial has a missing part

I think there is something missing in the "Routing" part of the "Tour of Heroes" tutorial -- after the "Make the router available" section and before the "Router Links" section.

The <router-outlet> needs "directives: [ROUTER_DIRECTIVES]". But the code for that is given way below of the tutorial.

I got confused. But I was able to see the error and found a fix for it.

Maybe you got confused also.

Here is the link to what I think should be placed in that missing part. I'm not a native English speaker so my English might not be 100% correct but I believe you can be able to understand it.

Happy coding!


Sunday, July 31, 2016

What is the purpose of "imports" in project.json of .NET Core apps?

I was doing the tutorial "Building Your First Web API with ASP.NET Core MVC and Visual Studio" and after I finished it, I thought of trying to see if Moq will work with .NET Core.


Then I changed the content project.json file with the JSON below whose significant parts came from the "Testing your Console App" section of the tutorial "Getting started with .NET Core on Windows/Linux/macOS using the command line":

Then I tried to remove the "imports" part of the project.json file because I do not know what it was for. But this error pops up in Visual Studio:



... and errors like this in the Output window:
Package xunit.assert 2.1.0 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package xunit.assert 2.1.0 supports:
- dotnet (.NETPlatform,Version=v5.0)
- portable-net45+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile259)

So I tried to look for the purpose of this "imports".

I found the docs for project.json but I do not understand what it is trying to say.

Will cause other packages targeting portable-net45+win8 to be usable when targeting netcoreapp1.0 with the current project.
What does that mean?

Then I tried to google for using this:
more explanation of "imports": "portable-net45+win8"
Google shows only one result which came from StackOverflow which led me to an article from the EF docs. It says this regarding EF:
Some of EF Core’s dependencies do not support .NET Standard yet. EF Core in .NET Standard and .NET Core projects may require adding “imports” to project.json as a temporary workaround.

The workaround is to manually import the portable profile “portable-net451+win8”. This forces NuGet to treat this binaries that match this profile as a compatible framework with .NET Standard, even though they are not. Although “portable-net451+win8” is not 100% compatible with .NET Standard, it is compatible enough for the transition from PCL to .NET Standard. Imports can be removed when EF’s dependencies eventually upgrade to .NET Standard.
That is a much better explanation! Even though I do not know what PCL is :) .

Happy coding!