C
C#4d ago
workani

✅ Do I always need to avoid coupling?

For example, I have a class Application where the main part of my project's logic is written, and I also have a GenerateVocabulary object which will itself use an Interface to actually generate content. Will it be good practice to simply create this object inside my Application class? And in general, is it acceptable in a professional context to create one object inside another if I'm 100% sure that I won't change it?
24 Replies
Jimmacle
Jimmacle4d ago
if you don't expect to benefit from decoupling, then don't do it it just makes the code more noisy and harder to understand otherwise
workani
workani4d ago
Thank you! After watching Bob Tabor's course about oop I started to decouple everything
workani
workani4d ago
Also, is it okay to use TLS varaibles like those?
No description
Jimmacle
Jimmacle4d ago
TLS as in top level statements? the visibility doesn't matter there, all that code gets shoved into a Main method so they're locals
workani
workani4d ago
Okay, is it okay to use locals?
Jimmacle
Jimmacle4d ago
you can't write useful code without them local variables are just variables declared inside a method
workani
workani4d ago
No, I think they're TLS
No description
Jimmacle
Jimmacle4d ago
when you say TLS, what do you mean
workani
workani4d ago
Variables that located in object outside methods
Jimmacle
Jimmacle4d ago
those are called fields
workani
workani4d ago
Ah, then what is TLS?
Jimmacle
Jimmacle4d ago
$tls
MODiX
MODiX4d ago
$toplevelcode
Jimmacle
Jimmacle4d ago
$toplevelcode
MODiX
MODiX4d ago
Traditionally in C#, the main entry point of any program would be a static Main method (see $mains):
public static class Program
{
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}
public static class Program
{
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}
In C# 9, a feature called top-level statements was added which allows the static Main method to be omitted in favor of brevity and simplicity, and in C# 10, this was made part of the default console application template:
Console.WriteLine("Hello, world!");
Console.WriteLine("Hello, world!");
If you are following a tutorial or example code and your default template looks different, you can still write the exact same code as if you were writing it inside the static Main method. In addition, you can still write static methods inside top-level code. You can however not use top-level code in more than one file. If you wish to use the old traditional entry point, you can still use a Program class with a static Main method. https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements
Top-level statements - programs without Main methods - C#
Learn about top-level statements. You can create programs without the ceremony of a Program class and a Main method.
workani
workani4d ago
Ah, okay So, is it okay to use fields like that?
Jimmacle
Jimmacle4d ago
yes
workani
workani4d ago
How important is it to carefully choose names for variables and methods?
Jimmacle
Jimmacle4d ago
it's important because you should write code in a way that people can read it, because the computer doesn't care what it looks like which means making it easy to understand like with meaningful names for things
workani
workani4d ago
Okay. Is using more than two words in variable name acceptable? For example, here is my two varaibels ExistingVocabulary and ExistingVocabularyPath
Jimmacle
Jimmacle4d ago
yes, the goal is to make it easy for people to understand trying to set arbitrary limits goes against that
workani
workani4d ago
May I contact you via dm if'll have questions like that? And hank you again for help. I wish you good evening.
Jimmacle
Jimmacle4d ago
just make new help posts, it will be much easier to get help that way i don't typically respond to dms from here
workani
workani4d ago
Okay
Want results from more Discord servers?
Add your server