SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,622
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    [code review] More efficient way?

    The goal is to get the array of family members made and print out the results in the order created. Is there any way to tidy this up ?
    Code:
    // Our Person constructor
    function Person(name,age)
    {
        this.name=name;
        this.age=age;
    }
    
    // Now we can make an array of people
    var family=new Array();
    family[0]=new Person("alice", 40);
    family[1]=new Person("bob", 42);
    family[2]=new Person("michelle", 8);
    family[3]=new Person("timmy", 6);
    
    // loop through our new array
    for(i=0;i<family.length;i++)
    {
        console.log(family[i].name);
    }
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by RyanReese View Post
    The goal is to get the array of family members made and print out the results in the order created. Is there any way to tidy this up ?
    There are some ways to tidy things up.

    Code:
    // Our Person constructor
    This comment doesn't explain anything that isn't already known. Comments should not say what is happening, they should explain the purpose instead, where the code can not be made clear enough to provide such an explanation for you. Instead of explaining what the code is already fully capable of showing you from the code itself, it's better to use comments to explain why things occur, or to provide a broad overview instead.

    Code:
    function Person(name,age)
    Spacing. A space after the comma is best practice to help aid in readability.

    Code:
    {
    Coding techniques from other programming languages, such as putting the curly brace at the start of the next line, is not just a bad idea - it can actually break JavaScript. Due to syntax limitations in JavaScript and how automatic semicolon insertion works, it is a fundamental principle in JavaScript that placing the curly brace at the start of a new line, is a practice that results in more fragile code.

    Code:
        this.name=name;
        this.age=age;
    Spacing before and after the equals sign is best practice to help improve readability of the code.

    Code:
    }
    That line is good, it has no problems.

    Code:
    // Now we can make an array of people
    Explaining what is happening once again, which is not a good use for comments.

    Code:
    var family=new Array();
    family[0]=new Person("alice", 40);
    family[1]=new Person("bob", 42);
    family[2]=new Person("michelle", 8);
    family[3]=new Person("timmy", 6);
    It is preferred for the sake of consistency to use [] instead of new Array.
    You can also create those new Person objects at the same time as defining the array, for example with:

    It is also a common convention in JavaScript to use single quotes to delimit strings instead of double. Both work, but single provides more convenience than double.

    Code javascript:
    var family = [
        new Person('alice', 40),
        new Person('bob', 42),
        new Person('michelle', 8),
        new Person('timmy', 6)
    ];

    Due to the duplication that we see here though, it may be worth considering storing the name/age in an array, and using a loop to create the new Person objects instead. Especially if more people are planned to be added in further development.

    Code:
    // loop through our new array
    Another useless comment.

    Code:
    for(i=0;i<family.length;i++)
    The i variable has not been declared so it is going to be a global one instead. That's a bad mistake. The best way to remedy that is to declare the i variable right at the start of the code, with the other var declaration.

    Spacing should be added after the for word, which helps to reinforce that it is not a function name being called.
    Also, spacing before and after the equals sign, after the semicolons, and before and after the less than sign for improved readability.

    Code:
    {
    Should come after the closing parenthesis of the for statement, with a space separating the two.

    Code:
        console.log(family[i].name);
    Be aware that Internet Explorer doesn't support console.log - there are shims though that help to provide support, such as https://github.com/kayahr/console-shim

    Code:
    }
    No troubles with this line

    So what we end up with is something like the following:

    Code javascript:
    // Create and show family info
     
    function Person(name, age) {
        this.name = name;
        this.age = age;
    }
     
    var family = [
        new Person('alice', 40),
        new Person('bob', 42),
        new Person('michelle', 8),
        new Person('timmy', 6)
    ],
        i;
     
    for (i = 0; i < family.length; i += 1) {
        console.log(family[i].name);
    }

    I have also replaced i++ with i += 1 because I agree with Douglas Crockford, that incrementors and decrementors are not as good to use as other techniques that make it clearer what is going on.
    For some details, see http://stackoverflow.com/questions/9...-in-javascript
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,622
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Comments-I didn't create the comments. I just kept them there because of being lazy. So I'm ignoring any suggestions for that . No offense to you.

    As for readability, I prefer no spaces in stuff such as (name,age). It helps my readability.

    I was mainly just asking for advice on how to shorten code, which you were able to answer with the array advice.

    console.log is just the part of the program that I'm supposed to write. I mean, I'm using codecademy.com and they force me to write console.log. Though again, comments based on that don't help me shorten the code.

    Thank you Paul for the array advice .
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by RyanReese View Post
    I was mainly just asking for advice on how to shorten code, which you were able to answer with the array advice.
    Another potential way to do that is to use the forEach method instead, which allows you to simplify things, and you won't need that variable either.

    Code javascript:
    family.forEach(function (person) {
        console.log(person.name);
    });

    The forEach method is available on all modern web browsers. If you want it to succeed on Internet explorer, the forEach documentation page has compatibility code that adds the capability for web browsers that don't know how.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,622
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Wow, that's some efficient looking code.

    Thanks for the advice Paul.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •