Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Bad Practice in Video - Suggest a Standard is applied to example code shown

Hi,

In this video: Including jQuery in our Project, Andrew is demonstrating adding an inline element that's not enclosed by a block level element and suggesting we change the double quotes to single quotes in the jQuery .before() method.

I'd suggest 2 things are wrong with this example.

1) Inline elements should be in a block level element for better HTML practice

2) It's not good practice to use single quotes to surround html attribute values. The better suggestion would be to instruct people to use single quotes around all JavaScript strings unless a double quote is required. This teaches the difference between double and single quotes in JavaScript and it avoids munging html just to output it from the context of jQuery.

I'd suggest establishing some coding standards in the videos to avoid accidentally passing on bad habits to those who are learning.

Also, this particular series isn't as well thought out as it could be. The introduction of methods to hide and insert may be fine, but suggesting that people use an insecure PayPal form isn't a good idea as it may lead to accidental misuse if not better coupled with a series on how to handle server-side security. Using the coded buttons from within PayPal would be a better solution since that's still very easy for copy-paste coding and doesn't introduce the tampering problem the video introduces.

I'd recommend some resources that encourage best-practice and include the in the relevant courses as a way to enhance the course and the techniques students put into practice.

Such as,

  • use all lower case for your tag names, e.g. <h1> </h1>
  • ALWAYS self-close tags properly, such as <img alt="" /> on br, meta, link, img tags, etc.
  • ALWAYS use double quotes to surround attribute values, unless you have a reason to do otherwise (such as including json in a data-* attribute)
  • write & and © and other entities in their entity format in code examples

I'm sure a list can be created to go along with each course as a take-away to encourage industry best practice

UPDATE: In this video, at least 2 references are made to jQuery "transversal". It should be "traversal" and someone looking it up might get confused.

13 Answers

@Andrew - Those are some really good points.

I'm not 100% about on points 2 & 4 of your "such as" list:

But I'm open to having my mind changed

2 great resources on best practices for writing HTML & CSS

Hi James,

While you can have void elements with or without the forward slash to make them self-closing, the practice (which is what I'm encouraging discussion about) of being mindful to close what you open encourages fewer mistakes than having one rule for void elements and one rule for elements that could contain content. My preference is to encourage the practice of deliberate closing. You can also look at the problems created by not having to have a head or body tag in your document. It will still parse and render, mostly, correctly, but it's discouraged. For the same reason, I would encourage a stricter practice for coding, even if it's not a practice required by the standards body.

When I write CSS, I don't need a semi-colon following the last property's value, but I add it because it's good form to stick to one rule, consistently. If I add another rule, I don't need to remember to add a semi-colon to the previous last rule. Similarly, if I move the rule, I don't need to worry about invalidating the css rules that follow.

In html5, the looser html parser (as opposed to xml) also allows you to avoid putting closing tags in, avoid putting quotes in to attributes and allows you to put in properties as booleans.

However, these practices can lead to more mistakes. And those learning here need the fewest mixed messages while learning.

So, while having less stringent rules allows you to get away with making your code less rigid, it's usually through experience that a developer can apply those rules consistently and have a lower chance of making a mistake when employing the looser syntax rules.

I think students should be taught the most strict syntax rules to encourage good practice and then encouraged to read about where they can loosen up, rather than provide a mixed set of examples without context. If the lessons are taught one way or the other, for these points, it would be fine, but don't mix and match, is the message I'm trying to get across.

When you read the guidelines by google, you'll see a mix of 'best practice' and opinion, such as the CSS one above, adding new line characters, etc. If you gzip your code, those new line characters are removed, but gzipping is encouraged to speed up delivery where supported.

So, their guidelines are not rules, but things to help developers code more consistently. I suggest a set of references or standard be promoted along with the courses, even if those references are on opposite sides of a particular view point. It will help encourage a 'way' of coding that's consistent.

Indentation by spaces should be indentation by tabs. Tabs take fewer characters and are configurable by almost all IDEs to represent a number of spaces that the user feels comfortable with. The reverse is not true with spaces, so this presents a usability issue when changing environments or working with other coders that's not presented by using tabs.

In the common world of web development, you can't rely on your server and your meta tags being in sync' with each other or to be sending utf8 at all.

So, in the lessons we should encourage the use of utf8 AND the use of the appropriate character OR make it clear that you can use the entity if you're not using utf8, but not, again, mix and match in the examples.

Consistency and standards help students achieve an appropriate quality, but mixing the messages can lead to haphazard quality in the code produced by students of the same course.

Do you think we could encourage the addition of more authoritative reference material or videos that follow a style-guide just for consistency's sake?

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Thank you for your feedback. Extra credit for both of you :)

(I'll add the transversal/traversal point to QA fixes.)

Hi Andrew!

Keep up the good work :) I love the content. I'm just picky because I teach web development to my brothers and I see little things like these trip them up so often that we've got some standard practice guidelines I make them follow while they are learning.

The fewer points of distraction there are, the fewer points of failure while learning.

Thanks! Andrew

@Andrew - What do you think of these coding guidelines for teaching those new to HTML.

Always do the with HTML

  • Self-close tags
  • Use double quotes for attributes
  • Use UTF-8
  • Use entities only for characters which have a special meaning in HTML such as &, <, > & "
  • Indent nested tag with one tab
  • Use semantically meaningful names for classes & IDs
    • E.g. Use this <p class="warning">...</p> over this <p id="big1">...</p> `

Hi James,

I 100% agree with those guidelines :)

I might suggest, practice and maintain a consistent form of indenting of your code and possibly naming classes and ids as close to the purpose of an element as you can rather than naming it based on its styling.

E.g. <p id="big1">...</p> vs <p class="warning">...</p>

Love it :)

@Andrew - I updated the list guidelines for indenting nested tags & semantic naming of IDs & classes.

I open to the idea of, adding a suggestion about configuring an IDE to convert tabs to spaces, if so use 4 spaces.

Agreed - as long as it's standardised, most people can work with it, and it makes it easier when you return to your own work later.

Love it :) Part of good coding is good habits - I like your list.

Thanks James :)

The very first programming class I took back in high school, one of the things we were graded on was the readability of our code that instilled in me a very strong views on consistent indentation & naming conventions.

A few weeks ago I put together this HTML5 style guide which might interest you.

Hi James,

That's an excellent resource - if it can be presented to treehouse members as a guide - I think it would be a brilliant reference!

I agree with all the statements there :)

@Andrew - I'll write it up

James,

Not sure if it's been posted somewhere else on the site but that style guide is awesome! Not what I came into this thread for but I am stoked that I did!

Kris