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

Good Code vs Bad Code

Hello everyone,

Would you mind to post some examples of what is a good code and what is a bad code? (HTML, CSS or JavaScript) - and if it's not self explanatory, to explain a little bit why the code is bad.

I get everyone has his own style, but how do the big "no's" look like?

3 Answers

It's not so much about "style" as is about best practices. There is a lot of discussion about the topic, but I would suggest you read the following blog posts about the topic.

JavaScript and CSS guidelines for pragmatists:

https://github.com/stevekwan/best-practices

I hope this helps.

Hi Jacob, I'm sorry but I am new to Github, and can't seem to get what am I doing wrong because when I'm trying to read about CSS I can only see "titles" and no content... how can I read the document?

Thanks!

Cool! this is a pretty important concept to know. As people who review your code will know if you can write clearly and efficiently. I'm glad it helped.

do you have an account?

if not no worries, here is another probably better resource to learn about best practices with CSS:

https://css-tricks.com/css-style-guides/

for the github blog it states with CSS:

"This section is a work in progress. I'll eventually add details, explanations and examples to each of the best practices"

so he just has built points to express the major issues when writing CSS, his JS blog posts are more detailed.

Probably that was the issue then, I don't have a Github account yet :)

Thank you for the css-tricks link!

Edit: Saw your comment in a small delay, alright so I was seeing good. I will check JS blog!

As other answers have mentioned there isn't really bad code unless it doesn't actually function, but there is best practices that a lot of programmers follow, either because it the most effecient and secure way to write the code or it is easier for other programmers to read and also understand.

Not all best practices are the same though.

For example CSS has multiple different ways of being written a few examples are:

BEM (Block Element Modifier)

.block {}
.block__element {}
.block--modifier {}

Examples:

.site-navigation {} /* Block */
.site-navigation__button {} /* Element */
.site-navigation--full {} /* Modifier */
/* BEM */
.person {} /* Block */
.person__hand {} /* Element */
.person--female {} /* Modifier */
.person--female__hand {} /* Element */
.person__hand--left {} /* Modifier */

/* Normal CSS */

.person {}
.hand {}
.female {}
.female-hand {}
.left-hand {}

Both of the above work fine but BEM allows other developers reading your CSS to quickly understand how a class is used and identify which other classes it is related to.

OOCSS (Object Orientated CSS)

The Main princible behind Object Orientated CSS is to write CSS with forethought into code re-use and duplication. Some people do not see it as best practice because it can clutter your HTML but I find myself using OOCSS far more in my personal projects then any other naming convention / CSS syntax because if done correctly can really speed up your site.

Example:

/* Normal CSS */

.header-navigation {
    width: 980px;
    margin: 0 auto;
    position: relative;
    padding-left: 20px;
    padding-right: 20px;
    overflow: hidden;
    padding-top: 20px;
    padding-bottom: 20px;
    height: 260px;
}

.content-wrap {
    width: 980px;
    margin: 0 auto;
    position: relative;
    padding-left: 20px;
    padding-right: 20px;
    overflow: hidden;
        padding-top: 55px;
        padding-left: 20px;
        height: 800px;
}
<div class="header-navigation"></div>
<div class="content-wrap"></div>

As you can see in the above example there is a lot of code duplication between the class .header-navigation and .content-wrap. You can save space and size in your style sheet by creating a class to share the common attributes.

For example:

/* OOCSS */

.globalwidth {
    width: 980px;
    margin: 0 auto;
    position: relative;
    padding-left: 20px;
    padding-right: 20px;
    overflow: hidden;
}

.header-navigation {
    padding-top: 20px;
    padding-bottom: 20px;
    height: 260px;
}

.content-wrap {
        padding-top: 55px;
        padding-left: 20px;
        height: 800px;
}
<div class="globalwidth header-navigation"></div>
<div class="globalwidth content-wrap"></div>

The benifets of OOCSS is that you can easily re-use classes without duplication.

Title CSS

With Title CSS you use a capitalized name for any global CSS class. For any modifier or descendant class you use a lowercase letter.

For example:

<!-- HTML Markup -->
<div class="Title isModified">
    <p class="descendant">
</div><div class="Title isModified">
    <p class="descendant">
</div>
/* Corresponding Title CSS */

.Title {}
    .Title.isModified {}
    .Title .descendant {}

and many more.

In most places I have worked and in most team projects I have worked on I found that BEM seems to be the general benchmark for best practice as it is easier to see how different classes relate to each other and makes life a lot easier in team projects.. but OOCSS and Title CSS are just a valid (I find myself using OOCSS a lot in my own personal projects that do not involve other developers).

Ashley, thank you for taking the time to post this, I really appreciate it! great stuff! :)