Tuesday, October 18, 2016

AtoZ CSS Quick Tip: Solving Common CSS Problems

Q is for Questions about CSS

There is literally nothing else to say on the topic of CSS quotes, nor is there any other selector, property or value that starts with Q. So, in this week’s tips, I answer some common questions I get from my students, AtoZ supporters and fellow professionals.

How do you center things vertically in CSS?

I get this one a lot. And I get this one in addition to the more general question of how to horizontally center things in CSS which I addressed in an earlier tip all about floats.

On the topic of vertical centering, some people will tell you it’s really difficult. But these days it doesn’t require much CSS and can be done on any element - even in a responsive project when you don’t know how wide or tall the container or the element to be centered is.

Take the following HTML which marks up a simple .modal message box inside of a container:

[code language="html"]
<div class="modal">
<h2>Message title</h2>
<p>Message text lorem ipsum dolor sit amet</p>
</div>
[/code]

To absolutely center the .modal element we can combine absolute positioning with translate.

Step one is to put the top left corner of the box right in the center of the parent container - which we can do by offsetting its top and left edges by 50% of the height and 50% of the width of the parent container:

[code language="css"]
.container {position: relative; width: 100vw; height: 100vh;}
.modal {position: absolute; top: 50%; left: 50%;}
[/code]

Step two is to bring the element back into the absolute center of the container. We need to move it back by half of its width and up by half of its height.

If we know the dimensions of the .modal we can achieve this with some negative margin and divide the width or height by 2 to get the required value. But if the .modal is likely to change size (due to being sized with percentages for example) this won’t work.

Instead, using a translate transformation, we can move the element by a percentage of its existing size.

[code language="css"]
.modal {position: absolute; top: 50%; left: 50%;}
/* back by half its width, up by half its height */
transform: translate(-50%, -50%);
[/code]

And there you have it, the element is perfectly centered even with a variable size.

Continue reading %AtoZ CSS Quick Tip: Solving Common CSS Problems%


by Guy Routledge via SitePoint

Let's Go: Testing Golang Programs

In this tutorial I will teach you all the basics of idiomatic testing in Go using the best practices developed by the language designers and the community. The main weapon will be the standard testing package. The target will be a sample program that solves a simple problem from Project Euler.

Square Sum Difference

The sum square difference problem is pretty simple: "Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum." 

This particular problem can be solved rather concisely especially if you know your Gauss. For example, the sum of the first N natural numbers is (1 + N) * N / 2, and the sum of squares of the first N integers is: (1 + N) * (N * 2 + 1) * N / 6. So the whole problem can be solved by the following formula and assigning 100 to N:

(1 + N) * (N * 2 + 1) * N / 6 - ((1 + N) * N / 2) * ((1 + N) * N / 2)

Well, that's very specific, and there isn't much to test. Instead, I created some functions that are a little more general than what's needed for this problem, but can serve for other programs in the future (project Euler has 559 problems right now).

The code is available on GitHub.

Here are the signatures of the four functions:

Now, with our target program in place (please forgive me, TDD zealots), let's see how to write tests for this program.

The Testing Package

The testing package goes hand in hand with the go test command. Your package tests should go in files with the "_test.go" suffix. You can split your tests across several files that follow this convention. For example: "whatever1_test.go" and "whatever2_test.go". You should put your test functions in these test files.

Every test function is a publicly exported function whose name starts with "Test", accepts a pointer to a testing.T object, and returns nothing. It looks like:

The T object provides various methods you can use to indicate failure or record errors.

Remember: only test functions defined in test files will be executed by the go test command.

Writing Tests

Every test follows the same flow: set up the test environment (optional), feed the code under test input, capture the result, and compare it to the expected output. Note that inputs and results don't have to be arguments to a function. 

If the code under test is fetching data from a database then the input will be making sure the database contains appropriate test data (which may involve mocking at various levels). But, for our application, the common scenario of passing input arguments to a function and comparing the result to the function output is sufficient.

Let's start with the SumList() function. This function takes a slice of integers and returns their sum. Here is a test function that verifies SumList() behaves as it should.

It tests two test cases, and if an expected output doesn't match the result, it calls the Error() method of the testing.T object. 

This is all straightforward, but it looks a little verbose. Idiomatic Go testing uses table-driven tests where you define a struct for pairs of inputs and expected outputs and then have a list of these pairs that you feed in a loop to the same logic. Here is how it is done for testing the SumList() function.

This is much better. It is easy to add more test cases. It's easy to have the full spectrum of test cases in one place, and if you decide to change the test logic you don't need to change multiple instances.

Here is another example for testing the SquareList() function. In this case, both the input and the output are slices of integers, so the test pair struct is different, but the flow is identical. One interesting thing here is that Go doesn't provide a built-in way to compare slices, so I use reflect.DeepEqual() to compare the output slice to the expected slice.

Running Tests

Running tests is as simple as typing go test in your package directory. Go will find all the files with the "_test.go" suffix and all the functions with the "Test" prefix and run them as tests. Here is what it looks like when everything is OK:

Not very dramatic. Let me break a test on purpose. I'll change the test case for SumList() such that the expected output for summing 1 and 2 will be 7.

Now, when you type go test, you get:

That states pretty well what happened and should give you all the information you need to fix the problem. In this case, the problem is that the test itself is wrong and the expected value should be 3. That's an important lesson. Don't automatically assume that if a test fails the code under test is broken. Consider the entire system, which includes the code under test, the test itself, and the test environment.

Test Coverage

To ensure your code works, it's not enough to have passing tests. Another important aspect is test coverage. Do your tests cover every statement in the code? Sometimes even that is not enough. For example, if you have a loop in your code that runs until a condition is met, you may test it successfully with a condition that works, but fail to notice that in some cases the condition may always be false, resulting in an infinite loop. 

Unit Tests

Unit tests are like brushing your teeth and flossing. You shouldn't neglect them. They are the first barrier against problems and will let you have confidence in refactoring. They are also a boon when trying to reproduce issues and being able to write a failing test that demonstrates the issue that passes after you fix the issue.

Integration Tests

Integration tests are necessary as well. Think of them as visiting the dentist. You may be OK without them for a while, but if you neglect them for too long it won't be pretty. 

Most non-trivial programs are made of multiple inter-related modules or components. Problems can often occur when wiring those components together. Integration tests give you confidence that your entire system is operating as intended. There are many other types of tests like acceptance tests, performance tests, stress/load tests and full-fledged whole system tests, but unit tests and integration tests are two of the foundational ways to test software.

Conclusion

Go has built-in support for testing, a well-defined way to write tests, and recommended guidelines in the form of table-driven tests. 

The need to write special structs for every combination of inputs and outputs is a little annoying, but that's the price you pay for Go's simple by design approach.


by Gigi Sayfan via Envato Tuts+ Code

How to Analyze Your YouTube Marketing With Google Analytics

kh-analyze-youtube-google-analytics-600

Do you use YouTube for business? Want to connect Google Analytics to your YouTube channel? Google Analytics can show you how people find and engage with your channel, and how YouTube drives traffic to your website. In this article, you’ll discover how to set up Google Analytics to track and measure the impact of your [...]

This post How to Analyze Your YouTube Marketing With Google Analytics first appeared on .
- Your Guide to the Social Media Jungle


by Kristi Hines via

5 Twitter Browser Extensions for Marketers

ct-twitter-browser-extensions-600

Do you use Twitter for business? Looking for tools to optimize your Twitter experience? Using browser extensions to enhance your Twitter marketing will save you time and streamline your workflow. In this article, you’ll discover five browser extensions to improve your Twitter marketing experience. #1: Analyze Twitter Profiles With Riffle Riffle by CrowdRiff is the [...]

This post 5 Twitter Browser Extensions for Marketers first appeared on .
- Your Guide to the Social Media Jungle


by Chris Tweten via

Clockface – Timepicker for Bootstrap

Clockface is a clock-like timepicker for Bootstrap. It can be used as Input, Component and Inline style.


by via jQuery-Plugins.net RSS Feed

Nightshift

New website for Nightshift, a leader in audiovisual and digital content
by via Awwwards - Sites of the day

Monday, October 17, 2016

Everything You Need to Know About the Golden Ratio

Everything You Need to Know About the Golden Ratio - Infographic

What do the ancient Greeks, Salvador Dali, and modern graphic designers have in common? They all use the golden ratio. And if you want to be on par with these artistic greats, you'll need to learn how to put this ratio to work for you.

The golden ratio—which is 1:1.618—occurs on its own in the natural world. It appears everywhere from nautilus shells to the human face. But people didn't start harnessing its power until a mathematician named Fibonacci created the sequence of numbers that describes the ratio. (If you're not a math nerd, all you need to know is that Fibonacci came up with the pattern that led the Greeks to discover the ratio itself.)

People began using the golden ratio for art—the Mona Lisa, for instance, uses it—and for other great achievements. They even discovered that they had unintentionally used it in architecture, like The Parthenon and the Great Pyramid of Giza.

Today, the golden ratio appears in many of the items you see, from web design to magazine layouts to photography. Even the Apple logo uses it. To help you put the golden ratio into practice, Company Folders has rounded up these great examples of how people have used it in the past—and how you can use it in the future.

by Irfan Ahmad via Digital Information World