JavaScript Numbers

The title for this post might sound vague, but the reason I’m writing it is because in JavaScript this is true:

(.1 + .2) !== .3;

This makes my head explode so I want to understand better the reason for this.

IEEE 754

It turns out that JavaScript only has one number type, unlike other programming languages that have many types(int, long, float, etc…). The type JavaScript uses is defined by the IEEE 754 standard for floating point numbers. This format is good because many hardware manufacturers ship they chips with support for this standard which makes operations on these numbers really fast.

Read More

Project scaffolding with Yeoman

Every time I start a new project I go to old projects to review and copy my build steps and linting rules among other things. A good developer would have automated the creation of a new project instead of going back every time. I want to redeem myself so I’m taking a look a Yeoman.

Yeoman is a tool for scaffolding web apps. It basically allows you to create custom reusable app skeletons called generators.


npm install -g yo
Read More

Enforce android coding style with checkstyle

If you appreciate elegant code that is easier to read and consistent from file to file you probably want to start using checkstyle on you java projects.

If your project is a simple java project that uses gradle you can start using checkstyle by adding this to your build.gradle file:

apply plugin: 'checkstyle'

and creating this file under config/checkstyle/checkstyle.xml (I stole it from Marco’s example):

Read More

Unable to exclude PMD rule after upgrading to gradle 2

After upgrading to gradle 2.2.1 I started having some weird issues with my PMD plugin. I kept getting this error:

[adrian@localhost project]$ gradle pmdMain
* file: ./src/main/java/src/com/ncona/project/
    rule: UselessParentheses
    msg:  Useless parentheses.
    code: getCurrentTime() + (1000 * 60 * 60 * 2),
Read More

Sharing IDE configuration with EditorConfig

It has happened multiple times that I am doing a code review and I find issues like trailing white space or tabs instead of spaces. This annoys me because it is something that your editor should do for you for free. It annoys me even more when I ask the developer to configure their editor to remove trailing white space and they tell me they don’t know how to do it. In most scenarios they are using Eclipse or some other fancy IDE that I am not familiar with, so I can’t help them much. EditorConfig will help me with that problem.

From now on I plan to add an .editorconfig file to all my projects and simply ask my colleagues to install the plugin on their IDE.

Lets see how to install the plugin for VIM. Assuming you have pathogen installed you should only need this:

Read More

Median of Integer Stream

The question

Given a stream of unsorted integers, find the median element in sorted order at any given time. So, we will be receiving a continuous stream of numbers in some random order and we don’t know the stream length in advance. Write a function that finds the median of the already received numbers efficiently at any time. We will be asked to find the median multiple times. Just to recall, median is the middle element in an odd length sorted array, and in the even case it’s the average of the middle elements.

Read More

Search Unknown Length Array

The question

Given a sorted array of unknown length and a number to search for, return the index of the number in the array. Accessing an element out of bounds throws exception. If the number occurs multiple times, return the index of any occurrence. If it isn’t present, return -1.

My answer

Reading the question there is one question that came to my mind: Can the exception be handled?. Assuming that it can’t I think the only alternative would be to check all elements starting from the first one. I will assume the exception can be handled so I can come with a better solution.

Read More

Watching JS variables for changes

After playing with angular for a while I got curios about how it can watch for variable changes and execute a function when these occur.

I did a little research and found out that all watches that you define in Angular are evaluated by an event loop that is entered when some event is triggered or apply is called.

That didn’t sound very interesting to me, but I did find some interesting alternatives.

Read More

Kth Largest Element in Array

The question

Given an array of integers find the kth element in the sorted order (not the kth distinct element). So, if the array is [3, 1, 2, 1, 4] and k is 3 then the result is 2, because it’s the 3rd element in sorted order (but the 3rd distinct element is 3).

My solution

This doesn’t sound as a very hard problem but I couldn’t find a solution better than:

– Sort using quicksort

– Return the element at index k

The complexity of this solution is the complexity of quicksort O(nlogn).

Read More

CSS Flexbox

Today I found myself in the need to create a layout that I hadn’t done before:

This layout would be easy if I knew the widths of the elements, but I needed it to be flexible. Another possibility that crossed my mind was using percentages but there was a requirement that didn’t allow me to do it. To understand better there are a few things about the image above that I need to explain:

  • The image has a static width
  • I want the button on the right to expand and use as much space as it needs, but not more than it needs
  • I want the center portion to use all the available space left by the image and the button
Read More