0

I'm a newbie in software development. Just wondering which code is better and why should I continue which pattern I should follow.

First Snippet:

Class TestClass
{
    private Object1 field = null;
    private Object2 field2 = null;

    public void TestMethod1() 
    {
          field = new Object1();
          field2 = new Object2();     
    }

    public void TestMethod2() 
    {
          field = new Object1();
          field2 = new Object2();     
    }
}

As you can see, I created fields in Class level and instantiate them inside the method.

Second Snippet:

Class TestClass
{

    public void TestMethod1() 
    {
          Object1 field = new Object1();
          Object2 field2 = new Object2(); 
    }

    public void TestMethod2() 
    {
          Object1 field = new Object1();
          Object2 field2 = new Object2(); 
    }
}

Here I created and instantiated the fields.

These fields are being used over and over in many methods in a class.

  • 1
    if fields make any sense together, put them together. do not create a class just because language allows you to do so. – Ashish Negi Feb 25 '15 at 12:08
  • one may argue that conceptually, this has been addressed in How would you know if you've written readable and easily maintainable code? If your peers keep complaining about your way of doing things, be it one way or another, you better change to make them feel better – gnat Feb 25 '15 at 12:39
1

It actually depends. I would only create a Class which contains instance attributes if more than one of its methods uses it. On the other hand, if an attribute (or variable) is only used inside the method, then it should be defined local to the method it uses.

In the example you describe, I think that you are using functions instead of methods, which to me seems like they are independent between each method execution. (This comes from the "test" word).

If both objects instances you plan to use are the same, maybe define in the class level and instantiate both in the setup method.

  • If i'm correct in c# when you define your variables in the class level they are called fields. These fields: field and field2 are being used multiple times in most of the methods I have. So instead of having them defined and instantiated inside every method which there are both served the same function or use I placed them outside under class level. Would there be a problem with that? And i used test because thats the only exampled name I could think of at the moment. – rpm07 Feb 25 '15 at 14:42
  • 1
    It is OK to place it under the class level since it is used across the methods. Also it is cleaner since you don't repeat every time the same code in all methods. – pietromenna Feb 25 '15 at 15:56
0

If field and field2 need to persist outside of a particular method call, then they need to be defined at class level, outside of the methods.
Where they happen to get instantiated thereafter doesn't really matter (as long as all of your code accepts that they might not be initialised).

If field and field2 are only ever used inside a particular method, then define and initialise them inside that method.

Scope variables as "tightly" as you can to avoid side-effects and the Perils of Thread Un-Safety.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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