Wednesday, October 26, 2016

Embracing Sass: Why You Should Stop Using Vanilla CSS

When I first heard about Sass and other CSS preprocessors, my exact thoughts weren’t quite enthusiastic. “Why do we need another tool for something that already works as beautifully as CSS?”. “I won’t use that”. “My CSS is ordered enough to not need it”. “A preprocessor is for people who don’t know how to write CSS, if you know how to write CSS you won’t need a preprocessor”. “After all, processors are for people who do not know how to write CSS. If they did, they wouldn’t need need a preprocessor”. And I actually avoided them for a while, until I was forced to use it in several projects.
Embrace Sass once, and you may never want to go back to vanilla CSS againEmbrace Sass once, and you may never want to go back to vanilla CSS again
I didn’t realise how much I was enjoying working with Sass until recently, when I had to switch back to vanilla CSS in a project. During that time, I learned so much that I decided to praise Sass and make this a better world, and make you a happier person!

Why Use Sass Anyway?

Organization: @import

This project that I just mentioned, a large e-commerce website, had a single CSS file with 7184 lines of uncompressed style declarations. Yes, you read it right: seven thousand one hundred and eighty four lines of CSS. I am sure this is not the biggest CSS file front-end developers had to handle in the industry, but it was big enough to be a complete mess.
This is the first reason why you need Sass: it helps you organize and modularize your stylesheets. It’s not variables, it’s not nesting. For me the key feature of Sass are partials and how it extends the CSS @import rule to allow it to import SCSS and Sass files. In practice, this means that you will be able to split your huge style.css file into several smaller files that will be easier to maintain, understand, and organize.
Sass helps you organize and modularize your stylesheetsSass helps you organize and modularize your stylesheets

The @import rule has been around for almost as long as CSS itself. However, it gained bad fame since when you use @import in your CSS files, you trigger separate HTTP requests, one for each CSS file that you are importing. This can be detrimental to the performance of your site. So what happens what you use it with Sass? If you never stopped to think about what the word “preprocessor” means, now is the time.
“A preprocessor is a program that processes its input data to produce output that is used as input to another program.” —Wikipedia
So, going back to our @import rule, this means that the @import will be handled by Sass and all our CSS and SCSS files will be compiled to a single file that will end up in our live site. The user will have to make only one request and will download a single file, while your project structure can be comprised of hundreds of modularized files. This is what the style.scss of a typical Sass project may look like:
@import “variables”;
@import “reset”;
@import “fonts”;
@import “base”;
@import “buttons”;
@import “layout”;

Don’t Repeat Yourself: Variables

Any article praising Sass will probably start by mentioning its star feature - variables. The most common use of variables is a color palette. How many times did you find several declarations of what is supposed to be the same color, ending up in the CSS as slightly different shades because nobody uses the same hex code? Sass to the rescue. In Sass, you can declare variables with almost any value. So, our color palette can be something like:
$brand: #226666;
$secondary: #403075;
$emphasis: #AA8439;
The words starting with “$” are Sass variables. What it means is that later in your stylesheets, you will be able to use those words, and they will be mapped to the values that you defined before:
body {
  background: $secondary;
}

.logo {
  color: $brand;
}

a {
  color: $emphasis;
}

a:hover {
  color: $brand;
}
Imagine how this could change our 7184 lines of CSS code, and you may start desiring Sass right now. Even better, imagine there is a redesign of the brand and you need to update all the colors in your CSS. With Sass, the only thing you need to do is to update the declarations of those variables once, and baam! The changes will be all around your stylesheets.
I coded this example in Sassmeister, a Sass playground. So go ahead and try changing those variables to something else.
The usefulness of variables are not just limited to colors, but font declarations, sizes, media queries, and more. This is a really basic example to give you an idea, but believe me, the possibilities with Sass are endless.
The possibilities with Sass are endless

Cleaner Source Code: Nesting

Nesting could be possibly the second most mentioned feature of Sass. When I went back to vanilla CSS after using Sass, the CSS file I was looking at seemed so cluttered that I wasn’t sure if it was minified. Without nesting, vanilla CSS doesn’t look any better than pretty printed .min.css files:
.header {
  margin: 0;
  padding: 1em;
  border-bottom: 1px solid #CCC;
}

.header.is-fixed {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
}

.header .nav {
  list-style: none;
}

.header .nav li {
  display: inline-block;
}

.header .nav li a {
  display: block;
  padding: 0.5em;
  color: #AA8439;
}
With Nesting, you can add classes between the braces of a declaration. Sass will compile and handle the selectors quite intuitively. You can even use the “&” character to get a reference of the parent selector. Going back to our example CSS, we can transform it to:
.header {
  margin: 0;
  padding: 1em;
  border-bottom: 1px solid #CCC;

  &.is-fixed {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
  }

  .nav {
    list-style: none;

    li {
      display: inline-block;

      a {
        display: block;
        padding: 0.5em;
        color: #AA8439;
      }
      
    }

  }

}
It looks beautiful and is easier to read. Feel free to play with this example.
Again! Don’t Repeat Yourself: Mixins and Extends
Repetition in CSS is always hard to avoid. And it doesn’t hurt to stress on this a bit more, especially when Sass gives you mixins and extends. They are both powerful features and help avoid a lot of repetition. Possibilities with mixins and extends don’t seem to have an end. With mixins, you can make parameterized CSS declarations and reuse them throughout your stylesheets.
Keep things DRY with SassKeep things DRY with Sass
For example, let’s say you have a box module with a button inside. You want the border of the box and the background of the button to be of the same color. With vanilla CSS, you do something like:
.box {
  border: 2px solid red;
}

.box .button {
  background: red;
}
Let’s say you now want the same box module, but with a different color. You will add something like this to your CSS:
.box-alt {
  border: 2px solid blue;
}

.box-alt .button {
  background: blue;
}
Now, let’s say you want a box module, but with thinner border. You would add:
.box-slim {
  border: 1px solid red;
}

.box-slim .button {
  background: red;
}
A lot of repetition, right? With Sass you can abstract these cases to reduce repetition. You could define a mixin like this one:
@mixin box($borderSize, $color) {

  border: $borderSize solid $color;

  .button {
    background: $color;
  }

}
And so, your source code can be reduced to:
.box { @include box(2px, red); }
.box-alt { @include box(2px, blue); }
.box-slim { @include box(1px, red); }
Looks beautiful, right? Play around with this example. You can create your own library of mixins, or even better you can use one of the community libraries out there.
Extends are similar, they let you share properties from one selector to another. However, instead of outputting multiple declarations, they output a list of classes without repeating your properties. This way you can avoid repetition of code in your output as well. Let’s forget about the buttons in our previous example and see how @extend would work with .boxes.
Let’s say you declare a first box:
.box {
  margin: 1em;
  padding: 1em;  
  border: 2px solid red;
}
Now you want two boxes similar to this one, but with different border colors. You can do something like:
.box-blue {
  @extend .box;
  border-color: blue;
}

.box-red {
  @extend .box;
  border-color: red;
}
This is how the compiled CSS would look like:
.box, .box-blue, .box-red {
  margin: 1em;
  padding: 1em;
  border: 2px solid red;
}

.box-blue {
  border-color: blue;
}

.box-red {
  border-color: red;
}
Powerful, right? You can find the example here. If you review the resulting CSS, you will realize that the class .box is being included in the output. If you don’t want this behavior, you can combine @extend with “placeholders”. A placeholder is a special selector that won’t output a class in the CSS. For example, I sometimes find myself resetting the default styles of lists a lot. I generally use @extend with a placeholder like this:
%unstyled-list {
  list-style: none;
  margin: 0;
  padding: 0;
}
You can then reuse this pattern in all your stylesheets:
.search-results {
  @extend %unstyled-list;
}

.posts {
  @extend %unstyled-list;
}

.nav {
  @extend %unstyled-list;
}
Your compiled CSS will look like:
.search-results, .posts, .nav {
  list-style: none;
  margin: 0;
  padding: 0;
}
Check out the examples here.

Is There More?

Absolutely! I didn’t want to overcomplicate this article, but there is a Sassy world waiting to be discovered by you; and there are also a lot of features beyond those: operations, single-line comments with //, functions, if loops … if you ever thought “it would be great to do some ‘X’ thing with CSS”, I’m sure that thing ‘X’ is already done by Sass. “CSS with superpowers” is its tagline, and that can’t be any closer to the truth.

Conclusion

Go and visit the install page and start hacking! Believe me, you won’t regret it.
Yes, there are some alternatives to Sass. Other preprocessors (LESS, Stylus), postprocessors, Grunt, etc. There are even CSS Variables. I’m not saying that Sass is the only technology out there. All I’m saying is that it’s the best! At least for now. Don’t believe in what I’m saying? Go ahead and try it yourself. You won’t regret it!

Wednesday, October 19, 2016

From Node.js to Paying Your Freelance Taxes: Interview with a Successful Developer

We’re always looking for the best freelancers around. But what makes a freelancer truly great? Luis Martinho, a top developer, is beloved by his clients — so we sat down with him to talk about freelancing, technologies like freelance Node.js and HTML5, and paying your taxes.

So, to start, how did you get into freelancing? Have you ever worked a full-time job?

“I had worked a couple of full-time jobs: some of them were relatively corporate, but the most recent was in a startup environment, specifically in the enterprise SaaS space, building sexy management software in the cloud. We had a very talented team and a very ambitious vision. After four years of growth, we had an exciting product in an exciting space, which was great, but I wasn’t very happy. I needed a lifestyle change. When we started, I personally did not understand how hard it was to “start up”. It’s not just the hours, because you work long hours in all sorts of environments and projects; it’s the stress, the responsibility, and the pains associated with creating something new. It’s not all flowers and rainbows. In the end, I decided that I wasn’t co-founder material (at least, not at the time). But the experience gave me a much deeper understanding of the kind of pressure faced by startup founders, and I know that I’ve become a better freelance software developer because of that.
Freelancing looked more and more like the life I wanted: it presented an opportunity to find interesting clients and projects while being rewarded for quality work.
I started looking for regular jobs: first in my hometown, then in the rest of the country, then in the rest of Europe. I managed to find some interesting projects, some interesting compensation packages, and some interesting locations; but I believed that I could have it all. So I began to look into freelancing. And as I kept looking, freelancing looked more and more like the life I wanted: it presented an opportunity to find interesting clients and projects while being rewarded for quality work.”

What was the toughest challenge that came about during your transition to freelance development work?

“Originally, I imagined a huge change would be the income stability that you expect from a full-time job, where you have a certain amount of money that you always get by the end of the month—and you get it, whatever happens. I expected that to be a major concern since I knew that being a freelancer meant that I wouldn’t always have clients. But that’s not really what’s happened—kind of surprisingly, at Toptal I’ve managed to always have a steady flow of clients without having to hunt for them. So, to be honest, the main challenge has really been to organize my schedule. I really don’t have any set hours to work. I basically make up my own schedule, and so I have to be more disciplined than I was before. I try to work more-or-less standard hours: I wake up in the morning, start working, have lunch at the time that everyone else has lunch, work in the afternoon, and then have dinner. I try to work a normal schedule, not pulling all-nighters, burning the midnight oil every day. This discipline was a big challenge at the start.”

But most people see loose hours as a benefit of freelance software engineering. What’s your take on that?

I’ve found that it pays off to be disciplined because I end up being much more productive if I have a set of routines.
“Certainly, it is a benefit. It’s cool in the sense that if you run into any problems, if you have to assist someone in your family, or you have this great opportunity for some kind of leisure activity, you can go after it and you can enjoy that opportunity. But on the other hand, I’ve found that it pays off to be disciplined because I end up being much more productive if I have a set of routines, because those routines basically guide me: I can cut to the chase and get more done during that time. It’s good to have that freedom, but I think it has to be used responsibly.”
Freelance software developers enjoy the benefit of flexible schedules and a different pay structure.

Your clients love you. What’s your secret?

“As a freelance software developer, it’s probably a good trait to be slightly lazy in terms of looking for the easiest, cheapest way to get something done; to be extremely annoyed by doing repetitive work, and so to continuously improve on your tool chain. For instance, you spend a lot of time in a terminal: you should seriously consider creating shortcuts, aliases, whatever saves you time and saves you typing. In the end, a lot of the tasks you do are not really rocket science; they’re just going through a couple of loops to get something done. If you optimize that stuff and get the repetitive work out of the way, you have more time available to do the actual rocket science.

If you get the repetitive work out of the way, you have more time available to do the actual rocket science.
You also have to have someone else handle hourly pay and billing, getting money from customers, and even getting new customers so that you can focus on doing great work. And that’s absolutely important. When you’re a freelance software developer, you’re working on delivery, but also on marketing and sales. And what you want to do—and what your clients want you to do—is to spend as much time on delivery, and as little time on marketing, sales, and even finance. You’ll always have to spend time looking for new freelance development clients when you’re working for old ones, and that’s not something the old ones want—and it’s not something you want. You just want to focus on doing your work well. So having someone else handle billing and finding new clients is something that you should be very happy about, and you’re very lucky if you find it.
Also, don’t forget: pay your freelance taxes.”

How do you keep yourself motivated on a daily basis?

They feel that you’re connected to them in a way that you’re not just sending over billable hours.
“What plays a huge part in my motivation is the quality of the clients. If possible, I try to be kind of picky. That is, I try to accept gigs that motivate me in terms of the technological challenge involved whether it be solving a hard problem or working in an interesting space. And it kind of just follows from that. I always work with a client thinking that I’m working towards a common goal: I try to see the bigger picture and see myself as part of the team. You’re never really part of the internal or onsite software development team in the full emotional way, especially when you’re out of the office, but you can still feel like part of the team in terms of the overall project and the quality of the work. And I think people on the other side (the client and the client team) feel that—they feel that you’re connected to them in a way that you’re not just sending over billable development hours, you’re really worried about whether they will succeed or if the quality of the work is good enough.”

What do you look for in a potential freelance software or web development client?

This freelance web developer is considering his many client options.
“Generally, I favor more recent technologies than older ones, and also emerging stuff—I wouldn’t necessarily say emerging because some of this stuff is current, but I’d rather work with someone making single-page applications and using Backbone.js andNode.js with Express or some MVC framework than someone who would be using Java Enterprise Edition-style.
I’m freelance, I would much rather have a client that has some asynchronous communication solution in-place. If you’re telecommuting, it’s much easier to get inside a team that’s using something as simple as a mailing list or has chat rooms in Campfire or uses GitHub as a collaboration tool (this last one works remarkably well). Altogether, if the client is not capturing the water cooler talk some way, you risk losing a lot of information and chatter that can impact your work.”

That’s an interesting point. Any other tips for telecommuting?

It’s harder to remember the guy that’s never in the building.
“You have to find a way to compensate for being out of the office. The solution I’ve found is to be proactive. For instance: analyze commit logs. When you’re telecommuting, you should set aside some time just to glance through and have an idea about what other people are working on. If you’re being aware of what’s happening in daily standups, notified through some project management solution, it’s a good practice to try to find hints about what the team is working on. Then, you’re more easily integrated. That’s obviously an additional effort on your side—you might be able to get out of doing it if they have a really good communication process in place, but it can be a good tip and it can save some problems. It’s harder to remember the freelance development guy that’s never in the building.”
Becoming a freelance software developer starts with communication.

What’s the most important quality of a freelancer?

“You have to be someone who can maintain a fluid balance between your personal life and your work. As a freelancer, it’s important to be responsive. But being responsive ends up having a big toll on your personal life. You don’t have to be checking your email all the time—you can just get notified, filter whether it’s important or not, and then you can just go back to enjoying time with your family or whatnot. You have to be a good context switcher. Context switching is usually expensive, but you need to make it cheap and fast, so that you can have a very high standard of service by being very responsive to emergencies but, at the same time, enjoying the company of friends and family.
You have to be a good context switcher. Context switching is usually expensive, but you need to make it cheap and fast.
For the usual freelance development arrangement, you have to know how to manage your finances conservatively. This hasn’t really happened with Toptal, surprisingly, but usually it’s very easy to go from feast to famine when it comes to software or web developer pay, even hourly. One month, you make a ton of money and suddenly you don’t have clients and you don’t have any more work. So you have to be disciplined: you need an idea of the average wage, and you need to spend that amount; all the excess needs to be some kind of cushion to help you sail through the uncertainties of doing this kind of work. This really isn’t that true for Toptal because the stream of clients has been really steady, but it’s still a good practice.”
As a freelancer, you take on a bunch of different software development projects throughout your career. So what do you do first time you’re given access to a new codebase? Lets say it’s a billion lines of code.
“I would try to be disciplined about taking a top-down approach. This means getting a feel for the overall structure of the codebase. That seems kind of obvious, but as you start trying to understand the code, you’ll be driven to what you’re working on at that specific moment and the specific task you have at hand. That will shift your focus from the top-down approach and will put you at the bottom trying to go up, assembling the pieces from the specific parts of code you’re looking at. That might be better in the short term for you to fix that specific bug or add that specific functionality, but in the medium term, you’ll wish you’d kept with the top-down approach. Then, you’ll have a much better view of the system and how the pieces interact.”
Being a freelance developer means taking a top-down approach to new codebases to start effectively.

You’ve done a lot of front- and back-end work. Do you think it’s important for freelance developers today to have both of those skillsets?

“Nope. I’ll be very honest: I think you can gain a lot from specialization, like if you’re ridiculously good with a ridiculously small set of technologies; but it’s just a personal choice. I’m curious, so I work with a lot of different technologies. And that’s why I end up doing both front-end and back-end freelance work.
Technologies will change — what’s hot today will be boring probably tomorrow or at least in 6-12 months.
Having said that, I think it’s crucial for a developer, throughout his or her professional lifetime, to try and learn as many technologies and paradigms as possible. Technologies will change—what’s hot today will be boring probably tomorrow or at least in 6-12 months—and one of the key skillsets for a freelance software developer is to quickly pickup a new technology or a new project. For that, it really helps if you have a systematic approach to learning new stuff and you practice it by learning at least one new language or framework or paradigm every year.”

You’ve done a ton of freelance work with Rails, Django, and more recently, Node.js. How have your experiences differed between the three?

“Uh oh, we’re going to get into a religious war here. I would say Node is quite different than the other two. When you’re first exposed to Node.js examples, you might have trouble getting into the event-driven paradigm. It’s something that might not be 100% natural to someone who’s been doing Rails work all the time. On the other hand, if you’re working with JavaScript, you’re probably developing for the browser and so you’re already familiar with the event-driven paradigm. Personally, in my freelance experience with Node, I’ve been using an MVC framework, so I haven’t developed core technology for Node; I’ve just been a standard user of a client-specific proprietary framework (comparable to Express).”

What are your favorite tools that too few developers are using?

Node.js freelancers have to think differently than Rails freelancers.
“Off the top of my head, WebStorm from JetBrains. It’s a JavaScript IDE that’s really worth looking up. The new Android studio released at Google I/O is powered by JetBrains software, not Eclipse or whatnot. I like Eclipse, but I’m having a really fun time with WebStorm. I’m still learning a lot of shortcuts—and that’s a practice that a good freelance software developer should have as well: learning more about their tools. I’m still learning more about my IDE. WebStorm has a lot of smart shortcuts, a good standard layout, and it also has some nice ZenCoding properties that let you write HTML as quickly as ever. All in all, it ‘s a good tool.”

If a freelance client wanted you to do a project with Rust or Go, or some new language with which you’ve never worked, what would you say?

No matter how good you are, there will always be setup time, and that can be costly for the client.
“I usually try to get my first contact with languages outside of client work, not on the client’s time—no matter how good you are, there will always be setup time, and that can be costly for the client. That being said, if the client told me, “Ok, we’re comfortable even if you don’t know anything about it”, I would see that as a great opportunity to learn something new. On the other hand, if the client had a demanding schedule, I would advise them to hire someone else or use a language that I was comfortable with so I knew I could be productive from the get go.”

What’s the biggest technical challenge that you’ve had to solve while freelancing?

“A freelance HTML5 project comes to mind. Digging into the HTML5 Canvas API to do some advanced graphics editor functionality, like doing lasso selection and freeform selection, where you crop a part of the image but leave out the rest, calculations with sets of pixels, things like that. This was kind of like MS Paint using HTML5 Canvas and implementing things like a brush: you’d think a brush would be something simple—it is if you just draw a line, but in this case you want to imitate a brush stroke, so I had to use a mathematical formula to simulate how a brush would contact the canvas and leave a blur with ink.”
HTML5 freelancers might find this new technology takes some getting used to.

Your favorite new technology that you’ve worked with lately?

“What I’ve been hearing about Meteor is interesting in terms of how they offer a complete head-and-shoulders framework for developing a single-page application. It handles the back-end as well as the front-end. I haven’t worked with it but I’m curious and itching to try it out.”
Source: Toptal