- 浏览: 2073408 次
- 性别:
- 来自: NYC
文章分类
- 全部博客 (628)
- Linux (53)
- RubyOnRails (294)
- HTML (8)
- 手册指南 (5)
- Mysql (14)
- PHP (3)
- Rails 汇总 (13)
- 读书 (22)
- plugin 插件介绍与应用 (12)
- Flex (2)
- Ruby技巧 (7)
- Gem包介绍 (1)
- javascript Jquery ext prototype (21)
- IT生活 (6)
- 小工具 (4)
- PHP 部署 drupal (1)
- javascript Jquery sort plugin 插件 (2)
- iphone siri ios (1)
- Ruby On Rails (106)
- 编程概念 (1)
- Unit Test (4)
- Ruby 1.9 (24)
- rake (1)
- Postgresql (6)
- ruby (5)
- respond_to? (1)
- method_missing (1)
- git (8)
- Rspec (1)
- ios (1)
- jquery (1)
- Sinatra (1)
最新评论
-
dadadada2x:
user模型里加上 protected def email ...
流行的权限管理 gem devise的定制 -
Sev7en_jun:
shrekting 写道var pattern = /^(0| ...
强悍的ip格式 正则表达式验证 -
jiasanshou:
好文章!!!
RPM包rpmbuild SPEC文件深度说明 -
寻得乐中乐:
link_to其实就是个a标签,使用css控制,添加一个参数: ...
Rails在link_to中加参数 -
aiafei0001:
完全看不懂,不知所然.能表达清楚一点?
"$ is not defined" 的问题怎么办
Why use HAML (and SASS)? I already know HTML.
http://chart.apis.google.com/chart?chs=200x200&cht=p&chd=t:10,20,40,30
The title of the post is more-or-less a verbatim quote from a coworker, as well as from an unrelated colleague of mine from a previous job. I was asked that question and, to be honest, I was a little thrown both times. I didn’t actually know why I use HAML. I guess I’d never really considered it much after I started doing everything with it. It just seemed better and more fun.
So I spent all last night thinking about the answer. Why use HAML? For that matter, why use SASS either? It’s notable, I guess, that yesterday Chris Eppstein was polling his twitter followers on whether they wanted to break SASS and HAML apart to be separate gems … clearly others out there are using one but not the other.
And I suppose it’s not enough to answer that I use HAML and SASS because other luminaries in the Ruby world swear by it. As my Mom was fond of saying, “If all the other kids jumped off a cliff…” So I guess that’s not entirely a valid reason.
So, here’s my attempt to coalesce my various reasons for using HAML (and SASS) into a single post, and to try and convince you why I think it’s a great idea. And to throw a bone to Eppstein, I’ll try to make a case for why, though I have no problem with them being split into separate gems, I still believe they should always go together, just like peanut butter and grape jam.
SIMPLER, SHORTER, LESS WORK
So this is the obvious one. Check out the two chunks of code below:
ERB (525 characters; 24 lines)
Just in terms of key presses, you’re hitting the keys 37% less in this case if you use HAML. In terms of lines of code, you’re getting a 33% reduction. (Hell, Wordpress doesn’t even need to display a scrollbar for the HAML example). I don’t know about you, but to me that’s a significant win right there. Every second I spend typing monotonous stuff is a second I didn’t spend building a feature. And when you’re writing hundreds of lines of code in a day, that can add up to a lot of extra time to either drink beer or add extra polish to the feature you’re implementing.
COGNITIVE DISSONANCE BETWEEN HTML AND CSS
That’s just a fancy way of saying “my brain has to switch gears too much or too drastically, reducing its efficiency.” Anyone who has done significant amounts of HTML and CSS can tell you that the constant back-and-forth between the stylesheets and the html templates becomes something of a rhythm, a development pulse of sorts. But with that rhythm comes delay and context shifts.
In HTML, I type:
In CSS, to style this, I type:
For those of us well-versed in modern web development, that shift isn’t tough, but it’s still a shift.
Using HAML:
That alone improves the contextual parity some, even if you don’t use SASS and just use straight CSS. You type “.highlight” to make the div. You type “.highlight” to style it. Simple.
If you use SASS, it gets even better:
Now, you don’t even have to worry about the curlies (which don’t exist in HTML, causing cognitive dissonance). In fact, the “code” in both the HAML and SASS templates is nearly identical in structure. The only difference is that in the former, you provide the data to display, and in the latter, you provide the presentation styling for that data. If you buy the whole concept behind “separation of concerns” (and you should!), this makes life clean and satisfying.
LOWER BARRIER TO REFACTORING
If you’ve spent as much time as I have looking at someone else’s legacy code in massive enterprise apps, you know how painful it can be to jump in at first. What the hell does this construct even do? Why did they implement this? Why did someone write 6 lines of code that duplicate a function that Ruby/PHP/Java/whatever already has? What are all these non-standard method calls and where do the actual methods live?
This path of fiery hoops must be jumped through before you can really get down to a level of comfort with the code. And you need that comfort to reach your full development speed when implementing or refactoring in that part of the app. The faster you can get there, the sooner you’ll be able to demonstrate your value to the team and to the product.
The bane of my existence has been so-called “cowboy coders,” those individuals who feel it necessary to hack together fast, poorly-considered implementations in order to move on to the next thing and prove to their superiors how “awesome” they are, because they can knock out 10 new features a day. Nice. Good for you, buddy. But you’re costing me, and your team, and therefore yourself, even more time in the long run, with your mistakes, poor architecture that becomes “too big to remove” (hmm…where have I heard that before?), and a product that becomes shackled to the cowboy, who is the only one who understands the parts of the app they wrote. And God forbid if they ever leave the team…
HAML/SASS provide a buffer against the cowboy.
Forcing Harassing Coercing your teammates to use HAML (and SASS) reduces the cognitive complexity of the presentation layer (aka the ‘view’ for you MVC folk), both in terms of number of lines and characters your brain has to process, and in terms of its immediate understandability. And that makes you faster, and your team faster, and your teammates faster. Faster means more features with more polish sooner in the release cycle, with less bugs. And the reduced complexity has the additional benefit of helping to prevent future bugs, because it’s more obvious what’s going on in a particular template. Think of it as the programming equivalent of Scotchgard sprayed on your templates; it might not prevent every stain, but it helps you get the carpet clean quicker.
Sure, the cowboy might still plow through poorly thought-out code (that’s a problem for another day), but at least now it’s easier to understand WHAT he’s doing with 200 lines of business logic jammed into the view. And it’s easier to see why it should be refactored immediately!
ENCOURAGING BEST PRACTICES
At the risk of igniting some holy war about what actually constitutes best practices in the various spheres of HTML, CSS, Ruby, Rails, etc, I think it’s worth discussing the concept of “best practices.” I think adopting Best Practices has gotten a bad rap in the eyes of some developers, especially those for whom learning the latest and greatest tech in the community seems like “following a fad.” Sometimes, the latest and greatest is actually better, because people in the community are actively thinking about better ways to do the things we do every day. Don’t dismiss it out of hand because you’re defending your little island of knowledge. Any time you hear yourself saying, “I already know how to do this. Why should I learn something else?” … stop! Pause and consider your own motivations. There’s a chance you’re dismissing the new tech because you feel threatened by it. Embrace change, people! Embrace new ideas! Try new things! It will make you (A) more educated in your field and (B) more marketable, which gets you (C) better pay and more responsibility. This is a much better and more-fulfilling path to job security than defending your turf and digging in your heels while the programming community moves on past you.
So, that said, I think HAML provides a major advantage here, as well. It’s established best practice that presentation templates (whether in Rails, or Django, or home-brew MVC-based architectures) should not contain business logic or even much in the way of presentational logic. HAML does a great job of gently encouraging you to avoid this problem:
This bit of ruby code is straightforward in HAML and, I would argue, reasonable in the view:
But in ERB, you’ll sometimes see things like this:
While you could do all that in HAML, the inability to break stuff across multiple lines without starting a new hyphen-fronted line makes it feel icky somehow. So then it gently encourages you to break up that code into the model and/or helper methods, where it should have been in the first place.
I’m not saying that HAML and SASS are silver bullets that make you or your teammates code better, but it sure helps. And when someone you know starts bitching about not being able to break a HAML ruby line into multiple lines, you’re probably safe in assuming that they are on the path to doing something Naughty™
CONCLUSION
So there you have it. There are many other reasons to use HAML and SASS that I’m sure I didn’t cover here. The “fun-ness” of it, the beauty of the syntax, the ability to have it render templates in different formatting modes depending on production or development environments, etc. But hopefully this gives you some food for thought on why I use it, and why maybe you should give it a look (or a second look, maybe?)
http://chart.apis.google.com/chart?chs=200x200&cht=p&chd=t:10,20,40,30
The title of the post is more-or-less a verbatim quote from a coworker, as well as from an unrelated colleague of mine from a previous job. I was asked that question and, to be honest, I was a little thrown both times. I didn’t actually know why I use HAML. I guess I’d never really considered it much after I started doing everything with it. It just seemed better and more fun.
So I spent all last night thinking about the answer. Why use HAML? For that matter, why use SASS either? It’s notable, I guess, that yesterday Chris Eppstein was polling his twitter followers on whether they wanted to break SASS and HAML apart to be separate gems … clearly others out there are using one but not the other.
And I suppose it’s not enough to answer that I use HAML and SASS because other luminaries in the Ruby world swear by it. As my Mom was fond of saying, “If all the other kids jumped off a cliff…” So I guess that’s not entirely a valid reason.
So, here’s my attempt to coalesce my various reasons for using HAML (and SASS) into a single post, and to try and convince you why I think it’s a great idea. And to throw a bone to Eppstein, I’ll try to make a case for why, though I have no problem with them being split into separate gems, I still believe they should always go together, just like peanut butter and grape jam.
SIMPLER, SHORTER, LESS WORK
So this is the obvious one. Check out the two chunks of code below:
ERB (525 characters; 24 lines)
<% if @users.empty? %> <div class="empty_collection"> <em>Could not find any users.</em> </div> <% else %> <table class="users_list"> <thead> <tr> <th>Name</th> <th>Email</th> <th>Age</th> </tr> </thead> <tbody> <% @users.each do |user| %> <tr class="<%= cycle('odd','even') %>"> <td><%= user.name %></td> <td><%= user.email %></td> <td><%= user.age %></td> </tr> <% end %> </tbody> </table> <% end %>
HAML (331 characters; 16 lines) - if @users.empty? .empty_collection %em Could not find any users - else %table.users_list %thead %tr %th Name %th Email %th Age %tbody - @users.each do |user| %tr{:class => cycle('odd','even')} %td= user.name %td= user.email %td= user.age
Just in terms of key presses, you’re hitting the keys 37% less in this case if you use HAML. In terms of lines of code, you’re getting a 33% reduction. (Hell, Wordpress doesn’t even need to display a scrollbar for the HAML example). I don’t know about you, but to me that’s a significant win right there. Every second I spend typing monotonous stuff is a second I didn’t spend building a feature. And when you’re writing hundreds of lines of code in a day, that can add up to a lot of extra time to either drink beer or add extra polish to the feature you’re implementing.
COGNITIVE DISSONANCE BETWEEN HTML AND CSS
That’s just a fancy way of saying “my brain has to switch gears too much or too drastically, reducing its efficiency.” Anyone who has done significant amounts of HTML and CSS can tell you that the constant back-and-forth between the stylesheets and the html templates becomes something of a rhythm, a development pulse of sorts. But with that rhythm comes delay and context shifts.
In HTML, I type:
<div class="highlight"> this is some stuff </div>
In CSS, to style this, I type:
.highlight { border: 1px solid #f00 }
For those of us well-versed in modern web development, that shift isn’t tough, but it’s still a shift.
Using HAML:
.highlight this is some stuff
That alone improves the contextual parity some, even if you don’t use SASS and just use straight CSS. You type “.highlight” to make the div. You type “.highlight” to style it. Simple.
If you use SASS, it gets even better:
.highlight border: 1px solid #f00
Now, you don’t even have to worry about the curlies (which don’t exist in HTML, causing cognitive dissonance). In fact, the “code” in both the HAML and SASS templates is nearly identical in structure. The only difference is that in the former, you provide the data to display, and in the latter, you provide the presentation styling for that data. If you buy the whole concept behind “separation of concerns” (and you should!), this makes life clean and satisfying.
LOWER BARRIER TO REFACTORING
If you’ve spent as much time as I have looking at someone else’s legacy code in massive enterprise apps, you know how painful it can be to jump in at first. What the hell does this construct even do? Why did they implement this? Why did someone write 6 lines of code that duplicate a function that Ruby/PHP/Java/whatever already has? What are all these non-standard method calls and where do the actual methods live?
This path of fiery hoops must be jumped through before you can really get down to a level of comfort with the code. And you need that comfort to reach your full development speed when implementing or refactoring in that part of the app. The faster you can get there, the sooner you’ll be able to demonstrate your value to the team and to the product.
The bane of my existence has been so-called “cowboy coders,” those individuals who feel it necessary to hack together fast, poorly-considered implementations in order to move on to the next thing and prove to their superiors how “awesome” they are, because they can knock out 10 new features a day. Nice. Good for you, buddy. But you’re costing me, and your team, and therefore yourself, even more time in the long run, with your mistakes, poor architecture that becomes “too big to remove” (hmm…where have I heard that before?), and a product that becomes shackled to the cowboy, who is the only one who understands the parts of the app they wrote. And God forbid if they ever leave the team…
HAML/SASS provide a buffer against the cowboy.
Forcing Harassing Coercing your teammates to use HAML (and SASS) reduces the cognitive complexity of the presentation layer (aka the ‘view’ for you MVC folk), both in terms of number of lines and characters your brain has to process, and in terms of its immediate understandability. And that makes you faster, and your team faster, and your teammates faster. Faster means more features with more polish sooner in the release cycle, with less bugs. And the reduced complexity has the additional benefit of helping to prevent future bugs, because it’s more obvious what’s going on in a particular template. Think of it as the programming equivalent of Scotchgard sprayed on your templates; it might not prevent every stain, but it helps you get the carpet clean quicker.
Sure, the cowboy might still plow through poorly thought-out code (that’s a problem for another day), but at least now it’s easier to understand WHAT he’s doing with 200 lines of business logic jammed into the view. And it’s easier to see why it should be refactored immediately!
ENCOURAGING BEST PRACTICES
At the risk of igniting some holy war about what actually constitutes best practices in the various spheres of HTML, CSS, Ruby, Rails, etc, I think it’s worth discussing the concept of “best practices.” I think adopting Best Practices has gotten a bad rap in the eyes of some developers, especially those for whom learning the latest and greatest tech in the community seems like “following a fad.” Sometimes, the latest and greatest is actually better, because people in the community are actively thinking about better ways to do the things we do every day. Don’t dismiss it out of hand because you’re defending your little island of knowledge. Any time you hear yourself saying, “I already know how to do this. Why should I learn something else?” … stop! Pause and consider your own motivations. There’s a chance you’re dismissing the new tech because you feel threatened by it. Embrace change, people! Embrace new ideas! Try new things! It will make you (A) more educated in your field and (B) more marketable, which gets you (C) better pay and more responsibility. This is a much better and more-fulfilling path to job security than defending your turf and digging in your heels while the programming community moves on past you.
So, that said, I think HAML provides a major advantage here, as well. It’s established best practice that presentation templates (whether in Rails, or Django, or home-brew MVC-based architectures) should not contain business logic or even much in the way of presentational logic. HAML does a great job of gently encouraging you to avoid this problem:
This bit of ruby code is straightforward in HAML and, I would argue, reasonable in the view:
- @users.each do |user|
But in ERB, you’ll sometimes see things like this:
<% @users.each do |user| full_name = user.first_name + ' ' + user.last_name savings = sprintf("%.2f", user.savings) if savings == 0.0 savings = false savings_row_class = 'red' end %>
While you could do all that in HAML, the inability to break stuff across multiple lines without starting a new hyphen-fronted line makes it feel icky somehow. So then it gently encourages you to break up that code into the model and/or helper methods, where it should have been in the first place.
I’m not saying that HAML and SASS are silver bullets that make you or your teammates code better, but it sure helps. And when someone you know starts bitching about not being able to break a HAML ruby line into multiple lines, you’re probably safe in assuming that they are on the path to doing something Naughty™
CONCLUSION
So there you have it. There are many other reasons to use HAML and SASS that I’m sure I didn’t cover here. The “fun-ness” of it, the beauty of the syntax, the ability to have it render templates in different formatting modes depending on production or development environments, etc. But hopefully this gives you some food for thought on why I use it, and why maybe you should give it a look (or a second look, maybe?)
- google_charts_on_rails.zip (16 KB)
- 下载次数: 12
发表评论
-
Destroying a Postgres DB on Heroku
2013-04-24 10:58 931heroku pg:reset DATABASE -
VIM ctags setup ack
2012-04-17 22:13 3256reference ctags --extra=+f --e ... -
alias_method_chain方法在3.1以后的替代使用方式
2012-02-04 02:14 3289alias_method_chain() 是rails里的一个 ... -
一些快速解决的问题
2012-01-19 12:35 1470问题如下: 引用Could not open library ... -
API service 安全问题
2011-12-04 08:47 1382这是一个长期关注的课题 rest api Service的 ... -
Module方法调用好不好
2011-11-20 01:58 1344以前说,用module给class加singleton方法,和 ... -
一个ajax和rails交互的例子
2011-11-19 01:53 1903首先,这里用了一个,query信息解析的包,如下 https: ... -
Rails 返回hash给javascript
2011-11-19 01:43 2274这是一个特别的,不太正统的需求, 因为,大部分时候,ajax的 ... -
关于Rubymine
2011-11-18 23:21 2266开个帖子收集有关使用上的问题 前一段时间,看到半价就买了。想 ... -
ruby中和javascript中,动态方法的创建
2011-11-18 21:01 1236class Klass def hello(*args) ... -
textmate快捷键 汇总
2011-11-16 07:20 8139TextMate 列编辑模式 按住 Alt 键,用鼠标选择要 ... -
Ruby面试系列六,面试继续面试
2011-11-15 05:55 2018刚才受到打击了,充分报漏了自己基础不扎实,不肯向虎炮等兄弟学习 ... -
说说sharding
2011-11-13 00:53 1485这个东西一面试就有人 ... -
rails面试碎碎念
2011-11-12 23:51 1942面试继续面试 又有问ru ... -
最通常的git push reject 和non-fast forward是因为
2011-11-12 23:29 17212git push To git@github.com:use ... -
Rails 自身的many to many关系 self has_many
2011-11-12 01:43 2733简单点的 #注意外键在person上people: id ... -
Rails 3下的 in place editor edit in place
2011-11-12 01:20 945第一个版本 http://code.google.com/p ... -
Heroku 的诡异问题集合
2011-11-11 07:22 1693开个Post记录,在用heroku过程中的一些诡异问题和要注意 ... -
SCSS 和 SASS 和 HAML 和CoffeeScript
2011-11-07 07:52 12953Asset Pipeline 提供了内建 ... -
Invalid gemspec because of the date format in specification
2011-11-07 02:14 2116又是这个date format的错误。 上次出错忘了,记录下 ...
相关推荐
"s-ublime-text-haml-sass-master.zip"这个压缩包文件显然是为Sublime Text 2设计的一个扩展资源,目的是为了增强其对HAML和SASS这两种语言的支持。 HAML(HTML Abstraction Markup Language)是一种简洁、清晰的...
用法将所有.haml文件保存在/app/views 将所有.sass文件保存在/app/styles 要观看文件运行(在终端中): ./watch要停止观看: Control + C就是这样! 所有已编译的Haml文件都存储在/compiled/ 所有已编译的Sass文件...
**gulp-haml-sass: Web开发的高效工具链** `gulp-haml-sass` 是一个针对Web开发工作流程的工具,它整合了HAML...对于想要提升Web开发效率的团队来说,`gulp-haml-sass`是一个值得尝试的解决方案。
描述:这是一个框架,你可以用它来开发带有haml、sass和coffeescript的cordova应用程序。 还包括backbone.js、jQuery 和bootstrap。 1.从这个存储库中拉取一个新的cordova / phonegap应用程序,事先确保你的git ...
gulp-haml-sass-coffee-livereload 用LiveReload服务器实现HAML,Sass和Coffeescript的gulp种子。 开始吧 运行gulp会编译所有文件,开始监视它们的更改并启动LiveReload服务器。 在终端窗口中按ctrl-c退出服务器。
Jekyll Gulp HAML SASS BrowserSync 这是我自己使用HAML和SASS进行的Jekyll项目风格,可以用作新的Jekyll站点的模板。 安装 npm install -g gulp npm install bundle install
使用 HAML 模板创建一个 Middleman 项目,并准备推送到 Heroku。 在以下技术上运行: 视图的 HAML 用于样式表的 Sass 用于 JavaScript 的 Coffeescript Heroku 上的 Puma 服务器 安装 确保安装了 Middleman gem...
维姆哈姆 该项目包含Vim随附的Haml,Sass和SCSS的运行时文件。 仅在需要最新更新时才需要。 执照 版权所有(c)Tim Pope。 按照与Vim本身相同的条款分发。 请参阅:help license 。
添加了 SASS (Windows).sublime-build、SASS (OSX).sublime-build、SASS (Linux).sublime-build、sasswatch.bat 和 Snippets 文件夹 构建文件将运行 'sass --watch .:.' 在 Sublime 中的当前 SASS 文件上通过 Tools ...
5. **扩展性**:Haml支持Sass,一个强大的CSS预处理器,共同为开发者提供了一整套优雅的前端开发工具链。 **Haml的使用场景** Haml主要应用于Ruby on Rails项目,但在其他框架和环境中也可以通过适配器使用。它...
基础离子项目,含哈姆尔,苏打和咖啡安装依赖项古尔普npm install bower gulp gulp-coffee gulp-concat gulp-minify-css gulp-plumber gulp-rename gulp-ruby-haml gulp-sass gulp-util gulp-watch shelljs gulp-...
这次使用 CoffeeScript、HAML Coffee、SASS、Jasmine、Backbone、Marionette、Underscore 和 jQuery。 包括不同的构建场景,例如开发、测试、登台和生产。目录 gem install compass npm install运行默认任务。 gulp...
Middleman Basis使用: 罗盘哈姆萨斯CoffeeScript 凉亭Modernizr.js Normalize.css 搜索引擎站点地图图像优化Humans.txt入门在开始之前,请确保已经安装了 & 。 安装Middleman -basis作为Middleman模板。 git ...
为 Bootstrap SASS、HAML、Compass 和 Coffeescript 修改的 ##与原始根的区别 grunt-haml-php - 针对最新版本的 MTHAML 进行了修改并修复了编译和 (不依赖于packages.json!) assets/sass - SASS 文件夹 assets/...
$ cd grunt-haml-sass-coffee-boilerplate $ npm install $ grunt server 要求 需要 Sass,可以用gem install sass 需要 Grunt Cli,可以使用sudo npm install -g grunt-cli 作者 迈克尔·杰内西尼
在Ruby开发中,Haml通常与Sass(Syntactically Awesome Style Sheets)配合使用,Sass是CSS的预处理器,可以提高CSS的可维护性和可扩展性。两者结合,可以极大地提升Web开发的效率和代码质量。 使用Haml的一些优势...
使用几行 Sass 和 Haml 生成近 77k 行 CSS 和 13k 行 HTML。 绘制 12800 个 div,每个 div 都有自己的 css 类,该类指定从 Julia 集计算的颜色,C = -0.8 + 0.156i。 请记住,这是一个可怕的想法。
而较新的语法叫做“SCSS”,使用和CSS一样的块语法,即使用大括号将不同的规则分开,使用分号将具体的样式分开。通常情况下,这两套语法通过.sass和.scss两个文件扩展名区分开。相信每个前端开发者都对这种css预处理
在制作窗口雨滴效果,将使用到HAML和Sass。 案例效果 查看演示 源码下载 看到上面的效果是不是有点像人站室内看窗外雨中的夜景,窗户上雨滴的效果是那么的真实,窗外的夜景却又是那么的模糊。咱们不在诗意化了,...