My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

Sunday, November 27, 2011

About Felix's Style Guide

This was a long sarcastic post that didn’t produce the desired effect.

I have decided to remove it from the Internet and summarize what I think about style guides in few points:

  • read them if you think you need to
  • keep in mind meaningful points
  • use common sense instead of stubborn mechanic rules
  • don’t impose rules to your team; discuss what’s worth discussing

Use a linter at least to catch obvious mistakes and write code as relaxed and reasonable as you can.

We really don’t have to make programming that boring, neither to stress ourselves with stubborn rules that at the end of the day won’t improve the overall code quality at all.

The end.


Andreas Bergmeier said...

I am not a big fun to I am not a big fan

Andrea Giammarchi said...

ahahah ... typo fixed, thanks

trenc said...

To be correct:

»it's OK« !== »it’OK«

And only single quotes are correctly highlited by Kate (KDE Editor) - but ok, this could be their faults.

Angus Croll said...

Excellent! Amazingly I agree with almost every word. As Felix himself said: "Programming is not about remembering stupid rules".

Crockford did not become Crockford by slavishly following his own rules. He made mistakes, he experimented, he learned, he understood.

Andrea Giammarchi said...

trenc "it's OK" === 'it\'s OK' since ever in JavaScript

Dusty Jewett said...

I haven't read the original style guide, but all of the things you're calling out as 'wrong' seem to be common sense.

Quotes: It doesn't actually matter which you always use, as long as you always use one. It makes debugging easier, as you're always escaping the same things. As you'll find yourself escaping HTML more often than sentences, it makes sense to use single quotes. (as double quotes for attributes is the defacto standard)

Variable Declarations: I don't understand your point on this, your previous section on quotes talks about the 'silly rules', but then you're suggesting a silly rule of declaring variables at the top of a function. This isn't C, you can declare your variables wherever you want. Either you want conventions or you don't... don't switch between the two just so you can critique someone's style guide.

Constants: this is absurd. Just stick with the convention that every language uses... you don't need to create actual constants if you always treat constants as constants. Your define() function fixes a problem that doesn't exist.

Equality Operator: Again, the triple equality helps with the readability and debugging of code. So what if there are hacks that let you do strange things, the maintainability of the code is drastically reduced for every 'hack' you use.

I do agree that you should avoid undefined, and declare variables as null... but if you're writing code that interfaces with external dependencies, you still have to test for undefined.

'Enhancing' built-in objects: Utterly, completely, irrefutably wrong. There is no time, EVER, that you should EVER, EVER modify the prototype of a built-in object. If you want additional functionality, create a new 'class' of object to use. Your example is EXACTLY why this should never be done... .empty() should be an action, while .empty or .isEmpty() would be the accessors to determine if the array is empty.

"property" in object || ( = defaultValue);
Really? You're going to suggest code that ONLY works when a property is undefined, two paragraphs after you just say that you should define everything as 'null'.
Again, this is utterly wrong, view my examples to see why:

Function lengths: This is industry standard. Sure, there are exceptions, it's not a hard-and-fast rule, but if you really have an issue with this recommendation, you need to reconsider writing code for a living.

Andrea Giammarchi said...

Nothing you said makes sense but feel free to use your rules.

Quotes ... no debugging is made easier with quotes

Vars on top: I explained why it makes sense to find vsrs on top. var is not let, var at the end of a function is available at the beginning so the beginning is the right place to find vriables, not in the wild

Constants: are not absurd, if you want to ensure no code can change them it's about security and reliability. const is a de facto standard too onserver side JS plus there are ways to define them with objects.
Which part is absurd

Equality: triple does not help anyone ... understanding the difference between == and === does help developers.
You apparently did not get it since you said I said to set everything as null ... I never even thought about it, improve your knowledge 'cause == null does not meen you need to set everything as null.
There's nothing to test against undefined but if you have to, == null is what you need as check.

Enhancing built in Objects is not bad in an absolute way ... the empty was an example after disambiguation.
I would not use that but that name disturbs you, set it as getter rather than value and you made my exact point I wrote there.

"property" in object once again you misunderstand accessors and the meaning of undefined.
Read this link to know more.

On Functions length you just trolled around ... what you say does not make sense neither is what I wrote.
I wonder if you actually code JS for living but it does not really matter to me.

Thanks for misunderstanding the whole post.

Dusty Jewett said...

You're right, I don't just do JavaScript for a living... I also use Java (Server-Side and Android), PHP, Python, AS3. I work on a team with other Senior SDEs that are expected to be able to read, understand and review each others' code at all times.

If JavaScripters ever want to have an ounce of respect, they need to learn how to develop maintainable software, rather than writing 'scripts'.

As it is, nearly every web dev we hire has to be taught how to write good, maintainable code. It's "Javascript Jedis" like you that give poor advice to aspiring Devs, confusing them beyond belief.

Being an engineer means understanding that convention is extremely important. Always quoting strings in the same way, never assigning a value to a variable that is IN_ALL_CAPS, keeping functions to a reasonable length... each of these practices are vital when you have to work with a team and maintain the code that you write.

And I understand that reassigning properties can be slow. Take a look at this JSFiddle Example to see how your proposed solution fails. If you're writing an API that is called by external code, you need to check for declared properties that are undefined and declared properties that are null. If it's all internal and you have tight control over what is being passed into your function, your code can work properly, but then you probably don't need to do these sorts of checks.

Andrea Giammarchi said...

You are saying constants should not be used anywhere since the practice is to do not reassign upper case variables plus you are inventing I said functions should be big plus you misunderstood the whole concept of properties access plus you used document.write ... once again, learn JavaScript, and feel free to use this blog as well to start with and read links I post or you will be yet another blind wannabe pretending to know a language because of few followed and never understood rules.

If you knew JavaScript you would not consider it "just writing script" ... I am a Senior SW Engineer since ages and I am promoting better knowledge of the JavaScript langauge for developers like you so that you will always be able to understand and maintain other developers code, inside your tiny team , or in the cloud with Open Source Softwares.

Have a nice evening/mirning

Andrea Giammarchi said...

just as extra hint

Dusty Jewett said...

We're still talking about the style guide, right? Because that's what your original article is about.

I'm sure that the things you mention are ways to optimize pieces of code, but they come at a price in readability and maintainability. These are the things that should be of primary concern in a Style Guide.

And you're right, I seem to have misread your Functions comment, and assumed you were calling it out because you disagreed with it. Your section does make it seem like you don't think it should be in the style guide, either way.

I still think your "Right" way of default assignment is a terrible suggestion to include in a style guide. There are so many ways that bad things can happen (null, undefined, false values)... It shouldn't be the 'default' way of coding it. The "Better" way is the "Right" way, and the "Right" way is the "Optimized after you've ensured that things can't possibly go wrong" way.

And please forgive my shortcuts in the jsfiddle... I just wanted a bare-bones example of how things go wrong.

My primary concern is that JS Developers fall neatly into two camps... 80% seem to be the ones that think that a 6000 line .js file with 300 some global variables is OK (an example of code I inherited from someone who called himself a "Senior Engineer"), and 20% who are so deep into the language that they have separate optimizations for the different JS Engines.

The 20% don't need a style guide. The style guide is for people who forget whether they should have underscores_in_variables or use camelCase. Its about establishing a better baseline.

Andrea Giammarchi said...

3 comments to start making sense and put a name on your sentences, you got promoted from troll to blog follower: congratulations!

trenc said...

@Andrea Giammarchi

"it's OK" === 'it\'s OK' since ever in JavaScript

Yes, but that’s not what I mean.

See my post: You write »It's OK« with a single quote. This is common but wrong. It should be an apostrophe ’. so no escaping i required.