Forum Moderators: not2easy

Message Too Old, No Replies

Ruby, Sass and Less

Pros and Cons of Pre-processing

         

whyyi

1:18 am on Jan 20, 2011 (gmt 0)

10+ Year Member



We're currently running a rails app and using haml with css. I've done a little bit of research and it doesn't seem like Sass is much of an upgrade. Are there really very many pros to using it aside from everyone in the ruby community saying it's great?

Any feedback from people that have experience with implementing sass into their rails app is greatly appreciated.

Thanks!

SuzyUK

8:29 pm on Jan 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm interested in hearing any replies too! :)

I don't have any working experience with Ruby/Haml/Sass, but had heard about it, so I had a good look around to find out the scoop. I'm not a fan of CSS frameworks ;)

but.. Sass is not a Framework!?

here goes my thoughts & rambles..from what I'm lead to believe Sass is not a framework though the way it's used means that frameworks, such as Blueprint, YUI etc. can be plugged in (i.e. the biggest source of re-usable snippets ;)). In other words like jQuery for JS, you can build your own plugins and share Library Snippets for common CSS code snippets, then plug them into your Sass e.g. fancy button code, cross browser rounded corners. Sass itself is the name for the syntax used, it seems to be part of the draw of using it as it's relatively simple, it's like CSS without the semi-colons and curly braces. Some say they don't like this and for someone used to writing their own CSS, in fact anyone with their editors primed just the way they like them, this would be like "learning a new language".. however they've now introduced Scss which is the same with the curly braces which people are more familiar with and I think it shows nesting more clearly anyway.. like any other language the brackets properly indented show the nests are closed.

Sass (or Scss) is just a way of writing CSS in a language/format that's understood by a compiler, the compiler used for Sass/Scss appears to be Compass which also runs on Ruby.

The Compass compiler interprets the Sass and spits out cross browser compliant (and minified if you want) CSS. This is much the same as LESS (Ruby or PHP) and csscaffold(PHP) - references linked at bottom

When I started reading I was definitely in the "I don't need this" camp. However when I saw that you can use Variables, Maths, Mixins (reusable code snippets) and functions, I thought that this could indeed have a place. Albeit in the Ruby/Sass/Compass environment and even then perhaps only in large workshops which run or manage many applications on the same platform.

I think single applications which might like to use variables/mixins/maths might be better ran through a php compiler prior to deployment in order to gain the same functionality on a smaller less complex scale.

In all the tutorials I saw (which wasn't many, and most of them were 2009, why?) all the examples for variables used color and they called the variables "light-grey" "light-blue" etc. which to me is no more than setting a class name of the same descriptive purpose. What happens when someone comes along and changes the color to pink? Sure you can just change the hex code in the one place next to the "light-blue" variable but then the actual Sass file has "$light-blue" throughout but the actual CSS is a pink hex?

Now of course that can be a company rule where you have to name the variables a bit more intuitively but how to do that so the next person following, or even yourself at a later date can change half the instances of said color.. i.e. only wants to change the color where it appears as a background, but not where it appears as a border?

I think it might take longer for someone to get to grips with the multiple/modular Sass sheets and print themselves out a "cheat sheet" in order to properly modify an entire CSS.. and to me that can be done quicker using Firebug (or for me & others like me NoteTab and manually ;)) and if you think about it the repetitive lines e.g. the x-browser border-radius rules can probably be spotted and amended quite easily in a CSS - whereas in Sass if you only wish to change half the instance to a different radius value, you have to make a function then go through the Sass and add in the values where the function is called or if you've already got one, you still have to find the lines which contain the elements you want to modify.

This is all possible, but I can definitely see that trade off time might only be worth it if you're training to staff to work to a set of Standards within your own workshop.

Maybe the "one application" deployment is not worth the time and effort unless it's part of learning curve or to put on CV?

Unlike jQuery where it's generally accepted that you don't need to know JS inside out to use it (Sorry JS guys, I know it's infinitesimally better if you do of course;)) - you would actually have to know CSS in order to "nest" properly and to maintain proper Specificity. Also inheritance might cause a problem, although if you nest as suggested it's probably best to redeclare all properties (which means no shortcut CSS) and thus to gain the functionality of variables/mixins/maths you lose the power of KISS CSS.

I have to say I'm still confused as to how exactly this would make debugging easier, which is touted in some of the forums/blogs I read, I can however see that especially SaSS which spits out minified code, without comments, if required would be useful for inputting those rules like border-radius which still 3 or 4 repetitions in order to be cross browser compliant. Any good CSS'er probably has their own library of things like that which they can copy/paste when required. and if a company/workshop just kept themselves a master file of snippets related to their application, then that would be the same thing.

I think these syntaxes and compilers have come from programmers, who in general are always looking for a way to automate things (bless them ;)) but CSS is not a programming language so it doesn't quite fit into the fully automated sphere..

I can imagine that the maintenance of a Sass/LESS/csscaffold library might become as much of a maintenance nightmare as a large CSS file, it's just that the maintenance will move to SASS whereby the variable or snippets (mixins) need to be maintained and if they're changed the relevant call to the variable in the SASS file might need updated.

Just to share a couple of things I found:
a tweet from Zeldman [twitter.com] (don't think it refers to Sass more to Blueprint etc. but I came across it via a Sass article & it made me smile)

More useful might be Chris Coyiers video intro to Sass/Compass [css-tricks.com], though even he says he's conflicted!

In general I think the theory is great! but the reality? I'm not so sure, I don't understand it all though, my head is abolutely fried with all this tech stuff, compiling, sudo, gem, .. some of us just want to open the editors and write code LOL.. so I would love to hear any user experiences too!

sources:
Compass [compass-style.org]
Sass/Scss [sass-lang.com]
LESS [lesscss.org]
CSScaffold [github.com]
LESSPHP [leafo.net]

SuzyUK

5:47 pm on Jan 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



another day and head is clearer.. am a little excited now I see more possibilities

We can all play along/try it out if you like, using LESS.js [lesscss.org], no Ruby, gems, or PHP required - though LESS does a PHP port if you fnd you like/use it.

Less uses the "newer" syntax, the one with brackets and semi-colons. the same as what we are used to - a 20min learning curve approx. LESS I think is Scss in Ruby. Sass relies on whitespace for it's nesting, so the LESS way is more intuitive coming from CSS :) - The syntax is like this *I think* because it's the closest to a proposed format should any of these benefits ever get incorporated into CSS itself.

Less JS is easy to play with as it only requires 2 lines in the head element of your page and then it compiles in the browser - the LESS site [lesscss.org] itself uses it in production and I can't say as I noticed a performance hit, apparently the CSS is cached as normal after the first hit, only changing when a less file is changed, and even then JS is much faster than server-side processing - though if hesitant about relying on JS for CSS, you could take the compiled CSS from development and copy/paste it to a static .css for use in production sites rather than rely on JS in production environment

<link rel="stylesheet/less" type="text/css" href="styles.less">
<script src="http://lesscss.googlecode.com/files/less-1.0.41.min.js" type="text/javascript"></script>



the stylesheet must come before the script, have a .less extension and note the
rel="stylesheet/less"
bit.

write the less syntax in the less file (or multiple files), view in the browser.. from there using your favourite web dev tool you can view/copy the actual generated CSS

I've found much more tutorials and info for LESSCSS :), I take from what I've read so far that this might be a preferred development tool for non Ruby apps, because it can be/is being used by much more people for their own development, big or small, a lower barrier to entry if you like

Certainly an advantage to the LESS way, because it uses existing syntax, is that you could just "convert" part of your existing stylesheets as you learn, or want/need to - just copy your existing stylesheet into the LESS file to start with and change little bits at will ;)

You could also use this to merge lots of smaller sheets into one for production, using @import

Importing works pretty much as expected. You can import a .less file, and all the variables in it will be available. If the file is a .less, the extension is optional:
@import "library";
@import "typo.css";


this could mean you/your workshop have multiple sheets for development, even your own wee Library of regularly used snippets, i.e. separating structure, typography, menus etc. but they will compile to one sheet for you keeping the page's http requests to a minimum on production site. obviously the whole point of ScSS/Compass is the same except the compilation takes place server side, though to get started you would need to convert any existing CSS to Sass first, btw.. really not sure about the performance issue of Sass/Compass compared to pre-compiling - it might be something else to consider..

You could also import existing CSS frameworks and use their classes as mixins if that's your thing, a great example is your reset, if you use one, just make a .css file with your preferred reset and @import (include) it into all your .less projects.

I think Compass comes with some common frameworks built in? however for any LESSer mortals just save the framework CSS, import it into you .less file and use their class name as a mixin, or of course build your own preferred one ;) Designshack has a mini-tutorial on making a LESS Grid [designshack.co.uk]

You can also reuse variables(or constants :o) no matter the order of the imports, however even in just a test scenario with 3 imported LESS sheets - I quickly got lost as to which sheet was providing my headings with their color!

I think the downside is that it will actually produce sheets with a lot of unnecessary CSS (especially if importing common frameworks) shorthand properties couljd fall into disuse, but the time to actually produce the sheets would reduce and if compiled into one sheet, page speed should also improve, even if the file has extras, if the alternative is multiple sheets.

My original concerns, about maintenance remain, however anyone new coming along, or the client, would in effect be debugging/amending the plain vanilla CSS sheet and needn't care how it was actually produced!

--
sorry whyyi, I think I may have gone off the topic of your question, but maybe not, I'm coming around to thinking that Sass syntax itself may not be a good idea, because of the learning curve of it's differences, remembering to not use the "hard tabs" and to insert 2 spaces for it's whitespace rules and remembering to drop the semi-colons.. but Scss might be OK as long as it's syntax is in line with the others?

Used with Compass it certainly offers the advantage of precompiling built in as well as some pre-built libraries(which may or may not be an advantage!

MAybe the fact that it's Ruby based and a lot of designers/coders (well me!) are afraid of the terminal/command line or don't have a Mac, or can't do a one click install on their windows box.. kind of made it slow on the uptake? I certainly see more people raving about LESS.js

it's certainly interesting.. I might convert one of personal sites or try to template a few layouts to see how easy it is to reuse! Hope you don't mind me rambling.. I like finding "new to me" things

whyyi

2:30 am on Jan 25, 2011 (gmt 0)

10+ Year Member



wow! this is great suzyuk thanks for the feedback this is definitely some great stuff. I too have been looking into LESS and the more I read about it and ask around the more I'm liking it. I've played around with it for a few different layouts and could really see myself moving forward with it on future projects. It saves a ton of time using variables and reusing classes.

Thanks again for the feedback this is great!

SuzyUK

5:50 pm on Jan 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks whyyi,

there's more! I quickly got frustrated with LESS as it doesn't quite have the MATH capability that ScSS has (though I'm sure cleverer folks than me could make it happen!). I see that as more valuable in using any of these preprocessors e.g. calculating gradients, contrasting color schemes & plugin menu sizes.. things like that

so I installed Ruby and Sass woohoo aint I clever.. actually it's quite easy for those on windows there's a one click Rails installer [railsinstaller.org] and how to. After installing the package already has Ruby and Gems, all you need to do to get to use Sass/Scss is install the haml gem

then save yourself a blank scss or sass file and a corresponding blank CSS file - with a few simple (if I can do it anyone can!) terminal (DOS Prompt - this package gives you a one click to the right place in the "terminal" ) commands you can have the Ruby preprocessor watching and updating your Scss live to the CSS, it automatically writes a CSS file for you.

..
That was at the weekend, I then went looking for pre-built mixins to see what could be plugged in (and I find the best way to learn is to take examples and chop them to bits!) and I see there's not too many yet, and some of them are in an older SASS syntax.. anyway each one, say "rounded corners" was different depending on how many variables the author had allowed for, definitely a lot of scope there for developing ones more customised to your coding style or a workshop's guidelines

Scss is indeed the more forgiving syntax, much more like CSS and doesn't throw a loop about whitespace. I think think the Ruby community might well like Sass better as it suits their style, like haml itself.. it doesn't matter which you use, just change the extension of your file, either scss or sass to suit - Ruby has a convert function built in so you can convert between sass and scss or indeed if you just want to see what CSS a mixin will produce you can convert it css without the watch function

If nothing else if you put your existing messy stylesheet into a scss file and preprocess it, it will return a beautifully formatted CSS, without comments is the default, but you can config it.. it will also minify it too but haven't looked how to yet

Again without using any variables/mixins or maths, if you simply use it to import/include your @imported sheets, it will return one CSS file (a hard copy which you would upload in production sites), this would reduce your http requests but would still mean you can keep your imported sheets separate for development

I've been doing a crash course on math with colors as I think it would be quite easy to plug in say "fancy CSS3 buttons"

in all whyyi, I think if you have this environment available already. I would change my mind and say go for it. It's not an "upgrade" on CSS it's an extension to it for dev environments - and like I said you don't even have to do anything with the Scss/Sass at first just use it for merging sheets (i.e. say you have a sheet for typography, layout structure, effects) and getting pretty (minus development comments) or minified output, anything else could be on an as and when learned basis

oh and in case you didn't see it. as fate would have it, there seems to be something in the wind - CSS Variables, mixins etc [webmasterworld.com]. I think by the sounds of that ScSS/Sass just might be the tool setting the pace?