Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Make code more compact

  1. #1
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Make code more compact

    Hi guys,

    The code below works. The code does what it should do. But is there a way to make the code more compact. Writing "beautifull code". Someone got some suggestions?

      public void bubbleSort(){
            int outer, inner;
            NAW temp;
     
            for(outer = nElems-1; outer > 0; outer--){
                for(inner=0; inner < nElems-1; inner++){
                    int compareName = nawdata[inner].getName().compareTo(nawdata[inner+1].getName());
                    if (compareName >= 0){
                        temp = nawdata[inner]; 
                        nawdata[inner] = nawdata[inner+1]; 
                        nawdata[inner+1] = temp; 
                    }  
                    }
            }
            for(outer = nElems-1; outer > 0; outer--){
                for(inner=0; inner < nElems-1; inner++){
                    int compareName = nawdata[inner].getName().compareTo(nawdata[inner+1].getName());
                    if (compareName == 0){
                       int compareResidence = nawdata[inner].getResidence().compareTo(nawdata[inner+1].getResidence());
                       if (compareResidence >= 0){
                       temp = nawdata[inner]; 
                       nawdata[inner] = nawdata[inner+1]; 
                       nawdata[inner+1] = temp; 
                       }  
                    }  
                }
            }
        }


    --- Update ---

    note:

    the code sorts an array with strings name, address, residence alphabetical.
    first it sorts the name, when the name is the same it sorts the residence alphabetical.
    System.out.println(" dream in code ");


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,476
    My Mood
    Mellow
    Thanks
    151
    Thanked 609 Times in 596 Posts

    Default Re: Make code more compact

    Compact code is overrated. In fact, striving for compactness might be something you've imagined as desirable, and you could be wrong. Strive for efficient, elegant code. (Elegant code is hard to define, but you know it when you see it.)

    Why aren't there any comments in your code? Why do you have to tell us what this code is supposed to do when we should be able to determine that from reading the code? (And we can't.)

    Your variable names aren't great. For example, someone reading the code should understand what the variables 'outer' and 'inner' do by reading their names.

    The method is poorly named, and it might be doing too much. Based on your updated description of what the code does, a method called sortByName() and another called sortByResidence() might be appropriate. Or maybe just one method called, sortByNameThenResidence().

    Source code line lengths should be broken at a reasonable length. 80 columns is the commonly accepted length.

Similar Threads

  1. Can I make this code better?
    By hrenfrow in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 23rd, 2013, 09:33 AM
  2. can someone please make this code
    By poldz123 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 19th, 2012, 05:50 PM
  3. I NEED HELP, HOW TO MAKE THIS CODE!!!
    By tahsim in forum Member Introductions
    Replies: 1
    Last Post: March 20th, 2012, 09:05 AM
  4. How to make this code even simpler
    By blakmaze in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2011, 03:00 PM
  5. How to make code more efficient?
    By Apocalypse in forum Java Theory & Questions
    Replies: 2
    Last Post: October 21st, 2011, 09:07 AM