Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is my first attempt at building a functional (and functioning) website. Thankfully, everything (or everything important) is working as it should, but I know the code is rife with redundancy and it would be extremely helpful to learn how I can improve it- can anyone help me tighten up my code?

http://jsfiddle.net/ayoformayo/Lwr5a/

share|improve this question
4  
Could you include code in the question, please? "This site is for code reviews, which are hard to do when the code is behind a link somewhere out there on the internet. If you want a code review, you must post the relevant snippets of code in your question. It is fine to post a "see more" link (though, do be careful — very few reviewers will be willing to click through and read thousands of lines of your code), but the most important parts of the code must be placed directly in the question." (from the FAQ) – palacsint Aug 4 '12 at 8:35

closed as off topic by palacsint, Michael K Aug 6 '12 at 13:08

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

I'm not allowed to comment so I'll give it real shot...As far as readability: Your html could look better showing hierarchic relation.

<html>
  <head>
     <title>Words</title>
  </head>

  <body>
     <div>
        <h1>Large Header</h1>
        <ul>
          <li>E1</li>
          <li>E2</li>
        </ul>
     </div>
  </body>
</html>

My JS advice might be seen as a personal preference, but instead of doing this

function chooseKnakk() {
    $('Global Persona Type').innerHTML = $('knakkChoice').value;

    ///if choice is a knakk
    if ($('knakkChoice').selectedIndex === 1) {
        $('peapodKnakkFacet').removeClass('hidden');

    }
    ///if choice is company or web
    if ($('knakkChoice').selectedIndex === 2 || $('knakkChoice').selectedIndex === 3) {
        $('dplay').removeClass('hidden');
        $('peapodContentFacet').removeClass('hidden');
    }
    ///if building methodCall
    if ($('knakkChoice').selectedIndex === 4) {
        $('choiceflow').removeClass('hidden');
        $('peapodKnakkFacet').removeClass('hidden');
    }
    ///if building challenge
    if ($('knakkChoice').selectedIndex === 5) {
        $('GP Photo').removeClass('hidden');
        $('gpPhotoLabel').removeClass('hidden');
    }

calling multiple ifs you could either use if else if statements or use a switch. Like so...

if(condition 1 is true){
    //do stuff
} else if(condition 2 is true){
    //do stuff
} else (condition 3 is true){
    //do stuff
}

or...

switch (value to investigate) {
    case 1:
        //do stuff
        break;
    case 2:
        //do stuff and maybe case: 3 stuff too!
    case 3:
        //do at least this stuff, but maybe case: 2 as well!
        break;
    default:
        //this is optional for when no case is met
}

You could achieve a result equivalent to my example if else with a "traditional" switch with break; statements concluding each case, however, I intentionally left off the break at the second case to show that if case: 1 returns false, but case: 2 is true, after it makes it's way through case: 2 it WILL proceed to check case: 3. If that is true as well you'll have multiple cases met so make sure that's what you intend. Lastly, you could consider dropping brackets enclosing single statement ifs the aren't mandatory, or you could use short if like var = (condition) ? (true action) : (false action); but some argue that's even less readable. I don't use them. You also have some areas where you break from your own style. Having multiple-needless blank lines makes it "look" confusing. Keep related stuff together provide one blank line when you feel it shows a new process or event. I also prefer to comment to the right as opposed to above. If there needs to be multiple things explained like a function's or variable's purpose it can look quite uniform that way(IMHO), but I've seen it both ways. Hope that helps at least as far as readability.

share|improve this answer
he can't use 'if else', if he expects more than one condition to be true. 'If else' will make sure than only one is executed and he may not want such behavior. – Senthil Kumar Aug 4 '12 at 8:28
@SenthilKumar- I think that can be seen through observing the logic of an if else. I, perhaps not clearly drawing the distinction, was referring to what would happen in the case of the example switch and not to imply they were equally capable. Thanks for the feedback, feel free to edit to show more clarity! – C. Lang Aug 4 '12 at 8:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.