The GitHub Styleguide
github.com
github.com
"The names of potentially 'dangerous' methods (i.e. methods that modify self or the arguments, exit!, etc.) should end with an exclamation mark. Bang methods should only exist if a non-bang method exists."
That last sentence finally made me understand why `string.gsub!` takes a bang but `FileUtils.rm_rf(dir)` does not, even though the latter is far more dangerous (in the generic case). It's not just about the danger level of the method but about the existence of a non-dangerous alternative. Now I've started noticing that some gems do not follow this guideline.
The only example I know of is DataMapper that uses bang methods such as `save!` for the methods that change data bypassing the validation mechanism. But I would argue that this use fits the original definition quite nicely.
You are right, though. Many Rubyists do not follow this convention and often don't even know about it. It's a key convention of the core implementation team, however.
Compare `some` and `every?` in Clojure:
http://clojure.github.com/clojure/clojure.core-api.html#cloj...
http://clojure.github.com/clojure/clojure.core-api.html#cloj...
And in Scheme `member` doesn't have a question mark for this reason, it returns the cdr of the list starting with the item if it's found.
It's a subtle point, and probably trips up newcomers, but I think it's useful, especially since the types of methods and functions in these languages are not as well broadcast as in statically typed languages.
It's pretty disappointing to see GitHub, a company that a lot of developers (including myself) look up to and will follow, espousing something like this.
If you are still using something like packer, that bit of advice will take about 5 minutes of work, and give you significant decreasing of file size.
Semi-colons need to be in 1 place, the beginning of a line that starts with a (, since that is the one place that automatic semicolon insertion will really screw you. If it makes you feel good to write a semi colon at the end of every line I don't think anyone is stopping you, but it isn't the language or tools that require it. You can also feel free to explicitly wrap every statement in parens (because it is necessary in a few cases), or end each line with a // after your semi-colon, all these are freedoms the language gives you, but are as necessary as semi-colons.
I haven't been using unnecessary semi-colons in js for years, and have yet to run into a problem.
Did you read the article they linked to? I did. I still disagree with the style rule, but I don't feel like I'd win an argument with them about it.
I don't even have a problem with people who want to put semi-colons all over the place, I am just tired of having to defend myself on why I choose not to. And it feels kind of odd that I have to, because I am not the one adding redundant characters everywhere
$ curl -I https://github.com/styleguide/javascript
HTTP/1.1 301 Moved Permanently
Location: https://github.com/styleguide/coffeescriptThis isn't a debate as to whether or not CoffeeScript is better, easier, cleaner, etc. It's a simple A does not equal B statement, and it's starting to remind me of the way my boss says I'm "good at Java".
"..my advice is to insert them (semicolons) immediately before the opening parenthesis or square bracket in any statement that begins with one of those tokens, or any which begins with one of the arithmetic operator tokens /, +, or - if you should happen to write such a statement..." (http://mislav.uniqpath.com/2010/05/semicolons/)
Then I would say it is really not a good idea.
"The and and or keywords are banned. It's just not worth it. Always use && and || instead."
sad trombone
# boolean expression
if some_condition && some_other_condition
do_something
end
# control flow
document.saved? or document.save!Prefixing each commented line is more efficient in text editing than wrapping things in blocks. Editor support for mappings to comment out sections of text are more consistent in supporting prefixing each line and removing that than wrapping and unwrapping a selection with the block syntax.
Also, that way you can comment out a larger section and subsequently uncomment a part of that section with a single action instead of uncommenting the entire thing, then re-commenting the part that you still want commented out.
It's a really insignificant detail in and of itself, and zefhous' comment provides a far more substantial rationale.
Use def self.method to define singleton methods.
...
# Also possible and convenient when you
# have to define many singleton methods.
class << self
I would argue against using "class << self" especially when you have many singleton methods. If the class is large enough, it's easy to miss the "class << self" and incorrectly read a class method as an instance method.* Alphabetize properties within each CSS rule
To here: https://github.com/styleguide/css
The only advantage from alphabetizing rules is perhaps slightly faster scanning of rules, but I don't think it is even very helpful in doing that. You generally don't have that many rules in a single style anyway so it's not a problem that needs solving.
However, by grouping related styles I think there are a some small yet worthwhile advantages. Grouped styles can reveal intention, while alphabetizing does not at all. Grouped styles can also make refactoring quicker and less tedious.
# bad
email_with_name = user.name + ' <' + user.email + '>'
# good
email_with_name = "#{user.name} <#{user.email}>"
# better
email_with_name = "%s <%s>" % [user.name, user.email] email_with_name = "%s <%s>" % [user.name, user.email]
can be easily identified even when many fields are added.On the contrary, strings like
email_with_name = "#{user.name} <#{user.email}>"
become quite cramped as soon as you use three or more fields. puts "#{num} #{vessels} of #{liquid} on the #{where}, you #{verb1} one #{adverb1}, #{verb2} it #{adverb2}, #{num - 1} #{vessels} of #{liquid} on the #{where}"
puts "%d %s of %s on the %s, you %s one %s, %s it %s, %i %s of %s on the %s", [ num, vessels, liquid, where, verb1, adverb1, verb2, adverb2, num - 1 ]
It's an extreme example, but you just read the first, while you have to think about the second.Currently I prefer using "private" indented to the same level as "def", with no change of indentation of code after "private", and an empty line before and after "private".
Recently, a coworker and myself were debating the use of tabs vs. spaces - and while I know this is a battle that has been going on for a while and won't end any time soon - one of my points against spaces was due to readability. 2 space soft-tabs seems much harder to follow - while using hard tabs for indentation, you are able to adjust your editor to visually work out what suits you best (2 spaces, 4 spaces or even 8 spaces).
I can't for the life of me figure out how to replicate the effect in vim to be able to read his files logically, and he can't tell me because he's an emacs user.
set ts=8 sw=4 et sta
(tabstops to 8 (the default), shift width to 4, expand tabs, smart tabs)
I enjoy 8-space indents, fellow programmers prefer 4-space indents, some others 2-space indents. With hard-tabs each of us is able to use their preferred settings without messing with the commits (setting the tab-to-space ratio in text editor).
Development projects are almost always team efforts. And, unfortunately, there's almost always one or two team members who aren't very good. Some of you folks that only work on startups with brilliant people might disagree, but in my experience most development teams have some bad apples who have let their tech skills rot, or won't try for some reason or another.
These people are going to have a tremendous time just writing halfway competent javascript (especially since it's probably not similar to their OOP language of choice). Suggesting they write code in an uncommon way is just begging for trouble. Most of the examples on the internet use semicolons, as do most of the frameworks. In fact, I hope that members of my future teams never see this javascript style guide or even know that it's possible to omit semicolons in many cases.
I'm quite sure someday soon I'm going to hear this over the cube wall: "Hey, GitHub doesn't use semicolons in their javascript so I won't either!" And then I will be very, very sad.
Once you get into the habit, it requires zero extra effort. You just know when to put them in, same way you just know when to use parens vs curlies.
Coding with semis is like coding with parens around every expression; unnecessary and paranoid.
> Coding with semis is like coding with parens around every expression;
> unnecessary and paranoid.
Unless, of course, one is using Scheme or some other Lisp variant.I think in general the answer may be to use the style that mimics the other code in the application. Rubyists may prefer the newline endings; those using Java or PHP may find having the semi-colon more comfortable. Having less contextual switch between languages may be easier.
Such a sweeping statement seemed a bit ridiculous. Choosing to interpret it in a broader context was intended to highlight my opinion of the original statement.
EDIT: It's been fun watching the mod of the GP go up and down ... zero, back to one, then to zero ... over and over.
He said that there were no problems.
No problems doesn't imply no benefits.
I guess I've never understood the argument for omitting them. It seems like an unnecessary trick. Coding guidelines should enforce the simplest possible patterns and techniques.
The guide doesn't have anything about brackets and spacing on function calls (maybe because it's dropping the language).
I'd rather have nicely crafted and long winded javascript with convention followed, than half javascript and half coffeescript which will hurt people's brain at some stage.
Eventually, that stuff bites you and productivity - all that for the fame of using a brand new language (no offence intended) in the enterprise world. Assuming code is meant to live on for 4-5 years at least here.