0

Is it okay to write a class like this:

public class AlexaApi
{
    public string CreateRandomPhrase()
    {
        return "Hello World" //There's more code, but needless for my question
    }
    public object ResponseBuilder(string text)
    {
       //CODE HERE
    }
}

But then when I use the class, call "new" each Time I want to access its functions.

new AlexaApi().ResponseBuilder(new AlexaApi().CreateRandomPhrase());

I understand that this creates two instances of the AlexaApi object and it most likely should be written like:

var alexa = new AlexaApi();
alexa.ResponseBuilder(alexa.CreateRandomPhrase());

Would it use up more memory calling "new" each time? What is the worst thing that can happen? Is it really bad form to do what is written in the first example?

lurker
  • 56,987
  • 9
  • 69
  • 103
user2224583
  • 69
  • 1
  • 16
  • 7
    Those seem to be helper/utility methods. What's the reason for them being instance methods rather than static? – Camilo Terevinto Aug 02 '18 at 14:18
  • 1
    For this example, you should just make them static methods – Rufus L Aug 02 '18 at 14:19
  • In this very particular example there's no 'worse thing' to happen. That doesn't mean that "newing up objects" is free or doesn't cost any resources. It's TOTALLY up to the object and it's implementation on how "costly" a new instance is. For all you know each instance claims a bunch of network connections, a bunch of memory, a bunch of file handles etc. Or a new instance does "nothing" like your example. There is no "one answer fits all" to this question. Having said all that; for this particular code/question I'd go (as already mentioned) with a static method too. – RobIII Aug 02 '18 at 14:19
  • If the two methods are sharing some state from the class, 1st usage will break the functionality. – M. Mennan Kara Aug 02 '18 at 14:20
  • if you have no unmanaged resources...then the GC should take care of it...static methods have a downside that they cannot be overriden from in a child class...keep that in mind if you choose to go that route – Ctznkane525 Aug 02 '18 at 14:22
  • I've add static to the functions and call them directly. Because the class itself was creating a client style object to communicate to Amazon, I though maybe having the functions accessible though the AlexaApi object might be a better practice, then calling the functions directly. There still might be room to change how the class behaves during refactoring. I am thankful for the quick responses. – user2224583 Aug 02 '18 at 14:52

4 Answers4

2

There's a fair chance that what you actually want is a static method:

public class AlexaApi
{
    public static string CreateRandomPhrase()
    {
        return "Hello World" //There's more code, but needless for my question
    }
    public static object ResponseBuilder(string text)
    {
       //CODE HERE
    }
}

you can then call these methods thus:

AlexaApi.CreateRandomPhrase();
..etc.

i.e. you avoid instanciating a new class each time.

But be aware that static methods and classes have slightly different symantics to none static ones. Particuarly

  • You can't store state in the class
  • There is only ever one instance of the method in memory (this could be memory efficient but it can also lead to memory leaks if your not careful)
  • static objects can be an issue in unit testing as they cannot be injected in

You don't show details for what this class does so it's hard to be explicit. If you methods are just "logic", then there is no harm in making them static. If they load resources, etc. then don't.

For more info see Static vs non-static class members

Also note a class that only contains static methods can itself be made static. This just prevents you declaring none static methods/properties

public static class AlexaApi
{
    public static string CreateRandomPhrase()
    {
        return "Hello World" //There's more code, but needless for my question
    }
    public static object ResponseBuilder(string text)
    {
       //CODE HERE
    }
}
Liam
  • 27,717
  • 28
  • 128
  • 190
  • Upvoting for well sourced information, but I'd edit to make sure you directly address the question asked as well as providing an alternative solution. – Aaron Aug 02 '18 at 15:05
0

To do all that without creating multiple instances you should static methods as has been suggested. Consider the following code:

public class AlexaApi
{
    public static string CreateRandomPhrase()
    {
        return "Hello World" //There's more code, but needless for my question
    }
    public static object ResponseBuilder(string text)
    {
       //CODE HERE
    }
}

This way you can get the same results using code such as:

AlexaApi.ResponseBuilder(AlexaApi.CreateRandomPhrase());
Fabulous
  • 2,393
  • 2
  • 20
  • 27
0

keyword static should fit your case:

public class AlexaApi
{
    public static string CreateRandomPhrase()
    {
        return "Hello World" //There's more code, but needless for my question
    }
    public static object ResponseBuilder(string text)
    {
       //CODE HERE
    }
}

and call directly the method without creating instance :

AlexaApi.ResponseBuilder(AlexaApi.CreateRandomPhrase());
Antoine V
  • 6,998
  • 2
  • 11
  • 34
0

As you mentioned in your question, using new instances of the class each time will take up more memory. Depending on the scope where you're calling this method and the size of the class (how many instance variables, not lines of code) this practically could amount to almost no extra memory (ram storing the extra instance of the class) or CPU time (creating the class with any default values/constructors you have in place) to eating up more memory than the rest of your program combined.

As mentioned in the comments and an answer that was posted while I was typing this, you could always make those methods static, which means they can be called without using instances of the class at all and will save you the trouble.

Aaron
  • 146
  • 8