Back

The Practical Dev

Three tips for writing better CSS

2018-05-16 17:52:16

It's not without its challenges, but nothing I don't experience when working with HTML or JavaScript in my opinion.

Here are a few quick tips I have been thinking about lately with regards to writing better CSS.

1. Reduce far reaching styles

Be careful styling raw HTML elements globally. Make careful use of the cascade. Globals are powerful and need to be throughly considered before being introduced.

For example, I get a little nervous when I see headings getting overly specific styling outside of a default font-weight and font-size. When you start to introduce colors, text transformations, I often see them having to be overridden farther down the document.

Try to reduce that amount of times you have to undo something you had previously declared farther up the cascade.

2. Whitespace

If you are using a build system to minify your CSS, whitespace is a free documentation tool. This is a small but handy tip I picked up from Harry Roberts, he does a great job explaining how whitespace can be semantic.

3. Periodically check the output

This may seem silly to some, "I know what the output will look like, I wrote it!", but Sass and tools alike do a great job of masking what you might expect in the output.

Periodically checking the output has often resulted in me refactoring a bit of my CSS for the better of the project.

This post was originally featured on my website.

Read more

One assert per test - What is that about?

2018-05-16 17:23:25

A colleague outside my team asked me and other colleagues to review a merge request. Although we try to hone our reviewing skills, it doesn't happen all too often that we invite colleagues outside of our specific teams to review. This was one of the cases and I appreciated the opportunity to give my two cents.

I was in the middle of my review when the colleague came by and asked for a first impression. I had looked briefly into the sources so my answer was brief as well: "I don't like your unit tests". When he inquired further why I didn't like them my answer was "Because they are shit." I know it sounds harsh - I am not a mean person, but I'm blunt and tend to exaggerate. Thankfully my colleague knows that and we agreed to talk again when I've had enough time to figure out why I didn't like the tests.

I sat down and took some time to figure out what bothered me and I want to share it with you.

More Than One Assert Per Test

One thing that was clear to me when I looked at the code, was the use of more that one assert per unit test. I'm not a fan of rules without reason so I had to have a closer look to make sure that it wasn't gut feeling. This is a broadly discussed topic, but I want to add my two cents.

One reason that seems to come up every time the internet discusses this topic is:

You won't see if there is something wrong with the second assert if the first one fails.

See here

Although the argument is true, I find it to be weak and tend not to use it. I don't consider it a problem.
I have a different gripe with it, and I've drawn two examples that I want to share with you to illustrate.
For privacy and brevity I simplified the tests, but let the essence intact.

public void testIfRegistryGetsValuesCorrectly() {
  Registry a = new Registry();
  // Imagine some setup code here.
  // originally there was a before-each method

  assertThat(a.getEntryByKey("RandOMstRIng"), equals(a.getEntryByKey("randomstring")));
  assertThat(a.getEntryByKey("   rand   oms.tri ng"), equals(a.getEntryByKey("randomstring")));
  assertThat(a.getEntryByKey("randomst37ring"), equals(a.getEntryByKey("randomstring")));
}

The test works fine and meets the requirements. Cut out all non-characters of the key and make it lowercase before you ask for the value. Everything seems fine.
The problem becomes clear when you touch the tested method and break the 'testIfRegistryGetsValuesCorrectly' test.

As a developer working on the implementation, you're not going to find out what doesn't work any more. At least not at first sight.
You will understand that the test broke, this is what the name tells you.

I suggested refactoring the test to the below code.

public void testIfRegistryIgnoresCases() {
  Registry a = new Registry();
  // Imagine some setup code here.
  // originally there was a before-each method

  assertThat(a.getEntryByKey("RandOMstRIng"), equals(a.getEntryByKey("randomstring")));
}

public void testIfRegistryIgnoresNonCharacters() {
  Registry a = new Registry();
  // Imagine some setup code here.
  // originally there was a before-each method

  assertThat(a.getEntryByKey(" 3  ra8nd   oms.tri ng"), equals(a.getEntryByKey("randomstring")));
}

With these changes in place, you will understand the problematic behavior with a quick glance.
Dots are now allowed? You know what to do:

  1. You broke one well defined test
  2. You fix the test according to new requirements
  3. You rename the test

Explicit Naming

Before continuing I'd like to present a second example to illustrate. Mind you: I simplified the tests, but let the essence intact.

public void testConstructorOneWorksCorrectly() {
  SpecialObjectClass a = new SpecialObjectClass("someID", "attribute1", "attribute2");

  assertThat(a.getID(), equals("someID"));
  assertThat(a.getFirstAttribute(), equals("attribute1"));
  assertThat(a.getSecondAttribute(), equals("attribute2"));

}

public void testConstructorTwoWorksCorrectly() {
  SpecialObjectClass a = new SpecialObjectClass("someID", "attribute1:attribute2");

  assertThat(a.getID(), equals("someID"));
  assertThat(a.getFirstAttribute(), equals("attribute1"));
  assertThat(a.getSecondAttribute(), equals("attribute2"));
}

public void testConstructorTwoWorksCorrectlyWithoutSecondParameter() {
  SpecialObjectClass b = new SpecialObjectClass("someID", "attribute1");

  assertThat(b.getID(), equals("someID"));
  assertThat(b.getFirstAttribute(), equals("attribute1"));
  assertThat(b.getSecondAttribute(), equals("attribute1"));

}

The problem with the above example is similar, but not as obvious. The complex logic lies in the second and optional third parameter. The ID parameter ("someID") is trivial and distracts more than it helps in each of the test cases. If something breaks in the handling of the ID parameter the three unit tests will break for the same reason.

I have two problems with that:

  1. It violates the idea behind a unit test, which is to test single isolated units
  2. It confuses the poor developer changing the tested code in the future. (Who could also be the author itself after a couple of months)

I suggested to refactor to the following form:

public void testConstructorSetsID() {
  SpecialObjectClass a = new SpecialObjectClass("someID", "attribute1", "attribute2");

  assertThat(a.getID(), equals("someID"));
}

public void testConstructorSetsBothAttributes() {
  SpecialObjectClass a = new SpecialObjectClass("someID", "attribute1", "attribute2");

  assertThat(a.getFirstAttribute(), equals("attribute1"));
  assertThat(a.getSecondAttribute(), equals("attribute2"));
}

public void testConstructorTwoSplitsAttributesAtTheColon() {
  SpecialObjectClass a = new SpecialObjectClass("someID", "attribute1:attribute2");

  assertThat(a.getFirstAttribute(), equals("attribute1"));
  assertThat(a.getSecondAttribute(), equals("attribute2"));
}

public void testConstructorTwoSetsSecondAttributeSameValueAsTheFirstIfNotGiven() {
  SpecialObjectClass b = new SpecialObjectClass("someID", "attribute1");

  assertThat(b.getFirstAttribute(), equals("attribute1"));
  assertThat(b.getSecondAttribute(), equals("attribute1"));

}

My naming here may not be perfect, but it gets the point across. And as you may have noticed, the one word that was present in all the unit tests is now gone: "Correctly"

This was my small epiphany.
As a word, "Correctly" should never be part of a test case's name. It does not provide any information at all.

Having found out my two gripes with the tests, I came up with a pattern for myself:

  • test case with multiple assertions -> look out for this dangerous word ("Correctly") and change test case accordingly
  • dangerous word in the title -> look at the assertions to figure out what this test does and change the name

That was my learning of the day: There are words that should not be part of any test case's name, and it's: "Correctly".

There might be more. Look out for them!

P.S.: The colleague that I spoke of in this article, was asked to proofread the article. Besides helping a lot with proper English, he also added a thought of his own. "As developers we often think about naming, and sometimes we seem to miss the most obvious points - or we tend to become lazy."

Read more

Geo Share

2018-05-16 16:36:24

What I built

Imagine you'd like to meet up with your friends, but you are in an open and maybe crowded space, so that beside sharing your own location, you'd love to also see where is everyone else, to eventually converge into a single place on a map without needing to install any native app.

This is Geo Share, a PWA where you can either be the host, or simply a guest of any other person that shared a unique URL with you.

Demo Link

You can become a host simply visiting geoshare.now.sh.

Once you've found yourself on the map, and clicked on top right 👥 icon, you'll have in your device clipboard a unique URL you can paste to your friends.

That's it, anyone using that link will see everyone else aware of such link and, as long as you are using the app, also see you as host of the group (dark icon).

Link to Code

https://github.com/WebReflection/geoshare

How I built it

Additional Resources/Info

The rest of the code is written by me and based on ES Modules, bundled through asbundle, sanitized via Babel, and minified via UglifyJS.

Common PWA techniques such ServiceWorker and a manifest.json are in place too.

Read more

Making an animated nav component - WotW

2018-05-16 16:12:46

Welcome to the Widget of the Week series, where I take gifs or videos of awesome UI/UX components, and bring them to life with code.

Today's the turn for a navigation component with four colorful icon buttons.The inspiration comes from this submission and it looks like this:

reference

Preparations

For today's widget we will be using Vue.js for the interactions, and TweenMax for animations. If you want to follow along you can also fork this codepen template that already has the dependencies.

We will also use FontAwesome icons, so make sure that you add this link to import them:

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">

The initial markup

We will start with the HTML. For this component we need just a container and the buttons. As I just mentioned above, we will use the FontAwesome icons for the buttons, they're not exactly the same as in the original submission but they're good enough.

<div id="app">
  <div class="btn-container">
    <div class="btn">
      <i class="fas fa-comment"></i>
    </div>
    <div class="btn">
      <i class="fas fa-user"></i>
    </div>
    <div class="btn">
      <i class="fas fa-map-marker"></i>
    </div>
    <div class="btn">
      <i class="fas fa-cog"></i>
    </div>
  </div>
</div>

Right now we should have the four icons, it's time to make it look more like the final product.

Styling

In the container we need a background color, I'll use black for now but later we will change that programatically. Also I'll use flex and justify-content to center the elements horizontally, then just some padding to vertically align them.

.btn-container {
  display: flex;
  background-color: black;

  /* center vertically */
  padding-top: 150px;
  padding-bottom: 150px;
  /* center horizontally */
  justify-content: center;
}

For the buttons there's a bit of more work needed, we'll use inline-block so that they render beside each other.

We need to define the sizes of both the button and it's content, along with some default colors, then use border radius to make them circles and also a couple of rules to align the icons correctly:

.btn {
  display: inline-block;
  cursor: pointer;
  width: 50px;
  height: 50px;
  margin: 5px;
  font-size: 25px;
  color: gray;

  /*  Circles  */
  border-radius: 25px;
  background-color: white;

  /* center icons */
  text-align: center;
  line-height: 50px;

  /* remove touch blue highlight on mobile */
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

And now we should have something like this:

html+css

The behavior

Now in our Vue instance we will start declaring the data that we need to use on the component. With a color picker, I took the different colors for buttons and backgrounds and put them inside a structure so we can reference them in the future:

new Vue({
  el: '#app',
  data: {
    buttons: [
      {icon: 'comment', bgColor: '#DE9B00', color: '#EDB205'},
      {icon: 'user', bgColor: '#3EAF6F', color: '#4BD389'},
      {icon: 'map-marker', bgColor: '#BE0031', color: '#E61753'},
      {icon: 'cog', bgColor: '#8E00AC', color: '#B32DD2'}
    ],
    selectedBgColor: '#DE9B00',
    selectedId: 0
  },
})

Also I already declared a variable that will have the current background color and the id of the selected button.

Since we also have the icon data inside the buttons array, we can change our HTML code to render with a v-for the buttons and become more dynamic:

<div id="app">
  <div class="btn-container" :style="{'backgroundColor': selectedBgColor}">
    <div v-for="(button, index) in buttons" 
         :key="index" 
         @click="selectButton(index)"
         :ref="`button_${index}`"
         class="btn">
      <i :class="['fas', `fa-${button.icon}`]"></i>
    </div>
  </div>
</div>

This code is also already binding the background color to the btn-container div style.

getting closer

Notice that we added an @click handler that should trigger a function called selectButton, also the ref attribute will help us reference the buttons when we need to animate them.

Clicking a button

We need to declare first the selectButton method in our Vue instance:

// ... data,
  methods: {
    selectButton (id) {
      this.selectedId = id
    }
  }

After this the selectedId will change on every click to values between 0-3, but that doesn't seem to do anything to our component. We need to start animating things!

Let's begin animating the simplest part, the background color. For that we need to make a computed property that will get the selected button data which will help us to get the corresponding background color.
Later when we change the selectedId we will be able to tween the color to the current selected one.

// ... data
 methods: {
    selectButton (id) {
      this.selectedId = id
      this.animateBgColor()
    },
    animateBgColor () {
      TweenMax.to(this, 0.2, {
        selectedBgColor: this.selectedButton.bgColor
      })
    }
  },
  computed: {
    selectedButton () {
      return this.buttons[this.selectedId]
    }
  }

We should have a working transition of the background color when clicking any button.

Animating the buttons

Buttons are going to be a bit trickier to animate. For starters, we will need to save a reference to the previously active button and the next button to activate.

To achieve that we can use $refs with the index of the selected button before setting the new one, like this:

// ... data
  methods: {
    selectButton (id) {
      const previousButton = this.$refs[`button_${this.selectedId}`]
      const nextButton = this.$refs[`button_${id}`]
      // ... rest of code

Now that we have those references we should be able to run a couple of methods, one to deactivate the previous button and the other one to activate the new one:

// ... methods
    selectButton (id) {
      const previousButton = this.$refs[`button_${this.selectedId}`]
      const nextButton = this.$refs[`button_${id}`]

      this.selectedId = id
      this.animateBgColor()

      this.animateOut(previousButton)
      this.animateIn(nextButton)
    },
    animateIn (btn) {      
      // TODO activate button
    },
    animateOut (btn) {
      // TODO deactivate button
    }

Before coding that part we need to stop and think how the buttons should animate. If we analize the gif, the button animation can be split in two changes, one for the colors of the button and icon and the other one for the width of the button.

The colors transition looks really straightforward, the button's background changes to white when inactive, and to the color property when active. For the icon, it just changes between gray and white.

The interesting thing is with the button width animation, it looks kinda "elastic" because it goes a bit back and forth at the end.

Playing with the GSAP ease visualizer I came with the props that closely match the easing of the original animation. Now we can finish coding the animateIn and animateOut methods:

// ... methods
   animateIn (btn) {      
      // animate icon & bg color
      TweenMax.to(btn, 0.3, {
        backgroundColor: this.selectedButton.color,
        color: 'white'
      })

      // animate button width
      TweenMax.to(btn, 0.7, {
        width: 100,
        ease: Elastic.easeOut.config(1, 0.5)
      })
    },
    animateOut (btn) {
      // animate icon color
      TweenMax.to(btn, 0.3, {
        backgroundColor: 'white',
        color: 'gray'
      })

      // animate button width
      TweenMax.to(btn, 0.7, {
        width: 50,
        ease: Elastic.easeOut.config(1, 0.5)
      })
    }
  },

We're almost done, there's just a small detail. When the app starts, the component doesn't look to have a selected button. Luckily that can be quickly solved by calling the selectButton method inside the mounted hook:

  mounted () {
    // initialize widget
    this.selectButton(0)
  }

And now the final result!

That’s it for this Widget of the Week.

If you're hungry for more you can check other WotW:

Also if you want to see a specific widget for next week, post it in the comments section.

Read more

Ensuring execution, retrying failures and reraising exceptions in Ruby

2018-05-16 15:34:06

Raised exceptions can be rescued to execute an alternative code path when things go wrong, but there are more ways to handle exceptions. In this edition of AppSignal Academy, we'll go over the retry and ensure keywords, and we'll look at reraising rescued exceptions.

Let's pretend we're communicating with an unreliable web API. Aside from it being down every once in a while, it's so slow that requests to it can take seconds. Our library depends on this API, and we need to make it as resilient as possible.

ensure

The ensure keyword is used for ensuring a block of code runs, even when an exception happens.

In our library, we'd like to ensure the TCP connection opened by Net::HTTP.start is closed, even if the request fails because it times out, for example. To do this, we'll first wrap our request in a begin/ensure/end block. The code in the ensure part will always run, even if an exception is raised in the preceding begin block.

In the ensure block, we'll make sure to close the TCP connection by calling Net::HTTP#finish unless the http variable is nil, which can happen opening the TCP connection fails (which will also raise an exception).

require "net/http"

begin
  puts "Opening TCP connection..."
  http = Net::HTTP.start(uri.host, uri.port)
  puts "Sending HTTP request..."
  puts http.request_get(uri.path).body
ensure
  if http
    puts "Closing the TCP connection..."
    http.finish
  end
end

Note: We close the TCP connection manually to allow us to use the connection when retrying later. However, since Net::HTTP.start takes a block which handles ensuring the connection is closed, the sample above can be rewritten to remove the ensure. Interestingly enough, the ensure block is also how this is implemented in Net::HTTP itself.

retry

The retry keyword allows retrying a piece of code in a block. Combined with a rescue block, we can use it to try again if we fail to open the connection, or if the API takes too long to respond.

To do that, we'll add a read_timeout to the Net::HTTP.start call which sets the timeout to 10 seconds. If a response to our request hasn't come in by then, it'll raise a Net::ReadTimeout.

We'll also match on Errno::ECONNREFUSED to handle the API being down completely, which would prevent us from opening the TCP connection. In that case, the http variable is nil.

The exception is rescued and retry is called to start the begin block again, which results in the code doing the same request until no timeout occurs. We'll reuse the http object which holds the connection if it already exists.

require "net/http"

http = nil
uri = URI("http://localhost:4567/")

begin
  unless http
    puts "Opening TCP connection..."
    http = Net::HTTP.start(uri.host, uri.port, read_timeout: 10)
  end
  puts "Executing HTTP request..."
  puts http.request_get(uri.path).body
rescue Errno::ECONNREFUSED, Net::ReadTimeout => e
  puts "Timeout (#{e}), retrying in 1 second..."
  sleep(1)
  retry
ensure
  if http
    puts "Closing the TCP connection..."
    http.finish
  end
end

Now, our request will retry every second until no Net::ReadTimeout is raised.

$ ruby retry.rb
Opening TCP connection...
Executing HTTP request...
Timeout (Net::ReadTimeout), retrying in 1 second...
Executing HTTP request...
Timeout (Net::ReadTimeout), retrying in 1 second...
Executing HTTP request...
Timeout (Net::ReadTimeout), retrying in 1 second...
Executing HTTP request...
... (in an endless loop)

While that might make sure no exception is raised for any timeout ever, retry-hammering it like this certainly won't help to get that API back up again. This is problematic because this code will keep looping forever if the API remains unresponsive. Instead, we should spread our retries and give up after a while.

Giving up: reraising exceptions using raise

When an exception is rescued, the raised exception object is passed to the rescue block. We can use that to extract data from the exception, like printing the message to the log, but we can also use it to reraise the exact same exception, with the same stack trace.

begin
  raise "Exception!"
rescue RuntimeError => e
  puts "Exception happened: #{e}"
  raise e
end

Since we have access to the exception object in the rescue block, we can log the error to the console or an error monitor. In fact, rescuing and reraising is exactly how AppSignal's integrations track errors.

Note: Ruby stores the last raised exception in a variable named $!, and the raise keyword will use it by default. Calling raise without any arguments will reraise the last exception.

In our library, we can use reraising to take the pressure of the API after a couple of retries. To do that, we'll keep track of the number of retries we've done in the retries variable.

Whenever a timeout happens, we'll increment the number and check if it's less than or equal to three, because we'd like to retry three retries at most. If so, we'll retry. If not, we'll raise to reraise the last exception.

require "net/http"

http = nil
uri = URI("http://localhost:4567/")
retries = 0

begin
  unless http
    puts "Opening TCP connection..."
    http = Net::HTTP.start(uri.host, uri.port, read_timeout: 1)
  end
  puts "Executing HTTP request..."
  puts http.request_get(uri.path).body
rescue Errno::ECONNREFUSED, Net::ReadTimeout => e
  if (retries += 1) <= 3
    puts "Timeout (#{e}), retrying in #{retries} second(s)..."
    sleep(retries)
    retry
  else
    raise
  end
ensure
  if http
    puts 'Closing the TCP connection...'
    http.finish
  end
end

By using the retries variable in the call to sleep, we can increase the wait time for every new attempt.

$ ruby reraise.rb
Opening TCP connection...
Executing HTTP request...
Timeout (Net::ReadTimeout), retrying in 1 second(s)...
Executing HTTP request...
Timeout (Net::ReadTimeout), retrying in 2 second(s)...
Executing HTTP request...
Timeout (Net::ReadTimeout), retrying in 3 second(s)...
Executing HTTP request...
Closing the TCP connection...
/lib/ruby/2.4.0/net/protocol.rb:176:in `rbuf_fill': Net::ReadTimeout (Net::ReadTimeout)
...
from reraise.rb:13:in `<main>'

Our request is retried three times before the code gives up and reraises the last error. We can then handle the error one level up, or crash our app if it can't finish its job without the API's response.

A resilient web API client

By combining these methods, we've built a resilient web API client in about twenty lines of code. It'll retry requests if it's down or unresponsive, and we'll give up when it doesn't come back up again.

We hope you learned something new about handling exceptions and would love to know what you thought of this article (or any of the other ones in the AppSignal Academy series). Please don't hesitate to let us know what you think, or if you have any Ruby subjects you'd like to learn more about.

Read more

JVM Primer Part 2 - Debugging memory issues

2018-05-16 14:39:13

This is part 2 of my series on JVM Memory management and debugging.
Read part 1 here:

In this post, we will cover symptoms of memory issues for JVM-based applications, which tools we can use to diagnose them and how we can fix them.

Symptoms

Here are a few symptoms of memory issues:

  1. Poor application performance
  2. Abnormal memory usage
  3. OutOfMemory errors (OOME)

Poor Application Performance

  1. Application not performing to expected level
  2. Long response times
  3. Dropping client requests
  4. Stuck threads
  5. Service unavailability
  6. Large gaps in timestamps in application logs

Causes of memory problems:

  1. Misconfigured memory
    • Old generation memory space is sized smaller than live-set of objects. This triggers a major garbage collection (GC), resulting in larger pauses.
    • Code cache is smaller than generated compiled code footprint
    • Young generation is not sized appropriately leading to premature promotion of objects
    • PermGen / Metaspace not sized correctly leading to full GC
  2. Memory leaks - Unintentional retention of objects in memory spaces
    • Unintentional references to set of objects in heap
    • Not dereferencing classloader instances appropriateky
    • Not releasing native resources appropriately
  3. Excessive use of finalizers
    • Objects with finalizers may delay their own GC
    • Finalizer thread needs to invoke finalize() method of the instances before reclaiming them
    • There can only be 1 Finalizer thread. If it does not keep up with rate at which objects become available for finalization, JVM fails with OOME
    • Pending finalizer objects are essentially accumulated garbage
    • Finalizers deprecated in Java 9
  4. Explicit GC calls
    • System.gc() and diagnostic data collections can cause long pauses
    • -XX:+DisableExplicitGC can disable System.gc() calls
    • -XX:+PrintClassHistogram also calls an explicit GC when receiving kill -3 signal

OutOfMemoryError

  • Hierarchy : Throwable -> Error -> VirtualMachineError -> OutOfMemoryError (Unchecked exception)
  • Thrown when JVM runs out of space in various memory spaces or cannot proceed further with process execution. Some of the possibilities:
    • Heap space full
      • JVM already invoked full GC but could not free up space
      • Heap may be sized smaller than app footprint or app is unnecessarily holding on to some set of objects in heap

    • GC overhead limit exceeded
      • Too many GCs with very less space claimed
      • Application threads not getting any CPU cycles

    • Requested array size exceeds VM limit

    • PermGen space / Metaspace / compressed class space
      • Full GC invoked but unable to free space in Metaspace and application is attempting to load more classes
      • Metaspace by default "unlimited" but can be controlled by MaxMetaspaceSize. By default, 1 GB reserved for compressed class space
      • Make sure that -Xnoclassgc is not in use as it prevents unloading of classes

    • Native memory - out of swap space / stack trace with native method
      • Native space used for Java thread stacks, loaded jars, zips, native libraries, native resources like files; mem allocated from native code
      • Unable to allocate more native memory or to create new threads or native memory leaks
      • Running 32 bit JVM on 64 bit machine puts 4 GB limit on process size
      • Position of Java heap can put a cap on max size of native heap. Can be controlled by option -XX:HeapBaseMinAddress=n to specify address native heap should be based at

CodeCache warnings

  • warning message printed by JVM saying CodeCache full, compiler has been disabled.
  • No OOME when code cache is full
  • Emergency cleanup undertaken by Sweeper. This may discard compiled code and JIT may need to perform optimizations again
  • Ensure appropriate size of CC using ReservedCodeCacheSize option

Direct Buffer Memory

  • ByteBuffer.allocateDirect(N) : Direct buffers which are garbage collected using phantom references and a reference queue
  • Unlimited memory by default but can be controlled by -XX:MaxDirectMemorySize=n
  • Used by Java NIO. Heap ByteBuffer for I/O uses temporary direct ByteBuffer

Diagnostic Data, Data Collection and Analysis Tools

Troubleshooting Memory leaks

  1. Confirm memory leak

    • Monitor heap usage over time
    • If full GCs unable to claim space in OldGen, could be config issue
    • Heap size may be too small -> Increase heap size and monitor! If issue persists, it could be a memory leak
    • -XX:+GCTimeLimit sets upper limit on amount of time GCs can spend in percent of total time, default 98%
    • -XX:+GCHeapFreeLimit sets lower limit on amount of space that should be freed by a GC, represented as % of max heap, default is 2%
    • OutOfMemoryError is thrown after a full GC if previous 5 consecutive GCs were not able to keep the GC cost below GCTimeLimit or free up at least GCHeapFreeLimit space
    • PermGen/Metaspace may be too small if frequent Full GCs do not claim any space
  2. Diagnostic data and analysis

    • GC logs are helpful for determining heap requirements, finding out excessive GCs and long GC pauses and in configuration of memory spaces
      • For Java 9+, G1 options are: -Xlog:gc*,gc+phases=debug:file=gc.log . For non G1, -Xlog:gc*:file=gc.log. For older JVMs, -XX:+PrintGCDetails, -XX:+PrintGCTimeStamps, -XX:+PrintGCDateStamps, -Xloggc:gc.log
      • For checking metaspace, -verbose:class or -XX:+TraceClassLoading , -XX:+TraceClassUnloading
      • We can analyse logs through manual inspection, GCViewer, GCHisto, gceasy.io
    • Heap dumps help determine unexpected memory growth and memory leaks.
      • We can take heap dumps in follwing ways:
        • jcmd pid GC.heap_dump heapdump.dmp
        • jmap -dump:format=b,file=snapshot.jmap pid
        • JConsole or Java Mission Control using MBean HotSpotDiagnostic
        • JVM option heap dump on OOM error : -XX:+HeapDumpOnOutOfMemoryError . Frequent full GCs can delay collection of heap dump and restart of the process
      • Eclipse Memory Analyzer Tool (MAT) shows leak suspects, histograms, unreachable objects, duplicate classes, reference chains to GC roots, allows using OQL to explore heap dumps.
      • JOverFlow for JMC and Java VisualVM, YourKit (a commercial profiler) can all take heap dumps.
    • Heap histograms - quick view of objects in heap
      • Collect using:
        • -XX:+PrintClassHistogram and SIGQUIT on Posix and SIGBREAK on Windows
        • jcmd pid GC.class_histogram filename=histo
        • jmap -histo pid core_file
        • jhsdb jmap (Java 9)
    • Java flight recordings - unexpected memory growth and memory leaks, GC events
      • Enable Heap Statistics. Can introduce additional performance overhead
      • To create a flight recording : -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=delay=20s,duration=60s,name=Rec,filename=lol.jfr,settings=profile
      • Flight recordings can find out type of leaking objects but you need heap dumps to find out what is causing the objects to leak
    • Finalizers
      • Collect data using JConsole, jmap
      • Analyse using Eclipse MAT / Visual VM using heap dumps
    • Native Memory
      • Native Memory Tracker output - tracks native memory used internally by JVM, not for external libraries. Start JVM with NativeMemoryTracking option
      • pmap, libumem, valgrind, core files

Conclusion

In this series, we have taken a look at how the JVM manages memory and how the garbage collection process works. We have also gone through how to diagnose memory issues, which tools to use to collect and analyze diagnostic information and some JVM options which can affect application performance.

Read more

Security and the Future of it with Laura Bell

2018-05-16 12:17:14

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!

Laura Bell. Security Nerd.

Laura Bell. Security Nerd.

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

I’m Laura Bell, you might have met me on the internet or at conferences or as part of my work at SafeStack. I’m a 33 year old security nerd from Auckland, New Zealand with two missions in life:

  1. Make sure everyone has the right to be safe on the internet

  2. Help people build amazing (secure) things, really, really fast

I founded SafeStack in 2014 as a way to change the way we do security in fast paced environments and to bring some innovation to the way we do security.

Who or what got you into security?

I’d just graduated university and took a job doing Java development for the UK government. It was not as much fun as the brochure promised and soon I had many feelings and opinions about the quality of the code I was writing.

Eventually these feelings and opinions reached the ears of the security team who gave me an option other than get fired. I moved to work with them and switched from development to penetration testing and red teaming (hacking for good not evil).

Why should developers worry about security?

There are two ways to see this.

Most developers aim to build high quality code that minimises defects first time around. By doing so they can move onto more exciting challenges and provide an excellent product. Security is a quality measure in the same way that scalability and performance are. We need these things as much as we do functionality.

Secondly, thinking about security as a developer can mean embracing simplified designs or removing ambiguity, confusion and complexity. These are the places that security vulnerabilities like to hang out. When we start working in this way our code is better quality, easier to maintain and more secure. This all means we can go even faster.

I recently heard that security jobs will outlast programming jobs. Do you think this is true? Why?

I don’t agree.

Security jobs in their current format will cease to exist soon enough. The future won’t need isolated specialists for most roles, it will need proactive hybrids.

I think the reality is all of our roles are evolving. The best of us will just adapt and evolve along with them.

How has conference speaking (we’ve all see you around!) impacted your business and career? What’s the strategy there?

Hahahah Strategy?

Honestly, I started speaking internationally as a bet to myself. I wanted to find a way to get better at speaking and so in a sudden burst of enthusiasm, I applied to some massive USA conferences in the dead of the night. I then promptly forgot all about it.

Four months later, my inbox filled with acceptances and I had to step up and give it a shot.

SafeStack is a tiny bootstrapped company without a marketing team or budget. Conference speaking has given us a chance to show our approach globally despite the lack of resources.

It’s been great for the business obviously but on a personal level it led to the book (Agile Application Security by O’Reilly) and a network of peers around the world that I can learn from and share with.

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

That it’s ok to have a different approach to those around you and that it’s ok to fail. Security is a field that likes to play it safe. We have stuck with the same ways of doing things for years without challenge. Trying new things is scary but it means we can learn and grow. Embrace failure as part of this process.

When you mime programming to somebody, do you use T-rex arms, or wiggly fingers?

Wiggly fingers for life.

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

Embrace serendipitous discovery. Don’t plan your career, the world is changing to quickly. Embrace the challenges around you, take risks and see where you end up.

Have you got any hobbies outside of your job? Do you think they help your tech career in any way?

I have a 4 year old daughter so I spend a lot of time learning about animals and dinosaurs right now. I’m also an amateur photographer, surfer and do Yoga.

Does it help my career?

Directly? probably not. However security is a very stressful space (as is being a company founder) making room for hobbies outside of technology gives you the headspace and relaxation to tackle hard challenges in your day job. If you don’t have an outlet, you get a little broken.

What books/resources would you recommend?

Of course you should all be checking out the latest OWASP Top 10 that was released in November 2017 https://www.owasp.org/index.php/Category:OWASP_Top_Ten_Project but here are a few books and things that you might also find interesting.

Books:

(Oh and I guess my book but that feels gross to talk about)

Videos:

https://www.ted.com/talks/apollo_robbins_the_art_of_misdirection

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

At this time of year, go look at something other than your work. Go out and get a break. Reconnect with the world and your loved ones.

If that’s not your jam however you might want to give some love to your favourite open source projects. Every OS project needs extra hands and we need to all get stuck in to make them safer to use and more sustainable.

Read more

The syntactical Sugar Classes of JavaScript

2018-05-16 11:40:18

In my previous article;

we looked at Getters and Setters as they apply to objects in JavaScript and i recall promising to show us how we could use getters in Es6 classes, We would also learn a great deal about classes.

First of; i wouldn't be talking about them differently. We would talk about classes and while writing some of our codes, i would show you how getter is used in the codes.

Classes
They are a tool most developers use to quickly produce similar objects.

let dog = {
            name: "lucky",
            breed: "American Bulldog.",
            age: 2

          }

They code above looks like what we already know - objects. Now imagine you own a dog daycare and want to create a catalog of all the dogs who belong to the daycare, instead of using the syntax above for every dog that joins the daycare which would be undoubtedly burdensome, we can create a Dog class that serves as a template for creating new dog objects. Since you would be saving yourself they energy required to rewrite a code you already wrote for a particular dog, it would be safe to say that classes are a great way to reduce duplicate code and debugging time.

class syntax:

class Dog{
    constructor(){

    }
    method1(){

    }
    method2(){

    }

}

Aha....i get the question in our heads, what is a constructor?...felt the same way though, lets talk about it a little.Also note the methods in the example above are normal functions we know, but when written inside a class they are called methods and are written without the function keyword.

Constructors
One notable difference that exists between objects and classes is the use of constructors in classes. JavaScript calls the constructor method every time it creates a new instance of a class.

class Dog{
    constructor(name,breed){
        this.name = name;
        this.breed = breed;
    }
    method1(){

    }
    method2(){

    }
    get method3(){

    }

}

inside of the constructor() method we use the this keyword. In the context of a class this refers to an instance of that class.

What then is an Instance?
An instance is an object that contains the property names and methods of a class, but with unique property values.

What are Methods?
Class methods and getter syntax is the same as it is for objects except you cannot include commas between methods. But how do we call this methods;

Method calls:
Before a method is called the instance of the class is created and assigned to a variable.

const lucky = new Dog('lucky','American Bulldog');

The syntax for calling methods and getters on an instance is the same as calling them on an object, which is done by appending the instance with a period, then the property or method name. For methods, you must also include the opening and closing parenthesis. But for getters you don't need to. In the example below we would show this;

lucky.method1() //when we are calling methods
lucky.method3 //when we are calling getters, no parenthesis

A full example of a class, constructors and method calls we can see below;

class Dog{
    constructor(name,breed){
        this._name = name; //when we are using getter and setters we add underscore to our properties.
        this._breed = breed;
        this._behaviour = 0;
    }
    get name(){
        return this._name;
    }
    get breed(){
        return this._breed;
    }
    get behaviour(){
        return this._behaviour;
    }
    incrementBehaviour(){
        this._behaviour++;
    }
}

let lucky = new Dog('lucky','American Bulldog');
console.log(lucky.name)//returns the dogs name
console.log(lucky.breed)//returns the dogs breed
console.log(lucky.behaviour)//return the value of behaviour
lucky.incrementBehaviour(); //increases the count for the behaviour property
console.log(lucky.behaviour)//returns the new value after incrementing.

But what if your daycare starts growing and we start accepting other pets like cats, etc. Are we going to be creating different classes for each of them?

class Cat{
    constructor(name,breed){
        this._name = name; //when we are using getter and setters we add underscore to our properties.
        this._breed = breed;
        this._behaviour = 0;
    }
    get name(){
        return this._name;
    }
    get breed(){
        return this._breed;
    }
    get behaviour(){
        return this._behaviour;
    }
    incrementBehaviour(){
        this._behaviour++;
    }
}

let kitty = new Dog('Kiity','American Bulldog');
console.log(kitty.name)//returns the dogs name
console.log(kitty.breed)//returns the dogs breed
console.log(kitty.behaviour)//return the value of behaviour
kitty.incrementBehaviour(); //increases the count for the behaviour property
console.log(kitty.behaviour)//returns the new value after incrementing.

That would also mean us repeating codes, something we were avoiding initially, but how do we solve this problem, this is where we use a concept in Es6 classes called Inheritance.

Inheritance:
When multiple classes share properties or methods they become candidates for inheritance. A tool developers use to decrease the amount of code they need to write.
With inheritance, you can create a parent class (also known as a Super Class) with properties and methods that multiple child classes (known as sub classes) share. The child classes inherit the properties and methods from their parent class.

class subclass illustration

In our example we would need to create a parent class Animal, that other classes like cat and dog would inherit from.

class Animal{
    constructor(name,breed){
        this._name = name; //when we are using getter and setters we add underscore to our properties.
        this._breed = breed;
        this._behaviour = 0;
    }
    get name(){
        return this._name;
    }
    get breed(){
        return this._breed;
    }
    get behaviour(){
        return this._behaviour;
    }
    incrementBehaviour(){
        this._behaviour++;
    }
}

Now creating the Dog class to inherit from the parent class Animal;

class Dog extends Animal{
    constructor(name,breed,sound){
        super(name,breed);
        this._sound = sound;
    }
    get sound(){
        return this._sound;
    }
}

class Cat extends Animal{
    constructor(name,breed,size){
        super(name,breed);
        this._size = size;
    }
    get size(){
        return this._size;
    }
}

let lucky = new Dog('lucky','Caucasian','barks');
console.log(lucky.name); //logs lucky
console.log(lucky.breed); //logs caucasian
console.log(lucky.sound); //logs bark
console.log(lucky.behaviour); //logs current value
lucky.incrementBehaviour(); //increments behaviour
console.log(lucky.behaviour); //logs new value

let kitty = new Cat('kitty','dontknow','4kg');
console.log(kitty.name); //logs kitty
console.log(kitty.breed); //logs dontknow
console.log(kitty.size); //logs 4kg
console.log(kitty.behaviour); //logs current value
kitty.incrementBehaviour(); //increments behaviour
console.log(kitty.behaviour); //logs new value

In the example above, we created new classes that extends the Animal class, lets pay special attention to our new keywords, extends and super;

  1. Extends keyword makes the method of the animal class available inside the cat or dog class.
  2. The constructor, called when you create a new cat or dog object, accepts three arguments(name,breed and sound or size).
  3. The super keyword calls the constructor of the parent class. In this case, super(name,breed) passes the new argument of the cat class to the constructor of the animal class. When the animal constructor runs it, it sets this._name and this._breed = name and breed respectively; for new cat or dog instance.
  4. sound and size are new properties unique to dog and cat respectively,so we set it in the constructor.

Note:
We call super on the first line of the constructor before calling this, just so as to avoid getting JavaScript's reference error thrown.
When we call extends in a class declaration, all of the parent methods become available to the child class.

SUmmary:

  1. Classes are templates for objects
  2. JavaScript calls the constructor method when we create a new instance of a class.

Read more

Is Javascript a compiled language?

2018-05-16 11:26:21

It really Is...

Why do people still look at JS as a dynamic or interpreted language?

There's a lot of misconception about the compiling of JS, and still now, with lots of information on the web, most people still argue with that and still don't know how exactly JS works before the runtime phase.

Javascript is a compiled language...

despite the fact that the compiling of JS works in a different way, if compared to other compiled language, It's still following some rules that reflect the process of compiling

First... we've to quote this from wikipedia:

A compiler is computer software that transforms computer code written in one programming language (the source language) into another programming language (the target language).

We all knows that computers don't speak Java or JS or Python and don't matter which language we're using, we're always translating our code into something that the machine can understand... but that's not the most important thing for now.

Important is... this kind of translation is called code generation and it's input is the Abstract Syntax Tree (AST) that is about some nested elements that represent the structure of the program. The structuring of this tree, happens in the parsing phase of compiling.

Of course we've to provide something to create this AST ... and we do ... we provide an array of tokens, from the previous compiling lexing phase.

let dog = labrador;

will be tokenized like this

let,dog,=,labrador,;

This splitted version of our code, means something for the language and creates that stream of informations to generate the AST.
We now have a variableDeclaration and an assignment and so on... in our tree.

I've been not very specific, because this post is about the fact that in JS all of this, It's happening.

Yes.

Javascript follows all of this compiling phases, in the order:

  1. Lexing
  2. Parsing
  3. Code Generation

The JS compiling It's not happening to make it works on different platforms or stuff like that... but It's happening

This is not something you should know as generic... this is something that can totally change your point of view about lots of JS behaviours.

Just quick examples are lexical scoping and hoisting.

The variable declarations in JS happens during the lexing phase, while the assignement happens on runtime and that's why hoisting It's happening in a more technical and correct point of view.
The scope, in JS, It's defined in It's lexing phase and that's why JS has got the lexical scoping definition.

What about closures ? More complex... but still something that happens because of scope reference and lexical scoping.

So, people, JS is quickly compiled, everytime... and there's lot of optimization included in the engine to make it possible without any collateral problem in performances, that you can break if you're not conscious about this stuff.

Have fun, looking for more info!

Read more

Highlights from Frontend Tooling Survey 2018

2018-05-16 10:50:43

Originally I posted these highlights on Syndicode blog.

To shed light on the tools that frontend developers are using right now in the industry I picked some highlights from Frontend Tooling Survey 2018. The survey was held this March and 5,097 frontend developers took part in it.

The pool of questions that formed the results of Frontend Tooling Survey 2018 consisted of 24 questions represented below:

By clicking on the question you're interested in, you can explore the detailed result.

And here I found some

Highlights from Frontend Tooling Survey 2018

  1. General frontend experience - 54.23% of respondents have been working with front-end technologies for over 5 years.
  2. CSS knowledge - 89.9% of respondents stated that their knowledge in CSS is at an intermediate level or above.
  3. CSS Processor Usage - with 65.33%, Sass is still the dominant tool.
  4. CSS Processor Experience - 73.34% of respondents indicated that they feel comfortable using Sass.
  5. CSS Frameworks - with 34,69% of respondents, Bootstrap is the most popular CSS framework. It's interesting, that 33% of respondents don't use frameworks at all.
  6. CSS Naming Schemes - 49.54% of developers now using one of CSS naming schemes when writing CSS.
  7. CSS Tool Experience - 49.46% of respondents comfortable using Autoprefixer.
  8. CSS Methodologies and Naming Scheme Experience - BEM is still the best-known CSS methodology with 38.85% of respondents saying that they feel comfortable using it.
  9. CSS Feature Usage - 68.04% of developers say they feel comfortable using Flexbox.
  10. JavaScript Knowledge - 86,85% of respondents consider themselves to be at an intermediate level or higher in JavaScript.
  11. Task Runners - NPM scripts now becoming the most used task runner with 47.79%.
  12. Knowledge of JavaScript Libraries and Frameworks -  jQuery (79.91%), React (41.02%), Vue.js (17.19%) and Angular 2+ (12.63%) dominate and are known with the most of developers.
  13. jQuery usage is still high amongst developers – with 50.52% of respondents using it on their projects. But React is now a very close second, with 47.77%.
  14. JavaScript Module Bundlers Usage - 66.04% of respondents use Webpack.
  15. JavaScript Extension Languages - with 21.66%, TypeScript is the tool that the most respondents felt comfortable using.
  16. JavaScript Linting - 61.11% of respondents use ESLint.
  17. JavaScript Testing - 43% of respondents don't use any testing tool. 22,29% of developers use Jest.
  18. JavaScript Package Managers - NPM is clearly still the leading package manager, with 62.90% of respondents using it.

Also, check how the usage of frontend tools is changing over time by looking at the figures from previous surveys.

Additionally, you can find the results of another survey devoted to Ruby on Rails here.

Read more

Display a file from another branch

2018-05-16 09:07:41

Ever wanted to see a file from a different branch? Well, no need to stash && checkout, just use git show.

dev $ git show master:path/to/file

PS: this works with any reference (not sure if that's the right term, correct me if I'm wrong), like commit and tags:

master $ git log --oneline
d529def Some more new stuff
4598db3 Some new stuff
e8dd680 init
master $ git show e8dd680:path/to/file
<The content of the file at commit e8dd680>

Enjoy!

Read more

Dynamically Sizing Text for a Tag Cloud Effect

2018-05-16 08:28:10

It's common to see data presented in a tag cloud format—which is to say, a chunk of keywords or terms displayed inline, with each term sized to show its relative importance or frequency out of all the existing terms.

tag cloud of New York Stock Exchange symbols

A lot of times we see these tag clouds built into content management systems or generated via plugins, but how do we make something like this if we want to do it from scratch? In this tutorial I'll walk through how I built this feature for my own website, to display the relative amounts of time I've worked with different technologies.

Setting up

I tried to keep the markup pretty simple:

<ul>
  <li data-year="2001">HTML</li>
  <li data-year="2002">CSS</li>
  <li data-year="2003">PHP</li>
  <li data-year="2010">Javascript</li>
  <li data-year="2012">Ruby</li>
  <li data-year="2010">Python</li>
  <li data-year="2017">Node.js</li>
  <li data-year="2010">MySQL</li>
</ul>

It's a simple unordered list. The only thing here that's special is that each <li> has a data-attribute, data-year, which denotes the year I started working with that particular technology. If you were creating a tag cloud for the number of articles with a given tag on your blog, you might instead call the data-attribute data-count, and set it to the number of articles with that tag.

Moving on to the styles, again keeping it pretty simple. Mostly I stripped out the default formatting, aligned the content to the center, and set list-items to display: inline-block so they coalesce into one block of text.

ul {
  list-style: none;
  margin: 0 auto;
  padding: 0;
  text-align: center;
}

ul li {
  margin: 0 5px;
  display: inline-block;
  vertical-align: middle;
}

The Scripting

First, let's wrap everything in an event handler for the window's load event, just to make sure everything's in place before we start applying our font-sizing. I'm using a bit of jQuery here to speed the process along, but there's no reason you couldn't do it in vanilla Javascript.

$(window).on('load', function() {
  //...
});

If your font sizes change at certain screen size breakpoints, you may also want to re-trigger this on resize as well as load.

Now that we've got that done, we need to get the range of numbers represented in our tag cloud.

  var nums = $('ul li').map(function() {
    return parseInt($(this).data('year'));
  }).get();

  var maxVal = Math.max(...nums);
  var minVal = Math.min(...nums);

The map function iterates over every list item and returns the value of the data-year attribute, parsed as an integer; the .get() method formats the output into an array.

We then pass the numbers into Javascript's Math.max and Math.min functions to get the largest and smallest values, respectively. (nums preceded by the ... to denote that it should be read as an array.)

Now, we'll calculate the sizing for the fonts.

  var currentDate = new Date();
  var currentYear = currentDate.getFullYear();
  var baseFont = $('ul li').css("font-size");
  var fontsplit = baseFont.match(/([0-9\.]+)\s?([a-z\%]+)/);
  var minFont = parseInt(fontsplit[1]);
  var maxFont = parseInt(fontsplit[1])*3;
  var fontUnits = fontsplit[2];

In this case, we're getting the value of the current year since we want to have the program do the math on how long I've worked with a technology. If you're getting something like a number of blog posts or inventory count instead, you can skip that step!

Then we get the base font size from the CSS. It comes with the units, so I've inserted a regular expression to match a number and a unit abbreviation. The current font size becomes the minimum font size for the tag cloud, assigned to minFont; the maximum font size is the minimum times three. You can adjust these to taste—for example, if you want your minimum to be 80% of the base font size, you'd set minFont = parseInt(fontsplit[1])*.8.

Now it's time to actually apply the font sizes!

  $('ul li').each(function() {
    var itemYear = parseInt($(this).data('year'));
    if (itemYear) {
      var num = currentYear - itemYear;
      var fontSize = (((maxVal - itemYear )/(maxVal - minVal)) * (maxFont - minFont)) + minFont;
      $(this).css("font-size", fontSize+fontUnits);
      $(this).attr("title", (currentYear - itemYear)+" years");
    } else {
      // if we can't determine the year, just set it to 90% size
      $(this).css("font-size", (minFont*.9)+fontUnits);
    }
  });
});

First it performs a quick check to make sure that data-year is actually set before trying to access it. Then we do a little math, subtracting the value of data-year from the current year to determine the years experience (again, this step is skippable if you're not trying to determine years since).

We determine the font size by figuring out what percentage the current number is between the minimum and maximum value, multiplying that by the difference between maxFont and minFont, and then adding that to minFont. We then set the css font-size value to that number and tack the units back on at the end.

Also, just to make sure that the information is represented in a way that's accessible to non-visual media, we set the list item's title attribute to "[N] years."

Final Product

All of this adds up to a result that looks a bit like this!

You can also see a live data version on my personal website if you scroll down to the bottom.

Read more

Midpoint Method: A Numerical and Computational Approach to Integrals

2018-05-16 06:10:20

Introduction:

Numerical methods are an important tool used to solve certain problems in mathematics. These problems tend to be very taxing and are better suited for the processing power of a computer. Integrals are one of the many mathematical problems that can be solved using numerical methods. There are many algorithms to solve integrals numerically, one of these methods is the midpoint method. This method looks to draw several rectangles under a curve and add up their areas to find an estimate of the area under the curve. The purpose of a numerical method is to find a solution to a problem using approximations.

Midpoint Method:

I chose the midpoint method for numerical integration and implemented it in F#, if you’re interested in trying out my code you can find it here. Given a function the midpoint method will create N rectangles to approximate the area under the curve of the function. More rectangles mean a much more accurate approximation.

For this example I'm using the function sin(x).

let f x : float =
sin x

The midpoint formula requires a starting point and an ending point. Given these values and the number of rectangles we can calculate the change in x.

let h = (b - a) / (float N)

Once we calculate the change in x we proceed into the loop and calculate the area of each rectangle and once we add them up together we obtain the area under the curve.

for i = 0 to N-1 do
let x = (a+h/2.0)
let funcEval = f(x + float i * h)
midpoint <- midpoint + funcEval * h

For this case we’re calculating the integral of sin(x) going from 0 to pi (3.14159) and we’re dividing it into 100 rectangles, the result for this integral is 2.000082, which is a good approximation considering the real result is 2.

Application:

Numerical integrals have many applications in the real world, one of the many applications can be found in the implementation of safety measures in the automobile industry. When a car stops the curve for acceleration over time can be abrupt if the car breaks suddenly or it can be gentle if the car slows down at a slower rate. Regardless of the time it takes to decelerate the area under the curve will always be the same since both cases have the same velocity. Numerical integrals can be used to approximate the area under said curve and find out how much deceleration is experienced during a period. Breaking at a lower rate might take longer but the passengers are not subjected to dangerous peaks, while stopping suddenly is faster but it comes at a higher risk. Doing this without a computer would be harder and would be error prone. Using numerical methods to solve this problem can yield very accurate approximations in a small period of time. This can help car manufacturers perform better and faster tests which can in turn result in new and improved safety measures which will save lives.

Bibliography:

https://www.encyclopedia.com/computing/dictionaries-thesauruses-pictures-and-press-releases/numerical-methods

https://www.whitman.edu/mathematics/calculus_online/section09.08.html
https://www.intmath.com/applications-integration/hic-head-injury-criterion.php

Read more

Taking Notes

2018-05-16 06:04:22

Originally published at www.gunnargissel.com

This is longer than the usual #discuss post, but I'd love to hear about how dev.to takes their notes. What systems do you all use? Who does it all with a computer, and how do they handle meeting notes? Who has some paper-based system I've never heard of? I'm pretty curious what y'all up to.

Notes!

I wrote earlier about why you might take notes. Here's how I take notes:

I've tried a lot of systems. I've tried Evernote. I've tried a wiki (several wikis, really). I've tried text files on the filesystem. I've tried Google Drive. None of these systems worked for me. Their organizational schemes, and the fact that I need a working computer to use them puts too many barriers between me, and the notes I want to write.

The note taking system that works best of me is Bullet Journaling, which is a structured way of categorizing and organizing notes. The practices that I find most useful are adopting a set of shorthand symbols for each note, keeping a weekly log, keeping a monthly calendar and keeping an index of my notes.

How I Take Notes

I take notes at all the meetings and trainings I attend. Sometimes my notes are just "I attended this meeting". Other times, I come out with a list of todos, and still other times I just come out with a bunch of things I want to remember.

Here's an example of notes I took during a project management training:

An example of what notes look like in a bullet journal

I kept track of what the training was about and observations on The Flight of the Phoenix, a training aid our instructor chose. I didn't have any todos or other particularly notable notes on this page, but it gives a flavor of how I use bullet journaling in the day to day.

My Preferred Set of Symbols

I adopted a set of symbols based on the bullet journal suggestions to indicate different types of notes. I went with a subset of the bullet journal symbols, because I found my todos are generally not very complicated. They either need to happen, happened or aren't going to happen. I didn't find moving them around from day to day very useful.

-

I use a dash to prefix general notes and observations

I use an open circle to indicate a todo. I put an "X" through the circle when it's done or cross it out when it becomes irrelevant.

👀

I use an eyeball for things I want to look into more

!

I use an exclamation point for things that I think are important

Keeping a Weekly Log

I work in an office environment, with semi-annual performance reviews. I find writing up what I did over the last six months a lot easier if I've kept track of it at some kind of high level overview. To that end, I use weekly logs

A detailed log of my weekly activities

I write out the log at the start of the week, then as I do things, I put a short note in on each day. "Attended stakeholder meeting," "Wrote workflow container," "Created permit entry UI," "Mentored junior developer," etc.

Keeping a Monthly Log

The weekly logs are great for looking back, but sometimes I want to look forward. I keep a couple months calendar in my journal to help plan when my busy periods are likely to be.

Here's what a fairly empty calendar on a new month looks like:

An empty calendar for a new month

As I learn new things, I put important events in my calendar, whether they are target release dates, testing cycles or demos. I also use these calendars to plan around personal events, like vacations or doctor appointments.

Keeping an Index

Keeping an index sounds boring and difficult, but I find that if I'm diligent, it's not particularly difficult, and it turns out to be one of the most useful parts of my journal.

An index of topics my work bullet journal covers

The three practices that make keeping an index easy are

  1. Number pages as you turn to them
  2. Title new entries before you write them
  3. Update the index as soon as you create a new entry

The payoff is six months after you've written a bunch and can't quite remember where that important technical decision originates from. Just look at the project meetings, or other promising entries and cut your search time down!

Ride off into the Sunset

Whatever method you choose to take notes in, having a system makes it easier to use the notes after writing them. I find the Bullet Journal technique very useful, but it took me a couple tries to settle in on how I wanted to record information. I'm still learning and changing how I take notes - never hurts to change!

If you like what you read here, consider visiting my blog for more!

Credits

Thank you Matt Reinbold for the notepad

Read more

5 TIPS TO GROW YOUR SOFTWARE ENGINEERING CAREER

2018-05-16 04:04:02

Hi everyone! My name is Amy and I'm new to dev.to. During the weekdays, I'm a systems software engineer that works with tools like Kubernetes, Docker, and Go! I love all things distributed systems. On the weekends, I make youtube videos about software engineering. This past weekend, I made a video that gives tips on career growth.

Here are 5 tips to grow your software engineering career!

  1. Learn how to communicate technical ideas
  2. Constantly work to expand your scope of involvement and understanding
  3. Work on "soft skills"
  4. Don't take everything as direction
  5. Keep track of your progress

If you're interested in learning more about each point, check out my video.
👉🏼https://www.youtube.com/watch?v=_lJarNEjT94👈🏼

Definitely make sure to follow me on dev.to for more awesome content
https://dev.to/amycodes

Read more

Build a Website Snapshot Service in 5 Minutes With StdLib and Node.js

2018-05-16 03:57:36

In this tutorial., I will show you how to schedule snapshots of a website using StdLib! If you want to trigger a snapshot of a website every month, week, day or even every minute-StdLib’s got you! The best part of this is that you won’t have to fill up your inbox or storage space with screenshots, they will automatically store in Wayback Machine at https://web.archive.org/.

alt text

Track the effect of HTML, CSS, Javascript changes on your website. Take screenshots of other websites that inspire you to build your site. Monitor your competitor’s websites and social media accounts. Keep your local politicians and president accountable for what they posted on their social media! If you are a developer, web designer, analyst, politician, community organizer, journalist, activist, or business owner you may find this article handy!

If you don’t know, the Internet Archive is a non-profit that has been working hard to build the digital library of the Internet since 1996! Through StdLib’s snapshot service, the community will have more significant input on what to preserve for the future on Web Archive!

So, let’s get to it!

What You’ll Need Beforehand

-1x Command Line Terminal With Node.js Installed

-5x Minutes (or 300x Seconds)

Minute 1: StdLib Account Setup

You’ll need a StdLib account to deploy your daily snapshot service. Getting started with StdLib is easy — head over to our website, choose a username and sign up for free!

alt text

Once you have created an account, all of your services will be stored and published under your username. For example, your snapshot service handler will be called: lib.<username>.DailySnapShot (or whatever creative name you decide to give your service).

Minute 2: Install the StdLib Command Line Tools

Before you begin deploying services to StdLib, you’ll need to install our open source command line tools. If you don’t have at least Node.js version 8.x installed, you can download the latest version, along with npm, here.

Once complete, install the StdLib CLI by opening up a terminal and running:

$ npm install lib.cli -g

This gives you access to the lib command for service management and execution. Next, create a stdlib directory for your StdLib services.

$ mkdir stdlib
$ cd stdlib
$ lib init

You’ll be asked to log in using the credentials you created your account with. That’s it, you are ready to build and deploy!

Minute 3: Creating a StdLib Service

You’ll now want to create a StdLib service for your snapshot. I’ve provided a @JanethL/DailySnapShot source (template) so that you can get your service up and running with very little effort. In the stdlib directory you just created, type:

$ lib create -s @JanethL/DailySnapShot

Next, You’ll be asked to enter a Service Name, I named my service TrumpsTwitterArchive because I have chosen to monitor the tweets that Trump deletes- you should select a name relevant to the website you are monitoring. Once your service has been created, enter the service directory by running:

$ cd <username>/servicename

Fire up your favorite editor text editor with $ code . or something similar to open the directory.

The process should look like this on your terminal:

alt text

Once your editor is open, you’ll want to enter the /__main__.js file in your service’s directory under /functions and change the url on line 7 to reflect your desired website’s url.

alt

The final step, enter your /package.json file and give your service a short description. My service description reads: “Takes a snapshot of Trump’s Twitter and stores it on https://webarchives.org." This step isn’t required to create your service, but I recommend it because it will help you stay organized as well as help others to understand what your service does.

alt text

Make sure to save the change, return to your terminal and deploy your function by running:

$ lib up dev

In order to run a scheduled task, you need to push an immutable release version with $ lib release:

$ lib release

Awesome! Your service is now available at:

<username>.stdlib.com/<service name>

Minute 4: Setting a Task on StdLib to Trigger Your Snapshot Service

We are almost done! Head over to your StdLib dashboard at https://dashboard.stdlib.com/dashboard/#/ Scroll down on the left sidebar menu and click “Scheduled Tasks.” Here you will be able to search up your released service.

alt text

Once you find and select the service you want to run as a task, choose which function within the service you want to execute. You can give your task a name and select how often you want your service to trigger a snapshot, anywhere from once a minute to once a week. After filling out the function parameters, you can easily run a test execution to ensure that your task does what you want it to do. A screenshot will be stored immediately in Wayback Machine. To check if it was stored, simply copy and paste the resulting URL into your browser, you should see a snapshot of your selected website.

alt text

And that’s it! You should see your scheduled task listed under “My Tasks”!

Minute 5: Explore the Wayback Machine

Whenever you need to get ahold of your snapshot all you have to do is go into https://archive.org/ and search up the website’s URL. You will be able to locate the precise date, and time your service took a snapshot.

alt text

Thanks for reading! I hope this article has been helpful showing you how easy it is to schedule a task using Standard Library!

I would love for you to comment here or e-mail me at Janeth [at] stdlib [dot] com, or follow StdLib on Twitter, @StdLibHQ or @mss_ledezma! Please let me know if you’ve built anything exciting that you would like StdLib team to feature or share!

Janeth Ledezma is the Community Manager for StdLib and recent graduate from UC Berkeley — go bears! When she isn’t learning the Arabic language, or working out, you can find her riding Muir Wood’s loop or exploring Marin County with a group of riders.

Read more

What are the unwritten rules of development?

2018-05-16 03:46:02

What underlying rules govern our work?

Which are good and which need to be dropped?

Read more

Handling Sounds With HTML5 Canvas

2018-05-16 02:57:52

There are two takeaways here: initialize assets and reuse them, and if you want to play event-triggered sounds that could overlap (laser firing, for example), you'll want multiple copies of the asset available.

I learned pretty early to initialize static assets once and then reuse them. The original implementation was adding a new asset for each game object. This meant every laser fired would add another sound to the DOM. As you can imagine, this allows you to figure out the upper limit on the quantity of elements in the DOM pretty quickly.

The solution was to not tie the sounds directly to objects, but rather to create a pool of sounds, then cycle through the pool when a sound was triggered.

Here is the implementation:

https://github.com/modle/centipede/blob/260fa54dadce91f25148638cf1eb10d46ff13dec/app/scripts/sound.js

init() will get called by the loader script, and will loop on the keys of the tracks object, creating the sounds and adding them to the DOM.

To play the sound, we just call playSound() with the type matching the key we want. playSound() will call getSound(), which will use mod to set the target index to the next index, or 0 if previous index was the end of the array. Then the sound element at that index will get returned and played.

Read more