Skip to main content
Share Your Experience: Take the 2024 Developer Survey
31 votes
Accepted

A Failable<T> that allows safe returning of exceptions

First: congratulations, you have rediscovered the error monad. https://hackage.haskell.org/package/mtl-2.2.1/docs/Control-Monad-Error.html Second: as noted in the comments, C# already has the ...
Eric Lippert's user avatar
  • 14.9k
8 votes

A Failable<T> that allows safe returning of exceptions

A few minor things: You do not check for null in GetHashCode() and in equality operators. You probably should, since both ...
Nikita B's user avatar
  • 13k
7 votes
Accepted

State Pattern for traffic lights

Design This sort of design isn't necessarily a bad thing, but in this example it's making things a lot more complicated, without providing any benefits: ...
Pieter Witvoet's user avatar
7 votes

A Failable<T> that allows safe returning of exceptions

For the sake of a consistent and discoverable API you should have a non-generic version of Failable. For Consistency: If I'm presented with two methods that can ...
Mr.Mindor's user avatar
  • 346
6 votes

Section 2: Classes - Design a StackOverflow Post

I don't know which version of C# you use, but as of C# 6 it's possible to set initial values on property definition, which has 2 advantages: Can eyeball the initial values quickly by looking at the ...
potato's user avatar
  • 1,122
6 votes

Date range generator

.NET 6 DateOnly Much of what you are doing would be greatly simplified if you were using DateOnly that came out with .NET 6. So many nagging issues would fall by ...
Rick Davin's user avatar
  • 6,350
5 votes

Evaluating Parser State

I don't know whether I've missed a nuance of this, but if you only consider the distinct states in the system the code gets a lot easier. This is typed into my browser and unchecked but I think it ...
RobH's user avatar
  • 17k
5 votes
Accepted

Evaluating Parser State

If I understand the rules correctly, the last rule "Otherwise return the state of the most advanced non-ready module." isn't met by your method. Having states ...
Heslacher's user avatar
  • 50.4k
5 votes
Accepted

Toggle between two options in an object?

You currently only print the currently selected mode to the console, but you probably will use this mode for other purposes, such as the business logic of your application. To that end, I suggest ...
gitcdn's user avatar
  • 431
5 votes

Scalable design of a system with multiple state machines, where valid state transitions depend on the state of other machines

This doesn't have to be complicated. Definitely avoid Approach 1 - having a dedicated class for state combinations is not a good idea. Follow vaguely Approach 2, but Do not have a ...
Reinderien's user avatar
  • 62.1k
4 votes

State Pattern for traffic lights

Pieter's answer already covers most of the important things, I just want to add a small bit (that's why it's a community wiki instead of a comment) about the logic behind a state machine for a complex ...
4 votes
Accepted

Vending Machine implementing State Pattern

VendingMachine internals should not be public This fundamentally subverts the idea of a state machine; that the vending machine's internal state is changed by a ...
radarbob's user avatar
  • 7,599
3 votes
Accepted

Cooking steak with State Pattern and without IFs

I have to dissapoint you because this is not a state-pattern what you have written. This is some strange hybrid that I don't know what to call. When you take a look at the State Design Pattern then ...
t3chb0t's user avatar
  • 44.3k
3 votes

Cooking steak with State Pattern and without IFs

You are correct that your current solution is over engineered. This is because you define one class per object of your program. Several of these classes behave exactly the same. Therefore their ...
Roland Illig's user avatar
  • 21.3k
3 votes

Cooking steak with State Pattern and without IFs

I'm not sure that eliminating if statements is a worthwhile exercise. _steakRules.First(rule => rule.Predicate(temp)) is ...
200_success's user avatar
3 votes
Accepted

Persisting Variables Over A State Loss

The dictSessionVars is initialized together with the default instance of the class: ...
Mathieu Guindon's user avatar
3 votes

State Pattern for traffic lights

First, I see no reason to make the setter of TrafficLight.State public, as there's no reason the client should be able to change it directly. Second, your solution ...
Arnon Axelrod's user avatar
3 votes
Accepted

getDerivedStateFromProps

Ted's answer is quite readable to me. You could use some to detect if you need to submit changes as that is semantically closer to what you are doing: ...
ndp's user avatar
  • 2,371
3 votes
Accepted

Capture some state in Haskell

What you have will work just fine: ...
ErikR's user avatar
  • 1,386
3 votes

Section 2: Classes - Design a StackOverflow Post

There is nothing to add more than @potato's answer. However, I just want to re-enforced the answer. The naming convention for TimeDateCreated can be changed to <...
iSR5's user avatar
  • 5,278
2 votes
Accepted

Logon state management

Scratching the tip of the Approach 2 iceberg... ...
t3chb0t's user avatar
  • 44.3k
2 votes
Accepted

Toggle between two states in react

It's strange that you would call this.componentDidMount. I have not seen this before. Either way, what if you created an indicator in your state that declared what ...
Adam Recvlohe's user avatar
2 votes

getDerivedStateFromProps

Here is one possibility: ...
Ted's user avatar
  • 306
2 votes

Haskell - Caching third party API token across requests

I think the type of apidefTokenRef unnecessarily exposes internal details. Perhaps it would better to parameterize APIDefaults ...
Daniel Díaz Carrete's user avatar
2 votes

Persisting Variables Over A State Loss

My tests indicate that adding an instance of TTestClass to the AppSessionVariables Dictionary does not survive a state loss. ...
TinMan's user avatar
  • 4,133
2 votes

Simpler state manament alternative to Redux, MobX, Context API, etc

After looking at the implementation, this is not an implementation of global state management. The entire point of things like redux is so you can directly connect components to a global store. In ...
Andrew's user avatar
  • 319
2 votes

N rotations of an array, either left or right, and then printing the result

There are two basic ways to solve this type of problem. One is having to do the rotation in place, where you can't allocate additional storage for a copy of the array. This uses less memory but ...
1201ProgramAlarm's user avatar
2 votes

Section 2: Classes - Design a StackOverflow Post

You have a couple of good answers. Since you are an experienced developer who is new to C#, I will address some other things. Things you do quite well Braces and indentation Naming (most of the ...
Rick Davin's user avatar
  • 6,350
2 votes
Accepted

State monad and pattern matching on data constructors

In general, you would extract the state using get and then case-match on it, like so: ...
K. A. Buhr's user avatar
2 votes
Accepted

Unit test for a React click-counting component

Use const instead of let let h2 = screen.getByRole('heading', { level: 2 }); Good that you'...
ggorlen's user avatar
  • 3,540

Only top scored, non community-wiki answers of a minimum length are eligible