The Practical Dev

Comparison of machine learning techniques in email spam detection

2018-03-13 19:13:13

Unsolicited bulk emails, also known as Spam, make up for approximately 60% of the global email traffic. Despite the fact that technology has advanced in the field of Spam detection since the first unsolicited bulk email was sent in 1978 spamming remains a time consuming and expensive problem.

This report compares the performance of three machine learning techniques for spam detection including Random Forest (RF), k-Nearest Neighbours (kNN) and Support Vector Machines (SVM).


Despite the rising popularity of instant messaging technologies in recent years, email continues to be the dominant medium for digital communications for both consumer and business use. Following industry estimations (Symantec Corporation, 2016, pp 31 1), approximately 200 billion emails were sent each day in 2015. On average, business users sent and received around 42 emails per day. Given those facts, it is no wonder that email is still the weapon of choice for cybercriminals who want to target the broadest possible audience electronically.

According to Nucleus Research (Nucleus Research, 2007 2), spam costs US businesses an average of $712 per employee every year due to diminished productivity, lost customers, spent bandwidth and increasing the cost of maintenance.

Estimates (Statista, 2017 3) are that slightly less than 60 percent of the incoming business email traffic is unsolicited bulk email (known as spam) which was the lowest level since 2003. However, even though the global percentage of spam/ non-spam ratio is decreasing, the competition between spammers and spam filtering techniques continuous. It is fair to say that the problem is not going away, and the need for reliable anti-spam filters remains high.

The idea of automatically classifying spam and non-spam emails by applying machine learning methods has been popular in academia and has been a topic of interest for many researchers.

Knowledge engineering and machine learning are the two main approaches scientists have applied to overcome the spam-filtering problem. The first solution focuses on creating a knowledge-based system in which pre-defined rules dictate if an incoming message is legitimate or not. The primary disadvantage of this method is that those rules need to be maintained and updated continuously by the user or a 3rd party like for example a software vendor.

The machine learning approach, in contrast, does not require pre-defined rules, but instead messages which have been successfully pre-classified. Those messages make the training dataset which is being used to fit the learning algorithm to the model. One could say the algorithm defers the classification rules from the test data.

This study compares three algorithms which are suitable for classification problems. In particular, we included the following methods:

  • Random Forest
  • k-Nearest Neighbours
  • Support Vector Machines with Linear Kernel

For the experiment, we use Hewlett Packard’s Spambase dataset which is publicly available and downloadable from the UCI Machine Learning Repository.


The following part provides a brief introduction to the three methods used for the experiment and compares general advantages and disadvantages.

Random Forest

Tin Kam Ho first introduced the general method of random decision forests at AT&T Bell Labs in 1995 (Tin Kam Ho, 1995 4). The thought is, that

If one tree is good, then many trees (a forest) should be better.

Stephen Marsland, 2014, p. 275 5

The algorithm deducts the classification label for new documents from a set of decision trees where for each tree, a sample is selected from the training data, and a decision tree is created by choosing a random subset of all features (hence "Random"). The algorithm is suitable for complex classification tasks in small datasets (Breiman, 2001 6). By averaging multiple trees, random-forest-based models have a significantly lower risk of overfitting and include less variance compared to decision trees. The major drawback is performance as a large number of trees may make the method slow for real-time prediction.

k-Nearest Neighbours

The k-nearest neighbour (kNN) classifier is a straightforward method and works well for simple recognition problems. It is considered as an example-based classifier because the training data is used for comparison and not for explicit category representation. In literature, the term lazy-learner is also often related to kNN.

When a new document needs to be categorised, kNN tries to find the k nearest neighbours (most similar documents) in the training dataset. Given that, enough neighbours are found and have been categorised, kNN uses their profile to assign the new document to the same category. This comparison is a real-time process, and therefore the main drawback of this approach is that the kNN algorithm must compute the distance and sort all the training data for each prediction, which can be slow if given a large training dataset (James, Witten, Hastie, & Tibshirani, 2013, pp. 39–42 7).

Support Vector Machines

The original Support Vector Machines algorithm was designed by Vladimir N. Vapnik and Alexey Ya. Chervonenkis in 1963 (Vapnik & Chervonenkis, 1964 8). SVM has its foundation in the broad concept of decision planes which define the decision boundaries. Decision planes separate distinct objects by finding the optimal hyperplane with the maximum margin between two separate classes.
SVM provides high accuracy on small and clean datasets but tends to perform less efficient on noisier datasets with overlapping classes (James et al., 2013, pp. 349–359 7).


The following part describes the experiment procedure including exploratory data analysis, model fitting, evaluation and prediction.

 Exploratory Data Analysis

The Spambase dataset was composed by Mark Hopkins, Erik Reeber, George Forman, and Jaap Suermondt at Hewlett-Packard Labs. The set includes a total of 4601 observations from Mr Foreman’s personal email account, 2788 messages are classified as Non-Spam and 1813 were effectively Spam (cf. figure 1).

Figure 1. Non-Spam vs. Spam

58 different attributes were computed of which 57 are continuous and one is a nominal class label. Typically, documents are represented as vectors of word frequencies. The dataset includes measurements for 6 character frequencies and 48 different word frequencies such as "Internet", "George" (Mr Foreman’s first name), "Credit". Furthermore, three data points were collected which represent the average, the maximum and the total length of character sequences in uppercase (cf. figure 2).

Figure 2. Attribute Information Spambase

Feature Selection

A widely used algorithm for automatic feature selection is Recursive Feature Elimination or RFE. It is based on the idea of repeatedly constructing models and select either the worst- or best-performing feature. RFE than removes the feature from the stack and repeats the process with the remaining features in the set.

Figure 3 illustrates RFE applied with a Random Forest algorithm to the Spambase dataset. All 57 attributes have been selected in the example, although the plot shows that selecting just 44 attributes provide similar accuracy.

Figure 3 . Feature selection using RFE algorithm

 Classification tree

Another handy technique in data mining is recursive partitioning. This method helps to visualise the decision rules for a particular prediction. Figure 4 shows an example of a classification tree on the Spambase dataset.

Figure 4. Classification Tree Spambase


After we have completed our initial data exploration analysis, we now prepare the data and train our models using the three describe methods. The data preparation involves the following steps:

  • Set human readable column names on the data frame
  • Replace the class data with descriptive label where zero represents "Non-Spam" and a one marks a record as "Spam"
  • Cast the class column to data type factor as the caret package complains if labels are 0 or 1
  • Take samples from 1000 and split those into test and training sets randomly with a training/ test ratio of 70%

Prediction and evaluation

Finally, after we have completed the training step for all three models let us have a look how they compare to each other regarding performance. We compare the performance of all three approaches by evaluating the most commonly used indicators: spam precision (SP), spam recall (SR) and accuracy (A). All three indicators originate from the confusion matrix of each model (cf. figure 5).

  • Spam precision is the percentage of correct results divided by the number of all returned results
  • Spam recall is the percentage of all Spam emails which are correctly classified as Spam
  • The accuracy is the percentage of all emails that are correctly categorised

Figure 5. Confusion Matrix

The table below summarises the performance result of all three machine learning methods. We determine from the results that k-Nearest Neighbours (kNN) and Support Vector Machine (SVM) perform similar weak regarding accuracy and Random Forest (RF) outperforms both. We see that RF and SVM have the same relatively high percentage of spam recall while kNN performs significantly worse in that category. Finally, we learn that RF has the highest percentage of spam precision and SVM almost 10 points less than RF.

Algorithm Spam Precision (SP) Spam Recall (SR) Accuracy (A)
Random Forest 92.66 87.07 92.31
k-Nearest Neighbours 88.07 82.76 88.96
SVM Linear 94.87 87.07 88.96


By the looks of the result, one could say that using the random forest approach is the gold way, although we need to keep in mind that we have not fine tuned any of those models at all! Therefore due to its design Random Forest performs relatively well "out-of-the-box" compared to k-Nearest Neighbours and Support Vector Machine.

This article was first published on my blog as "Comparison of machine learning methods in email spam detection".


  1. Symantec Corporation. (2016). Internet Security Threat Report (Vol. 21)

  2. Nucleus Research. (2007). Spam costing US Businesses $712 Per Employee

  3. Statista. (2017). Global spam email traffic share 2014-2017

  4. Tin Kam Ho. (1995). Random decision forests. Proceedings of 3rd International Conference on Document Analysis and Recognition, 1, 278–282

  5. Stephen Marsland. (2014). Machine Learning: An Algorithmic Perspective (2nd ed.). Chapman; Hall/CRC

  6. Breiman, L. (2001). Random Forests. Machine Learning, 45 (1), 5–32

  7. James, G., Witten, D., Hastie, T., & Tibshirani, R. (2013). An Introduction to Statistical Learning

  8. Vapnik, V., & Chervonenkis, A. (1964). A note on one class of perceptrons. Automation and Remote Control, 25. 

Read more

5 Reasons Why I Love Being Wrong

2018-03-13 18:23:33

This post was first published in its entirety on CoderHood as 5 Reason Why I Love Being Wrong. CoderHood is a blog dedicated to the human dimension of software engineering.

When I was programming 100% of my time, it was my job to write robust and maintainable code. My work needed to be well documented, designed with the proper level of abstraction and as bug-free and efficient as possible. I also had to come up with good ideas for how to resolve problems and create viable products.

As a developer, I had to be right a lot of the time. Being wrong when writing code resulted in bugs and other issues. Regardless of code reviews and testing, dumb ideas in software code have a way of costing an ever-increasing tax.

In my executive role, things are very different. It is an entirely different job. My primary responsibilities are to hire great people, create the best environment for them to thrive and be productive, make sure reality is well defined and make sure that we make good decisions.

In my job, it helps if I have good ideas, but it does not mean that I have to come up with the best ones. It just means that I have to make sure the best ideas surface to the top, regardless where they are coming from or who is coming up with them. I am entirely responsible for my team's results. I am not responsible for putting a thumbprint on everything my team does.

On Being Wrong

I don't deny it. Most of the time I have an opinion. It is part of my job to have informed opinions, but I don't expect them to be always right. I hire people way smarter than me to come up with better ideas than mine.

When having discussions with my teams, I don't mind being wrong. In fact, I like it. I could even go as far as to say that I love being wrong, at least in hindsight.

I don't try to be wrong; quite the opposite. I do my absolute best to be well informed, have good arguments and learn whatever I can to ensure that I am right as often as possible. If I were wrong every time I opened my mouth, eventually my team would lose confidence in my abilities, and that would make it impossible for me to lead.

Despite trying hard, I am wrong a lot. That is the joy and the burden of hiring smart people. They don't let the boss get away with BS, and that is awesome! I would not want it in any other way.

Most importantly, when I try to be right and I realize I am not and, as a result, the right decision is made, that's when I win. It might seem like an Alice in Wonderland upside-down logic, but let me explain what I mean by giving you five reasons.

#1 -- If I challenge a good idea with a bad one, the good one gets stronger

If somebody in my team has an opinion and I test it, if it is a good idea then it grows and becomes stronger. If it is a bad idea, then it dies or takes a different direction. Either way, we made progress.

To make progress, it almost doesn't matter if my challenge is right or wrong. For things that are important, it does matter that I don't let the first idea that sounds good take hold without some more in-depth scrutiny.

I am not advocating that you should challenge everything anybody says 100% of the time. That's just annoying, and sometimes you just need to trust people. Extremes are never a good thing. I am suggesting that, when something matters, it should get scrutinized even if it sounds good.

#2 -- When I am wrong, somebody else in my team is right

When I voice an opinion, I am convinced that it is right, unless I am playing devil's advocate, which sometimes I do. However, my job is not to be right; my job is to make sure the right decisions surface. Getting to the best idea is what counts. I do need to be careful to recognize when an idea is better than mine; but, as long as I listen and keep my eyes and ears open, that's usually not a problem.

I am not THE judge of ideas either. Everybody in my team is a judge of ideas. My job is to listen to the many angles, including mine, and choose the one that passes scrutiny and is on the right path to achieving the company's high-level goals.

When somebody proves my opinion to be wrong, we both made forward progress and everybody wins. It is crucial to abandon bad and distracting ideas for better ones, no matter who was the author. That is what leads to good decisions and outcomes. If I have a bad idea that helps someone come up with a better one, we made progress, and everyone wins, including me.

The worse thing that can happen is not being wrong. The worse thing that can happen is when nobody can come up with something that can survive scrutiny. If that happens, it is on me as I apparently haven't hired the right people or I haven't provided my people the right opportunities or environment to do what they need to do.

#3 -- When I am wrong, I learn

I don't deny it. Being right about something makes me feel good, and who doesn't like to feel good? I know I do. However, if I were right all the time, I wouldn't learn anything.

As long as the team ends up making the right decision, I much prefer to be wrong and learn, than to be right and stay stagnant. Stagnation is my definition of hell.

#4 -- When I am wrong, I grow

Whenever I am wrong, I learn something, but I also grow an inch. Realizing that I am wrong is a way to become more mature and resilient to the next challenge, and it thickens my skin. Next time in a similar situation I'll be able to apply what I learned and help the team make progress.

Now, again, I am not talking about extremes. Being wrong most of the time would eventually destroy me. There is a sweet spot and a healthy ratio when it comes to "growth by being wrong." I have learned that everything in life is a bell curve, and extremes are rarely healthy.

#5 -- When I am wrong, progress is being made

Imagine a totalitarian regime where the dictator must always be right by law. Nobody is allowed to question anything the dictator says, and everyone must defend his decisions no matter how outlandish they are.

Do you think that regime would be successful, even if the dictator is a genius? It is just impossible for one person to see reality from all points of view. Everyone has blind spots, bias and a limited understanding of reality. Different ideas and a constant search for the best ones is what enables progress and innovation.

As long as there is an open debate and the best ideas survive, progress is being made, regardless who had a good idea and who was wrong.

Some Final Thoughts

In the software industry, everything is the result of teamwork. Every once in a while you need a genius to plant a fantastic and revolutionary seed, but the plant will not grow unless it is cultivated by open dialog, collaboration and an "idea meritocracy" environment.

An idea meritocracy is a decision-making environment where the best ideas win. To create it, you must give an opportunity for people to put their thoughts on the table for everyone to see, have constructive disagreements, and have a protocol to get past any conflicts that remain. That protocol could be "the boss decides." It doesn't matter as long as a decision is made that has the best chance of being the right decision. No decision is the worst decision.

In an idea meritocracy, it doesn't matter who is right or wrong. It only matters that the best ideas survive and thrive.

If you enjoyed this article, keep in touch!

Read more

Changelog: Chronofilters!

2018-03-13 18:20:31

Chronofilter? It's a weird name but that's what I called my branch and I'm sticking with it!

We now have filters on the home page and the tag pages which will help folks discover the most interesting content from the past while. The cream rises to the top really well this way.

This will make spending time in the tag pages more valuable. As someone who has trouble keeping up with the JS community's pace of progress, this should help me click in and get a look at what the community engaged with most in the past week or month. And if you only come to the about once a week, this will help you catch up.

The latest tab might get spammy sometimes because it's the latest unranked and unmoderated content, but dipping in there to ❤️ the posts you like will go a long way in ensuring good content across the site. And we are always working on improving our ranking and moderation systems.

Anyway, hope folks enjoy the new navigation features!

Read more

Junior Dev Survival Guide: How to Communicate About Code

2018-03-13 18:16:09

This post was originally published on my blog The Great Code Adventure

Talking words into someone else's face is hard. Especially when those words are meant to describe a frustrating bug. Developers at any level, but especially juniors, often struggle to communicate clearly about their code to team members, making it hard to solve problems together. Strengthen your debugging communication skills with this handy guide!

Talking is Hard, But It's Important

Clear and precise communication about frustrating bugs can be incredibly difficult no matter how much engineering experience you have under your belt. As engineers, we're basically a bunch of weirdos! We don't always like talking to people and we get sucked into the problems we're trying to solve. These personality traits and habits can make it hard to collaborate effectively with our team members. This hurts our productivity as a team, our code quality as a team and our own personal growth as engineers.

It's Even Harder For Junior Devs

It's especially tough to acquire and improve on these communication skills as a junior dev. When you feel pressured to contribute or embarrassed to admit that you don't know something, you'll choose to stay silent or to ask for help begrudgingly.

If you choose the later, you're likely to avoid providing all the necessary details and context when reaching out for help. This will only frustrate your team members.

Try to remember that no one expects you to know all the things!

As a junior dev or a new team member, you're going to get frustrated about everything that you don't know. You're going to get trapped in a paper bag. Your teammates don't expect you to describe what you don't know, or to know what is outside the paper bag. Start by describing what you do know, by explaining what's inside the paper bag, it's shape and contours. Once you've provided your team member with the tools they need to help, you'll find your way out of there together.

cat in a bag

The guide in this article lays out what to say about our buggy code. But before we dive into what to say, let's take a moment to talk about how we say it.

Specificity of Language

When you talk about bugs we're struggling with or obstacles you're facing, always be specific and precise. In other words, DON'T say:

"It doesn't work when you do it like that."

DO say:

"When a user who is an admin clicks 'save', the form does save BUT the whole page reloads, instead of just updating the appropriate portion of the page."

The difference between these two statements is vast. Don't give your listener the benefit of the doubt. Even if they are familiar with the feature you're working on or the bug your struggling with, always provide lots of details and context.

Read on to learn how to provide that detail!

A Guide to Explaining What's Wrong

When you're working on a feature, or debugging a production issue, and you've reached the limit of your understanding and your patience, it's time to reach out. You're ready to pull over someone else on your team for help. But how can you explain the problem to them, give them all of the context you've built up for yourself and actually provide them with the tools they need to help? Try the following:

  • Understand and communicate the current behavior. Describe the bug that is occurring.
  • Understand and communicate the behavior circumstances, i.e. the circumstances under which the bug is occurring.
  • Understand and communicate how the current behavior differs from the expected behavior.
  • Understand and communicate how the current behavior differs from the desired behavior

Unpacking the Guideline

In order to follow this guide, we need to have a clear understanding of some of the underlying concepts. Let's take a closer look at some of the terms used above.

Current Behavior

This refers to the bug that you are experiencing and struggling with. Be specific when you describe what is currently happening in your feature. Use lots of details and then distinguish it from the expected behavior and the desired behavior in order to show what is wrong.

In the example above, we would describe the current behavior as:

When the form is saved, the record is saved and the whole page refreshes.

Behavior Circumstances

The conditions under which the current (buggy) behavior is occurring. In our example, our behavior circumstances are:

When a user who is an admin hits "save"

Desired Behavior

What we want to happen. In our example:

The page should not do a full refresh. The page should only update the appropriate portion to reflect the saved changes.

Expected Behavior

This can be different from desired behavior! For example, desired behavior represents the goal of your work on the feature. Think of this as the behavior, design or UX spec laid out for you when you picked up this unit of work.

We can experience bugs or roadblocks at any point in time as we dev on a feature. When we bring in a team member to help us debug, we need to explain to them what we expect to happen under the given circumstances, in light of the current state of the feature.

For example, the "bug" described in our example above isn't a bug if you haven't even built the code to prevent a full page refresh! It's only a bug if you have written that code and it's still not working. We'll gain a better understanding of the difference between desired and expected behavior in the following section.

Putting the Guide Into Practice: An Example

Let's walk through a specific example of a bug in a new feature and apply our fancy new guidelines.

Our Feature

Let's say you work at some kind of music distribution company and you've been tasked with building a feature onto the new album creation page. Here are your feature specs, i.e. you desired behavior:

A user should be able to upload a song. While the song is uploading, they should see a progress bar. When the upload is done and is SUCCESSFUL, they should see an audio player and the file name of the uploaded song. When the upload is done and is UNSUCCESSFUL, the user should see an error message and a retry button.

The Problem

We've been hacking away at this exciting new feature and we almost have it working. But we've been stuck on this frustrating bug for a few hours now and we're ready to ask a team member for help. So, we call over our team mate for some debugging help.

"Hey Rick! I'm super stuck on this tough bug in the feature I'm working on. Do you have a few minutes to help me figure it out?"

"Sure thing Jerry! I'm always happy to help out a pal!"

Obivously we are working on this app with Doofus Rick, the only Rick that would be happy to help.

Using the Guide

We walk our team mate through the current behavior, i.e. the bug:

When a song is uploading, the progress bar appears, but when the song finishes uploading the progress bar doesn't go away! It stays FOREVER (or until we refresh the page). OMG.

We explain the behavior circumstances:

This behavior occurs when the song DOES NOT upload successfully. When the song DOES upload successfully, the progress bar correctly goes away and is replaced with the audio player + file name.

We explain how this differs from the desired behavior:

The goal is that, for the case in which the upload is NOT SUCCESSFUL, the upload progress bar should be replaced with an error message and retry button.

Then, we explain why our current behavior is a problem, i.e. how it differs from the expected behavior:

I haven't implemented the error message + retry button yet, but I DO expect the progress bar to go away when the upload finishes, even under the condition in which the upload is NOT successful.

And that's it! We provided all of the context that our fellow dev needs to help us debug this frustrating issue. They know how it is supposed to behave, the manner in which it differs from that behavior and the conditions under which this difference in behavior occurs. They are ready to jump in and debug with you!

Good Communication Means a Successful Team and A Successful You

What do you win when you practice clear and precise communication? Let's revisit our sad path example. When we were frustrated with our bug, we told our teammate:

It doesn't work when you do it like that.

Our helpful teammate then is forced to ask a lot of questions: "What is 'it'?", "What do you mean by 'that'?", "Exactly what conditions lead to it behaving in that manner?", "Well, how is is even supposed to behave?"

This is frustrating and time consuming for both parties. It leaves the teammate who is there to help feeling like they are missing something, and it leads to you being bombarded with questions. In this situation, we have two developers who aren't on the same page or aware of the same goal. The work moves slowly and your relationship with your teammate suffers.

When you follow the guide, on the other hand, our interaction proceeds very differently. We use precise language and we provide the context and details up-front. Our teammate doesn't have to dig for answers or make dangerous assumptions. Both team members are striving towards the same goal––the desired behavior––and understand the same bug––the current behavior and conditions. Your work moves quickly and you eliminate interpersonal tension while you pair.

On top of that, you become an ideal team member. The kind of teammate who does the hard work of uncovering and describing the parameters of a problem––surfacing issues before they become disasters and making it easy for everyone to solve problems together.

Best of all, as a junior dev, you learn faster. You're no longer hesitant to ask questions or reveal what you don't know, because you've learned how to do so in a productive manner that moves the work forward for everyone.

Read more

Botters of the Galaxy - Complete Stream Collection

2018-03-13 18:00:49

These are all the streams I did for the Botters of the Galaxy competition. I'll be doing a post-mortem a bit later.

Day 1-1

Day 1-2

Day 2-1

Day 2-2

Day ...3

Day 4

Day 5

Day 6 (Finale)

Read more

Best Bootstrap themes in 2018

2018-03-13 15:26:47

Originally I've published this material to Syndicode blog.

Several years ago the agency I work in published a neat article about how to choose a template for your Rails project. Now I'd like to expand the topic with the best Bootstrap themes in 2018. Free and paid - choose whatever you like, regardless your project budget.

With no delay, I present you the list of

The best Bootstrap themes in 2018!

  1. Start Bootstrap. Just like several years ago, it is still a good start point! It's free to use.
  2. Some basic themes can be found on Bootswatch. It's free to use.
  3. For a more complex solution, you can check the paid one resource - WrapBootstrap.
  4. Another variant of clean and customizable admin dashboard theme templates is Bootstrap Dash. There you can find free and paid options.
  5. Latest Bootstrap free themes on Creative Tim. From landing pages and e-commerce to UI kits and components.
  6. Paid Propeller Bootstrap Starter Template based on Google Material Design is as great as simple.
  7. Light, customizable and free to use eCommerce Templates & Themes on LemonStand. Every template is fully mobile responsive, so it can be applied for any device.
  8. On BootstrapMade you can find free, fully responsive and cross-browser themes.
  9. Free and premium paid bootstrap templates & themes to download for commercial or non-commercial projects presented on Gridgum Marketplace site.
  10. Azmind delivered the scope of 6 Ruby on Rails admin templates.
  11. On ThemeWagon you will find a bunch of free Bootstrap 4 templates.
  12. 34 free and premium paid Bootstrap 3,4 themes and templates for personal and commercial use can be found on Bootstrapious.
  13. JustFreeThemes presents the scope of free to use mobile-friendly and responsive WordPress themes.
  14. The handpicked list of free admin dashboard templates for web applications by Colorlib. This site also provides a list of Bootstrap 4 website templates here.
  15. If you're not afraid of endless scrolling, check the list of free Bootstrap HTML5 website templates on freshDesignweb. There's a lot of!
  16. Neat paid Bootstrap website templates are also presented on ThemeHunt.
  17. CSS Author categorized Bootstrap different free themes, so you can find admin templates, portfolio, material design and even Bitcoin Cryptocurrency templates!
  18. Bootstrap themes provide high-quality free Bootstrap themes and templates built with Bootstrap.
  19. Mobirise Bootstrap templates are free and you may use them in any personal or business projects freely. The site also shows a video tutorial on creating a website with Bootstrap template.
  20. 100+ free to use Bootstrap themes for any taste and purpose collected by uicookies.

Read more

Working with Queues and Stacks in PHP

2018-03-13 15:26:33

The content of this post was inspired by the post To Queue Or Not To Queue by Vaidehi Joshi and from the Base CS Series Video Stacks and Queues

On the post made by Vaidehi she explain how queues work and show some "functions/methods" to enqueue and dequeue something, this is a simple and direct examples to show you how it works in PHP (i'm assuming that you already have seeing the CS video).

PHP comes with a standard library called SplQueue, (Spl stands for Standard PHP Library)

SplQueue inherits from SplDoublyLinkedList. So, objects of SplQueue support methods push() and pop(). But please be advised that if you use push() and pop() methods on a SplQueue object, it behaves like a stack rather than a queue

long short story, SplDoublyLinkedList means that the same library works both for queue and stacks.


As mentioned above, if you use push() and pop() on a SplQueue object the behave will be of a stack and not a queue, see:

$stack = new SplQueue();
$stack->pop(); // remove the last one


SplQueue Object
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
            [0] => 1
            [1] => 2


Note that 3 got popped and not 1.


Methods to use SplQueue as a queue are: enqueue() and dequeue(), see:

$queue = new SplQueue();
$queue->dequeue(); // remove the first one


SplQueue Object
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
            [0] => B
            [1] => C


Queue and Stack helper methods:

$queue->enqueue('dev'); // or $stack->push('dev');
$queue->enqueue('.to'); // or $stack->push('.to');

$queue->count(); // (int) 2
$queue->isEmpty(); // false

$queue->valid(); // false because right now the pointer is in the last position

// move the cursor to the first position, if you run valid() again after this it will be true

$queue->current(); // "dev", current() just work if you rewind() before call it

$queue->unshift(3); // add the value to the beginning
$queue->current(); // "dev", because we need to move the cursor back to the first position
$queue->current(); // 3

$queue->add(2, 'random-text'); // Add/insert a new value at the specified index

print_r($queue); // Show only 3, "dev" and "to" because we don't rewind() yet.

$queue->rewind(); // move the cursor back to the first position


SplQueue Object
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
            [0] => 3
            [1] => dev
            [2] => random-text
            [3] => .to


Iterating over the queue and stack:

$queue = new SplQueue();
$queue->rewind(); // always rewind the queue/stack, so PHP can start from the beginning.

    echo $queue->current().""; // Show the first one
    $queue->next(); // move the cursor to the next element




There is a bunch of methods that you can use and i cannot show all of it here, you can see every method in the official documentation about SplQueue

So, what programming language are you working right now? do this have queue and stack built-in? if so, how can i use it? if not how do you do it? write a post and show to us how to work with queues and stacks in your favorite programming language! :smile:

Thanks for reading out, I'm working on my English and if there is any misspelling please let me know.

Read more

About Unit Testing

2018-03-13 13:19:45

Hello Developers!

I'm just getting started to writing a Unit Test, okay this is too late, but better than never.

So, I have some confusion on writing unit test. Let's say I create function that sum two given number:

  function sum (num1, num2) {
    return num1 + num2

And the test is:

describe('add', () => {
  it('should add two numbers', () => {
    expect(sum(1, 2)).toBe(3)

If I pass string to my sum function, it's absolutely fail. To make another test, should I return "error" and give a message that we should pass number to parameter not string, or instead we parse it to number if the parameter is not number?

Thanks 😃

Read more

Dev Chats: Sami Peachey

2018-03-13 11:03:22

  Sami Peachey, formally of Google, now Atlassian

Sami Peachey, formally of Google, now Atlassian

This is a article from my "Dev Chats" series where I speak to an awesome developer or techie every week or so. You can read more here. Let me know in the comments if you find these useful to you!


Introduce yourself! Who are you? Where do you work?

Hi! I’m Sami and I work at Atlassian — an Australian software company trying to unleash the potential in all teams. I like making things — memes, food, art, puzzles, games, craft — anything! Some things turn out better than others, but it’s all in trying something new. I’m about to become a dog mum and I am incredibly excited and expect that will consume my entire life. 

Who or what got you into programming?

I originally got into programming back in the MySpace days, when you could write HTML/CSS/JS for your profile page. You always needed to have the best styles and the most obnoxious music playing on it. After that, my home group teacher in high school actually suggested that I apply for the National Computer Science School (NCSS) in Sydney at the start of Year 12. I got in, so I spent the week in Sydney learning Python to make a website. That was probably the best motivator to continue doing programming at university and I still run into people I know from NCSS almost a decade later. 

How was the transition from Australia to the Silicon Valley? Anything catch you by surprise?

The transition from Australia to Silicon Valley has been a pretty easy one I think. It’s actually a lot like my home city of Adelaide, but with more self driving cars and tech nerds everywhere. It’s definitely bizarre — everyone here has a startup idea or is working on some cool new tech, from the people in coffee shops to Uber drivers. I don’t think there’s been any one really big surprise, just lots of small ones. Things are just slightly different here — like shower/bath taps, the lack of ceiling lights, strange banking setups (I’ve legitimately had to write a cheque here), people not understanding some of the words us Aussies use, and that dogs are allowed almost everywhere (which is awesome!). 

What is Google’s BOLD programme, and how has it helped your career?

Google’s BOLD programme has since been renamed to STEP, but it’s an internship program for people traditionally underrepresented in computer science to get the opportunity to be mentored by Googlers while working on a project with another intern. It really helped my career because before I did this internship I didn’t have any confidence in myself. Not only that, but I felt like no one at university respected me or my skills and abilities until I did that internship. It’s silly, but after I came back from it, it felt like people took me seriously and listened to me. It was validation that I can do things and that I could make a career in computer science. It also helped me build a network of other awesome people in tech that I’m still in touch with 5 years later. 

Tell me a little bit about Lend A Skilled Hand - what is it? How are you involved?

Lend a Skilled Hand was the idea that a few of us came up with during Startup Weekend Adelaide in 2015. The idea was that we could match skilled volunteers up with volunteering opportunities where they could use those skills. I helped build the platform for matching volunteers with opportunities. We ended up sending a team of student dentists (with mentors, of course!) to Cambodia to provide free dental work to people who needed it! Unfortunately it’s no longer something we’re running as all five co-founders now live in different countries, but it was incredibly worthwhile both trying to build a startup and being able to help people in need. 

What has been your toughest lesson to learn in your software career so far?

I think the toughest lesson I’ve learnt so far working in software has been how to make tradeoffs while keeping the important things in mind. I for one am a stickler for quality and not screwing the customer. Sometimes the best solution isn’t the most optimal one, or the prettiest one, or the most elegant one. Sometimes you’ve got to do things incrementally rather than all at once. Sometimes you’ve just got to get a fix out. Striking a balance is hard and you usually can’t afford to get stuck in the weeds. It’s a cliché, but the big picture is important.

What would be your number one piece of advice for a successful software career?

My number one piece of advice for a successful software career would be to know when to ask for help. It’s a really hard skill to develop, especially if you’re afraid of being judged for not knowing the answer to something. Sometimes it involves finding the right people to ask these questions of as well. There are no stupid questions, and it’s better to bite the bullet and maybe feel a little bit stupid for a couple of minutes than it is to feel stupid for a couple of days stuck on something. Sometimes it’s good just having reassurance, asking for a sanity check to make sure you aren’t missing something small.

What advice would you have for developers seeking a job at prestigious company like Atlassian or Google?

The advice I’d give for developers looking for a job at companies like Atlassian and Google would be to research the companies beforehand. Read about what they build and what they do and the working environments they have and make sure they’re a fit for you. Do you agree with their mission and their values? Of course, as always, brush up on your programming and problem solving skills, but there’s no point in joining a company you’re not going to be happy working at. You’re interviewing them as much as they’re interviewing you.

What books/resources would you recommend?

I’m not really the kind of person to read tech related books or resources outside of work — I really like to try and keep a clear divide between work and the rest of my life. There’s a ton of good content on leanpub which has helped me learn some of the tech stacks we use at work. I’ve also enjoyed Google’s SRE book which has some great information regardless of what programming you do, and blogs like Joel on Software, Dan Luu, and Steve Yegge. 

Finally, make your shoutout! What would you like the readers to go have a look at?

My shoutout would be to go and give Unlocking the Clubhousea read! I learnt so much from it and I think most people would benefit from reading it one way or another.


Read more

I am a mediocre developer

2018-03-13 10:51:32

I personally know some developers who are very talented and can create wonderful pieces of software with no or little struggle. Because of these gifted individuals, our industry is full of high expectations. But the sad truth is: not everyone is a ninja/guru/rockstar developer.

And that's exactly who I am: a mediocre developer. This article will guide you through surviving in the industry if you are not a genius.

I google the simplest things all of the time

I fail to remember a lot of the things. Like functions and methods from the standard library, arguments positions, package names, boilerplate code and so on.

So, I have to google it. And I do it on a daily basis. I also reuse code from old projects. Sometimes I even copy-paste answers from StackOverflow or Github. Yes, it is actually a thing: StackOverflow Driven Development.

But I am not alone. A lot of other developers do it too. There's a popular twitter discussion which was started by the creator of Ruby on Rails.

But why is that bad in the first place? Well, there are several disadvantages about it:

  1. It allows you to copy bad design decisions or vulnerable code from other people
  2. It forms a specific mindset: if we can not google something up, then "Houston, we have a problem"
  3. When the internet will go down, we won't be able to work

But, I don't think that it is a big problem. It even may serve as your secret weapon. I have some pieces of advice to follow to decrease negative effects.

How to survive:

  1. Use IDE to get autocompletion and suggestions, so you won't have to google the language basics
  2. Remember where (not how) you have already solved this problem. So you can always look there for the solution
  3. All code you paste into the project should be analyzed, refactored, and reviewed later. This way we won't harm the project with the bad code, but help it with the fast solution

I keep things straight simple

Machines always do what they told. Sometimes they are just told to do the wrong thing. So, the main problem in software development is not machines, but developers' mental capacity. It is very limited. So, we - mediocre developers - cannot waste it to create complex abstractions, obscure algorithms, or unreadable long code blocks. Just keep it simple.

But how can we tell that this code is simple and that one is complex? We need to use WTFs/Minute method to measure code quality.

wtf/minute code quality

The principle is very easy and clear to understand. Whenever you find something in the code you do not understand - it is too complex. What can you do?

  1. Rewrite it to have cleanner design
  2. Provide documentation
  3. Add comments to the most tricky parts. But remember, that comments are the code smell themselves

How to write simple things from the beginning:

  1. Use correct names for variables, functions, and classes
  2. Make sure that every part of your program does only one thing
  3. Prefer pure functions over regular functions
  4. Prefer regular functions over classes
  5. Fallback to classes only in a strong need

I do not trust myself

Some developers proved to deliver high-quality code. Like this woman: Margaret Hamilton, lead software engineer of the Apollo Project. In this picture she is standing next to the code she wrote for the moon mission:

Margaret Hamilton

But, whenever I write any code - I do not trust myself. I can screw things up really badly even in the easiest parts of the project. This may include:

  1. Language errors
  2. Logic errors
  3. Design errors
  4. Style errors
  5. Security errors
  6. WTF errors (my all-time favorite!)

And there is no magic book about "learn how to write bug-free code". And it is perfectly normal. All software has bugs. Except this framework though. Deal with it.

The thing is: anyone should not be allowed to write code with obvious errors. At least, we should try. But how can I protect the project from myself? There are multiple ways to do it.

How to survive:

  1. Write tests. Write a lot of tests. Starting from integration tests down to unit tests. Run it in the CI before each pull request. This will protect you from some logical errors
  2. Use static typing or optional static typing. For example, we use mypy with python and flow with javascript. Positive effects: cleaner design and "compile time" checks
  3. Use automated style checks. There are tons of the style checkers for every language
  4. Use quality checks. Some tools run some complex heuristic algorithms on your code base to detect different problems like this line has too many logics inside, this class is not needed, this function is too complex
  5. Review your code. Review it before merging to master. And sometime after the merge
  6. Pay other people to audit your code. This technique has a huge positive influence! Because when developers look at your code for the first time it is easier for them to spot inconsistencies and bad design decisions

It should work not only on my computer

Works on my machine

When my team has developed our first big software project almost ten years ago, we have shipped it as java source files. And it failed to compile on the target server. That was several hours before the presentation to the client. This was a big failure! Somehow we have managed to get it up and running, but it was a life-changing experience.

That happened because there was a lot of configuration and a lot of complexity in the build pipeline. And we could not properly manage the complexity of this system. Since that day to reduce the complexity on this step I try to pack my programs in isolated environments. And to test them in this environment before the actual deploy happens.

In the last years with the rise of docker (and containers in general), it became as easy as ever. docker allows you to run development, tests, and production in the same isolated environment. So, you would never miss any important things along the way.

Wound't you? Talking about myself, I always forget something while creating servers, initially configuring them, or linking them together. There are so many things to keep in mind! Hopefully, we can still automate it. There are different awesome tools to automate your deployment process. Such as: terraform, ansible, and packer. Read about them to find which one do you actually need for your tasks.

I also try to set up CI/CD as soon as possible. So I will be reported if my build failed in testing or in deployment.

How to survive:

  1. Automate anything you use for deploy
  2. Use docker for application development, testing, and deploying
  3. Use deployment tools

After the application is deployed, I still do not trust myself

Oh, at last, my application is in production. It is working now. I can have a short nap, nothing is going to break. Wait, no! Everything is going to break. And yes, I mean it: everything.

Actually, there are some tools to make finding and fixing existing problems easier.

  1. Sentry. When an error happens for any of your users - you will be notified. Has bindings to almost any programming language
  2. Different services and tools to collect logs from multiple processes and servers into one place
  3. Server monitoring. That's the place where you can configure monitors for CPUs, disks, networks, and memory. You can even spot the time to scale before the users will actually break your service

To put it shortly, we need to monitor our application in production. We sometimes use all of these tools, sometimes only the most required parts.

Constantly learning

Wow, that's a lot of things to learn. But that's how it works. If we want to write good software we need to constantly learn how to do it. There are no short ways or magical tricks. Just learn how to be better every single day.

In conclusion, we need to understand two basic things:

  1. Problems happen to everyone. The only thing that matters is how ready we are for these problems
  2. We can reduce the sources of the problems to some acceptable rates

And it has nothing to do with your mental capacity or mindset.

Read more

MongoDB Guide - Getting Started

2018-03-13 09:07:29


In this guide, I am going to cover the essentials in terms of getting up and running with MongoDB. The best way to start learning new technology is to jump straight in and start using it. Therefore, after a brief introduction to MongoDB, the following sections are all about setting up your own MongoDB instance using 4 different hosting methods. Next, I introduce 5 tools that you can use to work with your MongoDB databases. The next section illustrates methods on creating your own database and collection with data. In this section I also provide a number of examples that illustrate some basic queries to query your data.

This guide is summarized as follows:

  • What Is MongoDB
  • MongoDB Setup
    • Install And Host Locally
    • Install And Host Using Docker
    • Host With MongoDB Atlas
    • Host With MLab

  • MongoDB Tools
    • MongoDB Shell
    • MongoDb Compass
    • NoSqlBooster
    • Robo 3T
    • Visual Studio Code

  • Basic MongoDB Commands

  • Conclusion

Lastly, this guide does not cover the reasons to use MongoDB (perhaps a guide for another time). However, I would like to encourage readers to spend some time researching and understanding the rationale between choosing a NoSql database over a Sql database. MongoDB is not a replacement for SQL databases. It is merely an alternative that may (or may not) align better with ones specific requirements.

The following question posted on Quora may be a good starting point:

What are some reasons to use traditional RDBMS over NoSQL?

From my personal experience and perspective, I will mention that one should think very carefully before committing to a NoSql database if your data is required to be agnostic. In this case I have found Sql databases to be a far better choice due to the simplicity of modelling data and exposing it in many different ways for many different applications.

The original guide can be found on my Github repository. Like my Docker Guide, I will be updating and adding more content over time to this repository.

What Is MongoDB?

MongoDB is a database. More specifically, it is an open source document-oriented database that has been designed for scalability and simplicity for both developers and sysadmins. Traditional relational database management systems (RDBMS) like MSSQL, Oracle, MySQL, and PostGreSQL store data in tables having a static schema composed of rows and columns. However, MongoDB stores it's data in JSON-like documents having dynamic schemas.

MongoDB Setup

I will discuss 4 options to get up and running with MongoDB:

  • Install and host locally
  • Install and host in Docker
  • Register for and use MongoDB Atlas (Database As A Service)
  • Register for and use MLab (Database As A Service)

Install And Host Locally

Installing MongoDB is relatively straight forward. There are currently 3 platform (Windows, Linux, OSX) releases available and can be found here

For more specific installation instructions, please see the following links:

Install And Host Using Docker

If you have never used Docker before, then this option is less suitable for you as it implies having some Docker knowledge and experience. However, if you are still interested in pursuing this option, then I recommend viewing my Docker Guide as a starting point. I also have a Docker Cheatsheet that you may find useful.

The Docker image that will be used, is the official Docker MongoDB image.

Run MongoDB

Run MongoDB Using Named Volume

For the following examples I map the Docker MongoDB port of 27017 to a local port of 37017. The reason for this is because I have a local instance on MongoDB running that is already using the 27017 port.

To run a new MongoDB container, execute the following command from the CLI:

docker run --rm --name mongo-dev -p 37017:27017 -v mongo-dev-db:/data/db -d mongo
CLI Command Description
--rm remove container when stopped
--name mongo-dev give container a custom name
-p map container published port to local port
-v mongo-dev-db/data/db map the container volume 'data/db' to a custom name 'mongo-dev-db'
-d mongo run mongo container as a daemon in the background
Run MongoDB Using Bind Mount
mkdir -p mongodb/data/db
docker run --rm --name mongo-dev -p 37017:27017 -v ~/mongodb/data/db:/data/db -d mongo
CLI Command Description
--rm remove container when stopped
--name mongo-dev give container a custom name
-p map container published port to local port
-v ~/mongodb/data/db/data/db map the container volume 'data/db' to a bind mount '~/mongodb/data/db'
-d mongo run mongo container as a daemon in the background

Access MongoDB

Access MongoDB From Local Machine
  1. Type the following command to access MongoDB instance hosted within docker container:
   mongo --host localhost --port 37017
Access MongoDB Via Docker Interactive TTY

There are 2 steps to accessing the MongoDB shell.

  1. Firstly, access the MongoDB container shell by executing the following command:
   docker exec -it mongo-dev bash

This will open an interactive shell (bash) on the MongoDB container.

  1. Secondly, once inside the container shell, access the MongoDB shell by executing the following command:
   mongo localhost

Host With MongoDB Atlas

MongoDB Atlas is a Data as a Service (DaaS) offering, and is hosted in the cloud. There is no installation of MongoDB required and a free tier is available.

To get started, signup for free by registering for a free tier account here. The free tier entitles you to 512MB storage.

Please review the [MongoDB Atlas Documentation] for more information.

Once you have registered and setup your MongoDB instance on Atlas, you will be presented with a dashboard resembling the following image:


Host With MLab

MLab is a Data as a Service (DaaS) offering, and is hosted in the cloud. There is no installation of MongoDB required and a free tier is available.

To get started, signup for free account here. The free tier entitles you to 500MB storage.

Please review the MLab Documentation for more information.

Once you have registered and setup your MongoDB instance on MLab, you will be presented with a dashboard resembling the following image:


MongoDB Tools

The tooling for MongoDB has improved a lot of late and there are many options. I have worked with the following tools and I find them all quite good. It's really a matter of personal preference but I find that I tend to use them together.

MongoDB shell

The MongoDB shell is the default way of interacting with MongoDB databases.

  • Connect to MongoDB
  mongo localhost
  • List Available Databases
  show dbs
  • Create Database
  use message_db
  • Create Collection
  db.messages.insert({ message: 'hello mongodb' })
  • List Collections
  show collections

MongoDB Compass

MongoDB Compass is a Graphical User Interface (GUI) tool that allows one to explore your MongoDB data.

  • It has a free addition available
  • It is cross platform and is available for Linux, Windows, and Mac
  • It supports the following primary features:

    • Run ad hoc queries
    • Perform CRUD operations on data
    • View and optimize query performance

In the following screenshot, I am connected to MongoDB running on my local machine, using MongoDB Compass.


For more information on MongoDB Compass, go here


NoSqlBooster is a Graphical User Interface (GUI) that provides an easy to use interface to work with your MongoDB database.

  • It has a free addition available
  • It is cross platform and is available for Linux, Windows, and Mac
  • It provides the following features:

    • Shell extensions
    • Fluent Query API for MongoDB
    • Query MongoDB with SQL
    • Use Node Modules in your script

In the following screenshot, I am connected to MongoDB running on Docker, using NoSqlBooster.


For more information, please visit the official NoSqlBooster website.

Robo 3T

Robo 3T, is a free GUI tool that can be used to explore your MongoDB databases.

  • It is free to use.
  • It is cross platform and is available for Linux, Windows, and Mac
  • It provides a MongoDB GUI with embedded shell

In the following screenshot, I am connected to a local instance of MongoDB using Robo 3T:


Robo 3T can be downloaded here

For more information, go here.

Visual Studio Code

Using the Azure CosmosDB Extension, one can connect to MongoDB databases in addition to Azure CosmosDB databases.

For more information, see the following links:

Install Azure CosmosDB Extension

  • Launch VS Code
  • Launch Quick Open (ctrl+P) from within VS Code
  • Paste the following command and press enter
  ext install ms-vscode.vscode-azureextensionpack

Configure User Settings

Once you have the extension installed, you will need to ensure that you have a path to Mongo configured in your user settings. Press Ctrl+, to open your user settings. Therefore, because I am using a Linux based OS, I had to add the following setting to the user settings json:

"": "/var/lib/mongodb",

Connect To Mongo On localhost

To connect to MongoDB instance, follow the following steps:

  • Expand Azure Cosmos DB extension panel located in explorer
  • Select the option to 'Attach Database Account'
  • Select MongoDB
  • Enter the connection details to MongoDB instance

The steps are shown below:


Run Commands Using Mongo ScrapBooks

To use the 'ScrapBook Feature', follow the following steps:

  • Attach to your MongoDB instance as seen above
  • Connect to database of your choice
  • Select 'New MongoDB ScrapBook' option to open scrapbook editor
  • Enter MongoDB command
  • Press Ctrl+Shft+' to execute

The steps are shown below:


At the time of this writing, there seems to be an issue with Mongo ScrapBooks

Basic MongoDB Commands

Once you have your MongoDB instance running or hosted, open a mongo shell to your MongoDB server. For the purposes of this demonstration, I will be using my local installation 'localhost'.

  • Connect to MongoDB instance
  mongo localhost
  • Create a 'zips_db' database
  use zips_db
  • Create 'zips' collection
     { "city" : "AGAWAM", "loc" : [ -72.622739, 42.070206 ], "pop" : 5338, "state" : "MA", "_id" : "01001" },
     { "city" : "CUSHMAN", "loc" : [ -72.51564999999999, 42.377017 ], "pop" : 36963, "state" : "MA", "_id" : "01002" },
     { "city" : "BARRE", "loc" : [ -72.10835400000001, 42.409698 ], "pop" : 4546, "state" : "MA", "_id" : "01005" },
     { "city" : "BELCHERTOWN", "loc" : [ -72.41095300000001, 42.275103 ], "pop" : 10579, "state" : "MA", "_id" : "01007" },
     { "city" : "BLANDFORD", "loc" : [ -72.936114, 42.182949 ], "pop" : 240, "state" : "MA", "_id" : "01008" },
     { "city" : "BRIMFIELD", "loc" : [ -72.188455, 42.116543 ], "pop" : 706, "state" : "MA", "_id" : "01010" },
     { "city" : "CHESTER", "loc" : [ -72.988761, 42.279421 ], "pop" : 688, "state" : "MA", "_id" : "01011" },
     { "city" : "CHESTERFIELD", "loc" : [ -72.833309, 42.38167 ], "pop"  177, "state" : "MA", "_id" : "01012" },
     { "city" : "CHICOPEE", "loc" : [ -72.607962, 42.162046 ], "pop" : 3396, "state" : "MA", "_id" : "01013" },
     { "city" : "CHICOPEE", "loc" : [ -72.576142, 42.176443 ], "pop" : 1495, "state" : "MA", "_id" : "01020" }
  • Search Queries

    • Get total collection count
  • Get all zip documents
  • Get 5 zip documents
  • Get zip having a city name of 'BLANDFORD'
  db.zips.find({"city": "BLANDFORD"})
  • Get zip having a city name of 'BLANDFORD', ignore case
  db.zips.find({"city": { "$regex": /blandford/i } })
  • Get zip having a city names of 'BLANDFORD' and 'BRIMFIELD'
  db.zips.find({"city": { "$in": ["BLANDFORD", "BRIMFIELD"] } }).pretty()
  • Get zip having a city names of 'BLANDFORD' and 'BRIMFIELD', ignore case
  db.zips.find({"city": { "$in": [ /blandford/i, /brimfield/i] } }).pretty ()
  • Get zip documents having a population greater than or equal to 30000
  db.zips.find({"pop": { "$gte": 30000}}).pretty()
  • Get all zip documents and use projection to only display city and population
  db.zips.find({}, { "city": 1, "pop": 1, "_id": 0 })
  • Get all zip documents and use projection to only display city and population. Sort documents by city name in descending order
  db.zips.find({}, { "city": 1, "pop": 1, "_id": 0 }).sort({"city": -1})
  • Delete operations

    • Delete all cities called 'CHICOPEE'. Ignore case
  db.zips.remove({ "city": { "$regex": /chicopee/i }})
  • Update operations

    • Update the city of 'BLANDFORD' by setting its population to 10
  db.zips.updateOne({"city": "BLANDFORD"}, { "$set": { "pop": 10}})


This has been a quick introduction to getting started with MongoDB. In this guide we learned about 4 different ways to host your MongoDB instance. We also learned about 5 different tools that can be used to manage your MongoDB databases. Lastly, we explored some basic queries that can be used to create a database, create a collection, insert data, update data, and fetch data.

MongoDB is an exciting and relatively new database that has a massive community and knowledgebase. I encourage all those that are interested to get involved by trying out the following learning resources:

The following MongoDB book is my personal favourite:

If you're interested in certification, please see the MongoDB Professional
Certification Program

Read more

The StackOverflow 2018 Developer Survey Results are live!

2018-03-13 09:07:12

Over 100,000 developers from all over the world took the survey this year and the results are live!

Read more

Conforming to JavaScript Code Styles

2018-03-13 00:32:10

In recent years, the code I have written has been mostly solo work, so I have not had to conform to any coding style guide. This can lead to some bad habits. Recently, however, I began contributing some code as part of the Kinvey team and needed to conform to their style guide.

Here's the thing though - writing code according to a style guide isn't easy. Taking the AirBnB JavaScrpt style guide as an example (which Kinvey's is largely based on), I understand all the rules but following them means breaking a lot of old habits and learning new ones.

For those of you already on teams that follow best practices, these tips may seem obvious. But for those of us making the transition, hopefully this is useful.

Note: This was originally posted on my blog

Linting Helps

Fortunately, tools like ESLint will tell me where I messed up and didn't follow the style guide. This lets me write code as I normally would, but then clean it up to follow the style guide. Running eslint --init will even allow you to configure ESLint to follow some popular style guides beyond the default ESLint recommended styles.

The nice thing is that ESLint let's you share configurations, allowing the team to all easily abide by the same standard. A lot of teams post their rules publicly including:

This is great, and once installed, using eslint --fix can even automatically fix many problems, but wouldn't it be nice if this just worked in your code editor to let you fix style issues as you code?

Prettier is Easier

Prettier is a code formatter the supports multiple languages and editors, including a Visual Studio Code extension (my editor of choice).

Prettier has default styling rules but is configurable. However, since I already have rules defined for ESLint that I want to follow, I can just configure it to use those.

To do this, first click the little gear in the lower-left corner of the editor and choose "Settings".

VS Code Settings

Or type cmd/ctrl+shift+p and search for "Open user settings".

VS Code Settings

All of the Prettier default settings are prefixed by prettier. if you want to see what they are. However, in this case, I just want to configure a user setting for this project by adding the following line to my Workspace Settings.

 "prettier.eslintIntegration": true

So that it looks like so (assuming you don't have any other workspace settings).

VS Code Workspace Settings

You can change your user settings if you want this setting to be used across the board in your projects, but this seems more like a project by project type of setting to me.

Now that this is set, I use cmd/ctrl+shift+p and search for "Format Code" and it automatically formats my JavaScript according to the ESLint style guide I configured previously.

Read more