C
C#2mo ago
Bronya

thinking how my code should look like

using System;
using static MyFunction.Function1;

public class Program
{
public static void Main(string[] args)
{
string readFilePath = "CSVReadFilePath";
string writefilePath = "CSVWriteFilePath";

CSVData csvData = new CSVData();
csvData.ReadCSV(readFilePath);

Func1(csvData);
Func2(csvData);
Func3(csvData);
//Func4, 5, 6, 7, 8, 9, and so on

csvData.WriteCSV(writefilePath);
}
}
using System;
using static MyFunction.Function1;

public class Program
{
public static void Main(string[] args)
{
string readFilePath = "CSVReadFilePath";
string writefilePath = "CSVWriteFilePath";

CSVData csvData = new CSVData();
csvData.ReadCSV(readFilePath);

Func1(csvData);
Func2(csvData);
Func3(csvData);
//Func4, 5, 6, 7, 8, 9, and so on

csvData.WriteCSV(writefilePath);
}
}
On other file: (there will be multiple files like this)
using System;

namespace MyFunction
{
public class Func1Field
{
public CSVData csvData { get; set; }
public decimal Field1 { get; set; }
public decimal Field2 { get; set; }
// only properties exist in this class, lots of properties!
}
public static class Function1
{
// this class have no field or property
private static void Method1(ref Func1Field field) {/*code*/}
private static void Method2(ref Func1Field field) {/*code*/}
// only one public method exist in this class
public static void Func1(ref Func1Field field)
{
//code
Method1(field);
//code
Method2(field);
//code
}
}
}
using System;

namespace MyFunction
{
public class Func1Field
{
public CSVData csvData { get; set; }
public decimal Field1 { get; set; }
public decimal Field2 { get; set; }
// only properties exist in this class, lots of properties!
}
public static class Function1
{
// this class have no field or property
private static void Method1(ref Func1Field field) {/*code*/}
private static void Method2(ref Func1Field field) {/*code*/}
// only one public method exist in this class
public static void Func1(ref Func1Field field)
{
//code
Method1(field);
//code
Method2(field);
//code
}
}
}
what do you guys think of this idea? is this a good structure?
23 Replies
Angius
Angius2mo ago
Why are your classes named Function...?
Bronya
Bronya2mo ago
because i use it like function
Angius
Angius2mo ago
Classes cannot act like functions, they're nothing like them
Bronya
Bronya2mo ago
like Func1(csvData); that look like function no?
Angius
Angius2mo ago
Yes, but it's not a class you're using here, is it?
Bronya
Bronya2mo ago
hmm... you are right
Angius
Angius2mo ago
Also, why ref on a reference type? Why split properties from methods? And call the properties container "field"?
Bronya
Bronya2mo ago
so I can do Func1(csvData); instead of csvData = Func1(csvData);
Angius
Angius2mo ago
Reference types are passed as a reference anyway
Bronya
Bronya2mo ago
because it's static, I heard static property and field is bad, especially if I need many of them so without ref I can still do Func1(csvData);?
MODiX
MODiX2mo ago
Angius
REPL Result: Success
class Foo(int bar)
{
public int Bar { get; set; } = bar;
}

static void Blah(Foo b)
{
b.Bar = 69;
}

var f = new Foo(420);
Blah(f);

f.Bar
class Foo(int bar)
{
public int Bar { get; set; } = bar;
}

static void Blah(Foo b)
{
b.Bar = 69;
}

var f = new Foo(420);
Blah(f);

f.Bar
Result: int
69
69
Compile: 406.938ms | Execution: 39.002ms | React with ❌ to remove this embed.
Angius
Angius2mo ago
Static properties, yeah But keep them instance properties And use instance, non-static methods
MODiX
MODiX2mo ago
Angius
REPL Result: Success
class Foo(int bar)
{
public int Bar { get; set; } = bar;
public void Blah()
{
Bar = 69;
}
}

var f = new Foo(420);
f.Blah();

f.Bar
class Foo(int bar)
{
public int Bar { get; set; } = bar;
public void Blah()
{
Bar = 69;
}
}

var f = new Foo(420);
f.Blah();

f.Bar
Result: int
69
69
Compile: 411.399ms | Execution: 59.606ms | React with ❌ to remove this embed.
Angius
Angius2mo ago
See?
Bronya
Bronya2mo ago
so this way better?
Angius
Angius2mo ago
Makes more sense
Bronya
Bronya2mo ago
it's actually what I do before, but feel like unnecessary to create a variable that will only be used once for one method too
Angius
Angius2mo ago
? Not sure what you mean Your solution with a separate class with static methods is the same exact thing except needlessly more code
Bronya
Bronya2mo ago
like I will probably do
var f1 = new Foo1(csvData);
csvData = f1.Blah();
var f2 = new Foo2(csvData);
csvData = f2.Blah();
var f3 = new Foo3(csvData);
csvData = f3.Blah();
var f1 = new Foo1(csvData);
csvData = f1.Blah();
var f2 = new Foo2(csvData);
csvData = f2.Blah();
var f3 = new Foo3(csvData);
csvData = f3.Blah();
hmm... you are right... ok thx, I guess I won't be using static class
Angius
Angius2mo ago
CSVData csvData = new CSVData();
csvData.ReadCSV(readFilePath);

Func1(csvData);
Func2(csvData);
Func3(csvData);
CSVData csvData = new CSVData();
csvData.ReadCSV(readFilePath);

Func1(csvData);
Func2(csvData);
Func3(csvData);
would just turn into
CSVData csvData = new CSVData();
csvData.ReadCSV(readFilePath);

csvData.Func1();
csvData.Func2();
csvData.Func3();
CSVData csvData = new CSVData();
csvData.ReadCSV(readFilePath);

csvData.Func1();
csvData.Func2();
csvData.Func3();
Bronya
Bronya2mo ago
the problem with that is each Func1, Func2, and so on actually very big. so if I make them all inside CSVData it will be very very big big class. which is why I want them to be their own class if possible or something to separate them
Angius
Angius2mo ago
Partial classes?
Bronya
Bronya2mo ago
yeah, that is what I do before. but because each Func use lots of field, and their name sometimes similar, it sometimes confuse me ok so, I'm thinking of making the methods in Function1, Function2, and so on into local function and move it inside Func1, Func2, and so on the properties in Func1Field class will be put inside the Func1 method as local variable since local function can access local variable in the method
Want results from more Discord servers?
Add your server