Results 1 to 3 of 3
  1. #1
    shivam0101 is offline Member
    Join Date
    Mar 2010
    Posts
    28
    Rep Power
    0

    Default What is the best practice in wrting the same code

    Hi,

    This i posted in stackoverflow.com, i received some suggestions, i also like to get more suggestions from your site.


    Is it a good practice to use properties as local variable. In cases where there are many methods which uses some variables, In each method the variable value changes. This avoids many times creating new variables and the code increases. Any suggestion?

    private void method1(){
    int totalLength = length1 + 10;
    int totalBreath = (breath1 + breath2) + 20;
    int size = (totalLength * totalLength);
    System.out.println(size);
    }


    private void method2(){
    int totalLength = length1 + 20;
    int totalBreath = (breath1 + breath2) + 30;
    int size = (totalLength * totalLength);
    System.out.println(size);
    }


    private void method3(){
    int totalLength = length1 + 60;
    int totalBreath = (breath1 + breath2) + 10;
    int size = (totalLength * totalLength);
    System.out.println(size);
    }
    As you can see, totalLength, totalBreath, size is repeated in every method. Can i make them as fields of the class? So, i need not declare it in every method.

    private void method1(){
    totalLength = length1 + 10;
    totalBreath = (breath1 + breath2) + 20;
    size = (totalLength * totalLength);
    System.out.println(size);
    }
    Last edited by shivam0101; 08-14-2012 at 05:35 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: What is the best practice in wrting the same code

    Hello! Please use [code][/code] tags when posting code so we can read with ease :D

    To your question,
    It is good practice to only make a variable a class/instance variable if that variable itself is going to be used in multiple places. In your case, you're not really using the variables anywhere other than locally (the values only have meaning in the context of the method they are currently being used in). So, you should keep them local.

    If you think of variables as 'attributes' as they are sometimes referred, then making them class-scope makes no sense at all.

    A general rule is if the variable only has meaning in a local context, keep it local. You don't really gain anything other than confusion by putting it in the class.

  3. #3
    shivam0101 is offline Member
    Join Date
    Mar 2010
    Posts
    28
    Rep Power
    0

    Default Re: What is the best practice in wrting the same code

    Quote Originally Posted by quad64bit View Post
    Hello! Please use [code][/code] tags when posting code so we can read with ease :D

    To your question,
    It is good practice to only make a variable a class/instance variable if that variable itself is going to be used in multiple places. In your case, you're not really using the variables anywhere other than locally (the values only have meaning in the context of the method they are currently being used in). So, you should keep them local.

    If you think of variables as 'attributes' as they are sometimes referred, then making them class-scope makes no sense at all.

    A general rule is if the variable only has meaning in a local context, keep it local. You don't really gain anything other than confusion by putting it in the class.
    Nice and crisp answer. Thanks

Similar Threads

  1. Educational practice in NYC
    By barss in forum Jobs Discussion
    Replies: 1
    Last Post: 08-19-2011, 02:22 PM
  2. GUI - is this bad practice?
    By keo in forum New To Java
    Replies: 3
    Last Post: 05-01-2011, 01:40 PM
  3. I need help on a practice code for java
    By Sageinquisitor in forum New To Java
    Replies: 12
    Last Post: 07-13-2010, 05:00 PM
  4. SCJA - practice
    By Mihail Kravsun in forum Java Certification
    Replies: 2
    Last Post: 04-29-2009, 08:26 AM
  5. Best practice of IF loop with NOT
    By sathishranganathan in forum Advanced Java
    Replies: 13
    Last Post: 05-29-2008, 01:11 PM

Tags for this Thread

Posting Permissions

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