C
C#3mo 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
Jimmacle3mo 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
workaniOP3mo ago
Thank you! After watching Bob Tabor's course about oop I started to decouple everything
workani
workaniOP3mo ago
Also, is it okay to use TLS varaibles like those?
No description
Jimmacle
Jimmacle3mo 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
workaniOP3mo ago
Okay, is it okay to use locals?
Jimmacle
Jimmacle3mo ago
you can't write useful code without them local variables are just variables declared inside a method
workani
workaniOP3mo ago
No, I think they're TLS
No description
Jimmacle
Jimmacle3mo ago
when you say TLS, what do you mean
workani
workaniOP3mo ago
Variables that located in object outside methods
Jimmacle
Jimmacle3mo ago
those are called fields
workani
workaniOP3mo ago
Ah, then what is TLS?
Jimmacle
Jimmacle3mo ago
$tls
MODiX
MODiX3mo ago
$toplevelcode
Jimmacle
Jimmacle3mo ago
$toplevelcode
MODiX
MODiX3mo 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
workaniOP3mo ago
Ah, okay So, is it okay to use fields like that?
Jimmacle
Jimmacle3mo ago
yes
workani
workaniOP3mo ago
How important is it to carefully choose names for variables and methods?
Jimmacle
Jimmacle3mo 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
workaniOP3mo ago
Okay. Is using more than two words in variable name acceptable? For example, here is my two varaibels ExistingVocabulary and ExistingVocabularyPath
Jimmacle
Jimmacle3mo ago
yes, the goal is to make it easy for people to understand trying to set arbitrary limits goes against that
workani
workaniOP3mo 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
Jimmacle3mo 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
workaniOP3mo ago
Okay
Want results from more Discord servers?
Add your server