Tuesday, September 2, 2014

Elixir: Phoenix compilation error

I got the following error when I tried to compile master. If you get it too, just delete the _build folder.

==> plug
Compiled lib/plug.ex
Compiled lib/plug/adapters/cowboy.ex

== Compilation error on file lib/plug/conn/adapter.ex ==
** (RuntimeError) cannot compile Plug because the :cowboy application is not available. Please ensure it is listed as a dependency before the plug one.
    lib/plug/conn/adapter.ex:4: (file)
    (elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/3
    (elixir) lib/kernel/parallel_compiler.ex:97: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/8

could not compile dependency plug, mix compile failed. You can recompile this dependency with `mix deps.compile plug` or update it with `mix deps.update plug`

--------------------------------------------------------------------------
When you get the error below, remove the deps `rm -r deps` and redownload them again `mix deps.get`

== Compilation error on file lib/postgrex/types.ex ==
** (CompileError) lib/postgrex/types.ex:6: module Decimal is not loaded and could not be found
    (elixir) src/elixir_exp.erl:104: :elixir_exp.expand/2
    (stdlib) lists.erl:1352: :lists.mapfoldl/3
    (stdlib) lists.erl:1353: :lists.mapfoldl/3
    (elixir) src/elixir_exp.erl:49: :elixir_exp.expand/2
    (elixir) src/elixir.erl:206: :elixir.quoted_to_erl/3
    (elixir) src/elixir.erl:175: :elixir.erl_eval/3

Friday, August 22, 2014

Brew commands

Normal install
brew install elixir 

Install github master version of Elixir
brew install elixir --HEAD

Upgrade Elixir to github master:
brew upgrade elixir --HEAD

Uninstall
brew uninstall elixir

Your best friend
brew man  

Manually install Elixir
  1. Clone elixir https://github.com/elixir-lang/elixir.git
  2. Check out the branch/version/hash you want to build (git co branch/hash)
  3. In the elixir directory, make clean test
  4. Make the version directory mkdir /usr/local/Cellar/elixir/version
  5. Copy the bin and lib files to the directory 
    1. cp /Users/nhu/me/elixir/elixir/bin /Users/nhu/me/elixir/elixir/lib CHANGELOG.md LICENSE README.md  /usr/local/Cellar/elixir/1.0.0
    2. Brew install elixir
    3. Brew switch 1.0.0

Wednesday, August 20, 2014

My Testing Style


Before we get into the topic, let’s define some vocabularies. I know people will disagree with how I use the term, however, I want to define it so you know what I mean when I use these words.

Unit test - Isolated tests that test a single class. Dependencies are mocked.
Integration test - Tests that test through API. It could reach several classes and/or databases.

A few years ago, my testing structure follows the testing pyramid. By default, I wrote unit test first. I wrote a test for every single functional class. (I usually didn't write test for model/bean because they were simple getter and setter. They usually didn't have a lot of functionality.) I mocked dependencies, regardless if the dependencies were written by me or not. On top of the unit test, I wrote a few integration tests to ensure everything worked well together. 

Lately, I noticed that I am leaning toward writing integration tests by default. I find that I am more confident that the code work because I don't have a lot of mocks. I also write less tests because one single test scenario could touch several classes. It is also easier to refactor because the test uses the API, therefore I can change the internal structure as much as I like. There is a downside to this. Integration often takes longer to run. It is harder to pinpoint a bug. Sometimes the setup makes it hard to understand what the test is doing. When I run into these situations, I add unit tests.
Even though I prefer integration heavy test, when I have a resident apprentice, I will make my apprentice follows the testing pyramid and write more unit tests. I believe unit tests train your mind to build cleaner code because it exposes design flaws. When you are writing a unit test and it takes 50 lines to set up the test, there is problem. My philosophy is if it’s hard to unit test, there is probably a code smell. As the apprentice develops a sense for writing clean code, I believe the apprentice will develop his/her own style of testing.

My advice on testing is to write the kind of test that let you sleep at night, literally. When you don’t write test or when you write bad test, things break and you worry when you deploy. Or your code might break at midnight and you have to stay up and fix it. Therefore write tests that makes you feel confident that you won’t have nightmare or midnight emergency.

The reason why robot can’t do our job yet is because we change our style depend on the situation. You can’t go with one extreme or another. Not writing test is bad. However, if it will takes you half a day to write a test so you can change one simple line of code, is it worth it? Can you sleep with changing that code without a test? If you are choosing one practice over another, make sure you understand why. Develop a style that let you have a good night sleep.


Here are a few pros and cons to both style:
Unit testing
Pro
- Run quickly
- Quick bug detection
- Easy to adhere to the single responsibility

Con
- Coupled to the code, thus hard to refactor
- May need a lot of set up for mocks
- May mock the wrong return

Integration test
Pro
- Test code is decoupled from production code, thus allowing you to refactor to your heart content.
- Confidence everything works well together
- Understand how to use the code

Con
- Slow
- Could be a lot of data to set up
- Harder to find bugs

Disclaimer: The content written on here is based purely on my experiences and what I’ve seen so far. It is my opinion at this time. My opinion will change.


Tuesday, July 22, 2014

How I learned Elixir

Here are the things I did to learn Elixir
  • Do the Roman numeral kata. I know this kata by heart. But I often do this when I start a new language because I want to focus on learning the syntax. The kata teaches me enough to get a feel of the language, like using a list, Enum, String, and functions.
    • I made a video walking through the kata and explaining what the syntax mean. 
  • Read the getting started guide
  • Read the docs. I general read the kernel, Enum, List, and Map. Those should be enough to get you started.
  • Create my own application.
  • Read other people code. 
Resources:

Sunday, May 11, 2014

Mac: Can't access terminal with bash

Thanks to Apple Malverick update, I had to redo a lot of configurations to get my software to work again. During that update, I somehow locked myself out of my bash shell so I couldn't use the terminal. I keep getting "Permission denied" when it tries to open bash.

Thankfully, I have zsh shell installed. I have iTerm2 so I changed the profile to start with the zsh shell.
  1. Open iTerm2
  2. Click "Profiles" from the menu
  3. Click "Open Profiles"
  4. Click "Edit Profiles"
  5. On the "General" tab, select a profile
  6. Click "Other Actions" at the bottom right
  7. Select "Duplicate Profile"
  8. On the left side, in the command section, select "Command"
  9. Enter "/bin/zsh --login" or wherever you think your zsh might be located
  10. Click "Profiles" from the menu again
  11. Select your new profile
That will allow you access to the terminal. Now you can try to change the permission or ownership on your bash file.

Unfortunately, that didn't work for me so I had to reverted back to an old bash version located. I did a lot of fiddling around, but I think these two steps did the trick.

  1. Change your shell file
    1. sudo vim /etc/shells (it doesn't have to be vim, use whatever text editor you like)
    2. Change where your bash program is located
  2. Change your default shell
    1. chsh
    2. Fill out everything.
      1. For the longest time, I only filled out my name and got the "chsh: /bin/bash: non-standard shell"
 That should do the trick I hope.

Thursday, May 8, 2014

Resolving Library not loaded for readline on Mac

After upgrading my mac, I was having this error whenever I start my terminal:
dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.6.2.dylib
Referenced from: /usr/local/bin/psql
Reason: image not found
After some Googling, a lot of answers were, reinstall readline, by
brew install readline
I couldn't do that because I don't have access to my terminal. So I decided to manually install it. After some more frustration and googling, I found the readline that works.

Saturday, January 11, 2014

Working with ClojureScript

someecards.com - I may or may not bang my head against a wall after working with ClojureScript.