almaz
  • Member for 9 years, 2 months
  • Last seen more than a month ago
  • Kiev, Ukraine
BOOL×BOOL→ENUM mapping
29 votes

I think ? operator will make your code more compact and readable: return isCheck ? isStuck ? GameState.Mate : GameState.Check : isStuck ? GameState.Stalemate : GameState.Ok

View answer
Complex if else
Accepted answer
18 votes

In order to simplify nesting I reordered conditions so that you can deal with simplest cases first, and return result as soon as you know it instead of capturing it in days variable. Also ternary ...

View answer
Simplify C# Code
Accepted answer
12 votes

A simpler version of your code (will work in all .NET versions down to 2.0) is the following: public bool IsAccept { get { return string.Equals("accept", Status, StringComparison....

View answer
Unit testing with dependency injection and MOQ
11 votes

There are a number of issues in your code. Naming. Methods should use verb phrases to denote the action they perform and try to be specific. So your DateList method should become something like ...

View answer
Is this the proper way to find the subclass of a polymorphic superclass?
11 votes

You have to learn a lot about OOP... There is no polymorphism in your code, as this principle is designed to tackle exactly the kind of code you've written. The code that uses Asset should never ...

View answer
C# Async Socket Server with Events
Accepted answer
11 votes

There are a number of naming issues here so it's quite hard to understand the code, I'll start from those: public properties/events/methods should be named PascalCase (receiveMessageHandler => ...

View answer
Onion Architecture
Accepted answer
10 votes

Let me know if I went wrong somewhere. This is the first time I set up an application with a proper architecture. I'm afraid there is no such thing as general "proper" architecture. Relevant ...

View answer
Playing card game method
10 votes

We can use the fact that in the ordered sequence of cards the cards that match your requirement will be located at the beginning of sequence, and their rank will match their position: var count = ...

View answer
How can I make this code (that is usually in a loop) faster and efficiently?
Accepted answer
10 votes

Cache the instance of Graphics and Font objects so that you don't need to create them each time you measure the length of string. I've created a small test to see which part takes most of the time: ...

View answer
Item-level locks for a large number of items
9 votes

I don't see memory leaks here, as well as cases where lock is not applied. From design point of view I improve it a little bit. Currently you require customers to get the item lock and then lock on ...

View answer
Tokenizing each document in a large document of documents
9 votes

Tokenizer method: You use ref incorrectly, all reference types are always passed by reference, please read the MSDN or any other online resource to understand the ref Your code doesn't follow naming ...

View answer
Collection that can be modified in a foreach
Accepted answer
8 votes

Your logic has a serious flaw: you expect that the consumer of the collection will iterate it till the end. In reality there are break or return keywords, and exceptions that will not follow your ...

View answer
Correct way to delete elements from a ConcurrentDictionary with a predicate
Accepted answer
8 votes

I know it's not a direct answer to your question, but I'm trying to get to the roots of the problem rather than giving correct answer to issue caused by potential misuse... In your code what you are ...

View answer
How to make static methods testable?
Accepted answer
8 votes

If you want to write tests for this helper class only - they would be integration tests since they touch the file system. In order to make the code unit testable you need to make it independent from ...

View answer
Correct approach to wait for multiple async methods to complete
Accepted answer
8 votes

Your code is absolutely correct in case when you want to start workflows only when all of them are configured. But if you want to start each workflow once it's configured (independently from other ...

View answer
Best way convert byte array to hex string
Accepted answer
8 votes

There are popular topics on StackOverflow that cover this exact question: How do you convert Byte Array to Hexadecimal String, and vice versa? byte[] to hex string

View answer
Global data singleton
7 votes

Singletons are evil Even if you do want to use it, use Lazy<T> instead of manually doing double-checked locking Best solution would be to leverage IoC container to inject dependencies you need.

View answer
Am I using async C# correctly?
Accepted answer
7 votes

You are using async/await absolutely correctly, that's how they were supposed to be used. Please note that from this code it's hard to tell whether the method will return the Task immediately, since ...

View answer
Optimizing a distinction code
Accepted answer
7 votes

Try types = myTableList .Select(x => (int)x.item_type) .Distinct(); You may also want to add .ToList() at the end if you want to match original code

View answer
Collection of Actions
Accepted answer
7 votes

Starting from minor issues - please follow naming conventions (variables should be camelCased and methods PascalCased, do add access modifiers, give variables meaningful names). Your design breaks ...

View answer
Custom dictionary for caching assistance
7 votes

Your code is not safe: simultaneous calls to getter of the indexer and setter for the same key may result in data missing in cache: setter adds the key ...

View answer
Notifying the UI that Issues Were Found
Accepted answer
6 votes

You are still using the UI thread to do the project parsing _parser.Parse(project), because Task.Yield will return the execution back to UI thread. If this call may take significant time, I would ...

View answer
Reverse linked list using recursion
6 votes

Do use descriptive name for the variables and parameters, all those n, temp, newNode do not tell anything. Reversing the list using recursion should not rely on 'external' variables as you do with ...

View answer
Refactor Linq Expression
Accepted answer
6 votes

Note that your code will return null in case if the sort order is not defined. I've preserved this logic, but you may want to change it to return original query (and thus change return type to ...

View answer
convert timespan to readable text
Accepted answer
6 votes

ToPrettyFormat method can definitely be improved. Instead writing the conditional statement for all possible permutations of HasDays, HasHours and HasMinutes it's better to step back and define rules ...

View answer
Unit-testing the importing of data into a database
Accepted answer
6 votes

The points you've specified in your question actually show that you (almost) see issues in your code yourselves, so thumbs up for that. I'll only expand on your points plus add a couple: your class ...

View answer
Making async SQL calls
Accepted answer
6 votes

Your code does not follow naming conventions (private fields should be camelCased, and usually have an underscore prefix; parameters should be camelCased) there is no need in private properties, use ...

View answer
Expression design
5 votes

You have a bug in your code: Simplify() can return a new instance, and you ignore this instance when calling Left.Simplify() and Right.Simplify(). Also, I do not see the reason to iterate on ...

View answer
Using LINQ or Lambda instead of nested and multiple foreach statements
5 votes

What you are actually doing is updating the initialList with objects from secondList based on matching Value. The optimal solution would have an O(N+M) comparisons (N and M - number of elements in ...

View answer
Should I use and loop, or just a loop?
Accepted answer
5 votes

Use .Where extension method to filter the records you need: foreach(ModelClass item in listObj.Where(x => x.id < 0)) { // code to create a new Obj in the database }

View answer
1
2 3 4 5