Esa
Esa
CC#
Created by steven preadly on 7/4/2024 in #help
βœ… how can i improve this code ?
@steven preadly make sure you check out this before it gets closed. πŸ™‚ also ty @Angius , it was made with the current constraints in mind, but that was a good point. can fix that.
78 replies
CC#
Created by steven preadly on 7/4/2024 in #help
βœ… how can i improve this code ?
The main loop happens in line 5-25, everything below that is just methods
78 replies
CC#
Created by steven preadly on 7/4/2024 in #help
βœ… how can i improve this code ?
Allright so I mocked up something here: https://github.com/EivindAntonsen/RefactoringExample/blob/refactoring-example/ConsoleApp1/Program.cs This branch is a refactored version of the code you pasted up above. There's probably a lot of different ways to do this, and many ways this can get over-complicated, but this is how I'd think about solving this. * Split classes into their own files. * Changed Company from being an instanced class to a static one. * Kept Employee as a class (like you had it initially, and not a record as I suggested a bit hastily). * Changed some of the do-while loops into while(true) loops with break conditions inside instead. * Removed the accessors in your Company class in favor of more explicit methods with accurate names, to more clearly express the intent of the methods. * Improved error messages for bad user input. Just ask if any of this stuff is unclear. And to anybody else watching, let me know if you disagree with where I've taken the code for any reason, I'm also interested in learning if anybody thinks I've made some poor choices.
78 replies
CC#
Created by steven preadly on 7/4/2024 in #help
βœ… how can i improve this code ?
Didn't finish it, will do it tomorrow.
78 replies
CC#
Created by steven preadly on 7/4/2024 in #help
βœ… how can i improve this code ?
It's readable πŸ™‚ You did a good job. This is just to showcase the next few tips and changes I would make.
78 replies
CC#
Created by steven preadly on 7/4/2024 in #help
βœ… how can i improve this code ?
I'll post an example in a bit on github for you showcasing how I would refactor it
78 replies
CC#
Created by steven preadly on 7/4/2024 in #help
βœ… how can i improve this code ?
And "record" instead of class, and "init" instead of set πŸ™‚
78 replies
CC#
Created by steven preadly on 7/4/2024 in #help
βœ… how can i improve this code ?
It dependsβ„’. Performance can be measured in several ways, personally I consider readability a performance index, since that directly affects how long it'll take me to understand and modify a codebase - and therefore it affects developer output. Then there's stuff like cyclomatic complexity (how many different branches are there, such as if-statements, switches, do-while loops etc) as well as time complexity which you mentioned. If you do not agree that readability is a performance index, then you may honestly make the argument that the code you shared is fine as-is.
78 replies
CC#
Created by steven preadly on 7/4/2024 in #help
βœ… how can i improve this code ?
Either channel works fine I think, there's generally a lot to tackle here. I did give you some advice earlier which is still applicable for this block of code.
78 replies
CC#
Created by Alex on 6/25/2024 in #help
βœ… How to get Options from DI
The next immediate question is "but where does it get the values from?" to which the answer is go read the MSFT docs, it'll explain it all in detail πŸ™‚
14 replies
CC#
Created by Alex on 6/25/2024 in #help
βœ… How to get Options from DI
services.AddOption<SpotifyConfig>()
.BindConfiguration("Spotify")
.Validate(config => !string.IsNullOrWhitespace(config.ApiSecret), "Api secret must be defined!");
services.AddOption<SpotifyConfig>()
.BindConfiguration("Spotify")
.Validate(config => !string.IsNullOrWhitespace(config.ApiSecret), "Api secret must be defined!");
This is an example of how to bind a config class and register it as an Option in your app. When you then create a class and add IOptions<SpotifyConfig> to its parameter list, the DI container will inject that for you.
14 replies
CC#
Created by Alex on 6/25/2024 in #help
βœ… How to get Options from DI
This all assumes you want to bind the options to some values in your configuration. As we mentioned above, this topic does necessitate some understanding so reading the MSFT docs here truly is your best bet.
14 replies
CC#
Created by Alex on 6/25/2024 in #help
βœ… How to get Options from DI
I don't know what .AddGoogle() does or where it comes from. But unless it calls .AddOptions<T> and then either
.Configure<IConfiguration>((options, configuration) => /* configure shit */ )
.Configure<IConfiguration>((options, configuration) => /* configure shit */ )
or
.BindConfiguration()
.BindConfiguration()
Then the options haven't been configured.
14 replies
CC#
Created by Alex on 6/25/2024 in #help
βœ… How to get Options from DI
The MSFT docs for the IOptions<T> pattern are really good, and will answer the general question "How do I use/obtain IOptions<T> from DI?" sufficiently. If your question is more about the google credentials specifically, that's an appropriate question for a thread like this imo.
14 replies
CC#
Created by Abdesol on 6/20/2024 in #help
cancellation token to stop Console.ReadLine is only working on the first try
I'm going to have to go for lunch now, but this is the approach I made:
// Setup the cancellation delay
CancellationTokenSource cancellationSource = new();
TimeSpan timeout = TimeSpan.FromSeconds(5);
// The token will be cancelled after the specified timeout
cancellationSource.CancelAfter(timeout);

// Create a task that fails when timeout is reached
TaskCompletionSource<bool> completionSource = new TaskCompletionSource<bool>();
cancellationSource.Token.Register(obj =>
(obj as TaskCompletionSource<bool>)?.SetCanceled(), completionSource);
Task<bool> timeoutTask = completionSource.Task;

// Await the user input or the timeout task to complete
string? userLine = string.Empty;
Task<string?> readTask = Task.Run(() => userLine = Console.ReadLine());
Task finishedTask = await Task.WhenAny(readTask, timeoutTask);

// print the result
Console.WriteLine(finishedTask == timeoutTask ? "Too slow!" : $"Your line: \"{userLine}\"");
// Setup the cancellation delay
CancellationTokenSource cancellationSource = new();
TimeSpan timeout = TimeSpan.FromSeconds(5);
// The token will be cancelled after the specified timeout
cancellationSource.CancelAfter(timeout);

// Create a task that fails when timeout is reached
TaskCompletionSource<bool> completionSource = new TaskCompletionSource<bool>();
cancellationSource.Token.Register(obj =>
(obj as TaskCompletionSource<bool>)?.SetCanceled(), completionSource);
Task<bool> timeoutTask = completionSource.Task;

// Await the user input or the timeout task to complete
string? userLine = string.Empty;
Task<string?> readTask = Task.Run(() => userLine = Console.ReadLine());
Task finishedTask = await Task.WhenAny(readTask, timeoutTask);

// print the result
Console.WriteLine(finishedTask == timeoutTask ? "Too slow!" : $"Your line: \"{userLine}\"");
93 replies
CC#
Created by Abdesol on 6/20/2024 in #help
cancellation token to stop Console.ReadLine is only working on the first try
Let's share
93 replies
CC#
Created by Abdesol on 6/20/2024 in #help
cancellation token to stop Console.ReadLine is only working on the first try
So is mine, but honestly that's just an input source that can be changed
93 replies
CC#
Created by Abdesol on 6/20/2024 in #help
cancellation token to stop Console.ReadLine is only working on the first try
Mine, which has comments and newlines, is 19 lines
93 replies
CC#
Created by Abdesol on 6/20/2024 in #help
cancellation token to stop Console.ReadLine is only working on the first try
If it is less than like 30 lines, you can just do it in here tbh
93 replies
CC#
Created by Abdesol on 6/20/2024 in #help
cancellation token to stop Console.ReadLine is only working on the first try
We tend to want to help you figure the goal instead of providing the entire solution. Reason being you don't really learn much if the solution is just handed to you
93 replies