Return this and make repeated calls vs Return void and make calls seperately?

Viewed 24

Im reorganizing my code and have come across a few questions. Here is an example of how I currently have my code. All the Utility class have similar functions, but just hold a lot of methods.

public class Client
{
    public object Data;
    public void Action1(object _something)
    {
        Utility_Red _utility=new Utility_Red(this);
        _utility.Prepare1().BoolStuff();
        _utility.Prepare1().IntStuff();
        _utility.Prepare2().BoolStuff();
        _utility.Prepare2().StringStuff();
    }
}
public class Utility_Red
{
    private bool CanCall=false;
    private bool Reading=false;
    private Client Attached;
    public Utility_Red(Client _attached)
    {
        Attached=_attached;
    }
    public void BoolStuff()
    {
        if(!CanCall)
        {
            return;
        }
        if(Reading){}          // do stuff
        else{}                 // do different stuff
    }
    public void IntStuff()
    {
        if(!CanCall)
        {
            return;
        }
        if(Reading){}          // do stuff
        else{}                 // do different stuff
    }
    public void StringStuff()
    {
        if(!CanCall)
        {
            return;
        }
        if(Reading){}          // do stuff
        else{}                 // do different stuff
    }
    public Utility Read()
    {
        CanCall=true;
        Reading=true;
        return this;
    }
    public Utility Write()
    {
        CanCall=true;
        Reading=false;
        return this;}
    }
public class Utility_Blue{}
... more utility classes

Return this vs Return void

Before calling any of the methods in a utility class I call Read() or Write() to determine what action to take in the next method. Is this bad for any reason? Would it be better to make calls on seperate lines like this? This just seems harder to read.

public class Utility_Red
{
    ...
    public void Read()
    {
        CanCall=true;
        Reading=true;
    }
    public void Write()
    {
        CanCall=true;
        Reading=false;
    }
}
public class Client
{
    ...
    public void Action1(object _something)
    {
       _utility.Read();
       _utility.BoolStuff();
       _utility.Read();
       _utility.IntStuff();
       _utility.Write();
       _utility.BoolStuff();
       _utility.Write();
       _utility.StringStuff();
    }

}

Store object as field or pass as parameter

The Utility classes have a Client field that is the Client it is being used in(because thats the only time the Utility classes are used). I switched it to this so that I didn't have to keep passing the Client as a parameter everytime I called a method in the Utility class. Is it better to have the Client object passed as a parameter in every method?

public class Client
{
    public object Data;
    public void Action1(object _something)
    {
        Utility_Red _utility=new Utility_Red();
        _utility.Read().BoolStuff(this);
        _utility.Read().IntStuff(this);
        _utility.Write().BoolStuff(this);
        _utility.Write().StringStuff(this);
    }
}
public class Utility_Red
{
    private bool CanCall=false;
    private bool Reading=false;
    public Utility_Red(){}
    public void BoolStuff(Client _attached){...}
    public void IntStuff(Client _attached){...}
    public void StringStuff(Client _attached){...}
    ...
}

Seperate Methods by Read vs Write

Is having a Read() and a Write() method that act like a switch bad? Should (1)I have seperate methods for each Stuff() method? or Should (2)I just pass the Read/Write as a bool into the Stuff() methods? If I stay with the current setup of having a preparing method then calling another or use pass a true/false instead, it seems like the class is not filled up with more classes, while (2) has the class filled with even more methods, but they dont have to check if they are Read vs Write and dont have to check if a preparing method was called. Does this have any kind of effect on the class at all? can having more methods take up more memory? can having 2 if statements in each method cause it to run slower?(my understanding is that they only branch when evaluated to false, and also are pretty much negligble in slowing code down)

public class Client
{
    public object Data;
    public void Action1(object _something)
    {
        Utility_Red _utility=new Utility_Red();
        _utility.BoolStuff(true,this);
        _utility.IntStuff(true,this);
        _utility.BoolStuff(false,this);
        _utility.StringStuff(false,this);
    }
}
public class Utility_Red
{
    ...
    public void BoolStuff(bool _read,Client _attached){...}
    public void IntStuff(bool _read,Client _attached){...}
    public void StringStuff(bool _read,Client _attached){...}
}

vs

public class Client
{
    public object Data;
    public void Action1(object _something)
    {
        Utility_Red _utility=new Utility_Red();
        _utility.Read_BoolStuff(this);
        _utility.Read_IntStuff(this);
        _utility.Write_BoolStuff(this);
        _utility.Write_StringStuff(this);
    }
}
public class Utility_Red
{
    ...
    public void Read_BoolStuff(Client _attached){...}
    public void Read_IntStuff(Client _attached){...}
    public void Read_StringStuff(Client _attached){...}
    public void Write_BoolStuff(Client _attached){...}
    public void Write_IntStuff(Client _attached){...}
    public void Write_StringStuff(Client _attached){...}
}

Methods in Utility or in Client

If having a bunch of methods in a class doesn't cause it to take up more space(each Utility class has between 10 to 20 methods as is), would it be better to just put all these methods from each Utility(currently 3 Utility classes, but have more that would need to be built out) class into the Client? Then I don't have to pass the Client as a parameter or store it in Utility, and don't have to create the Utility class to use it in/with the Client

public class Client
{
    public object Data;
    public void Action1(object _something)
    {
        Red_Read_BoolStuff();
        Red_Read_IntStuff();
        Red_Write_BoolStuff();
        Red_Write_StringStuff();
    }
    void Red_Read_BoolStuff(){}
    void Red_Read_IntStuff(){}
    void Red_Read_StringStuff(){}
    void Red_Write_BoolStuff(){}
    void Red_Write_IntStuff(){}
    void Red_Write_StringStuff(){}
    void Blue_ ...
    void Green_ ...
}

The purpose of the Client class is to read/write to a network stream, and contains a Socket. The methods read and write custom packets to the network stream and depending on which utility will store/pull different data either in the client, or on the local system. One of the Utility classes is for connecting to a listening socket, determining connection parameters, exchanging keys, closing connection, altering the other endpoint's configuration. Another is for sending different types of log data,system update versions, system errors. Another for sending service status,scheduledtask last run time and whether if failed or not, how many remote connections to database, database replication information. I'm working in .NET Framework 4.7.2

0 Answers
Related