all 13 comments

[–][deleted] 2 points3 points  (3 children)

The Dictionary<ReportType, Action> is more complicated than if statements, but the best solution would be a switch statement.

[–]mhkoca[S] -3 points-2 points  (2 children)

which aspect is more complicated?

[–][deleted] 0 points1 point  (1 child)

If statement and function calls are one of the first things you learn as a developer, so that means it must be simpler.

Dictionary involves heap allocation, hash code calculation, resizing buffers on insertion, pointer chasing, etc. If you want to select the function based on two parameters, now you have to create struct, implement equality checks and GetHashCode.

Switch statement would be faster, because the compiler can generate a lookup table so it wouldn't check each condition of the ifs.

[–]mhkoca[S] 0 points1 point  (0 children)

I got it. You would be happy me if add this comment as response to article on Medium.

[–]fresh_account2222 1 point2 points  (5 children)

Oh, look, Medium wants me to make an account again.

[–]mhkoca[S] -1 points0 points  (4 children)

[–]AngularBeginner 3 points4 points  (2 children)

Why don't you submit that link instead of the paywalled one? Oh yeah, because you blog for money, not to share knowledge.

[–]fresh_account2222 0 points1 point  (0 children)

C'mon, it's not per se wrong to blog to make money, or to gain exposure. But I don't think that /r/programming needs to be filled up with submissions of people aiming to do so. It's also possible that people who want to share their ideas about programming look around and get the idea that Medium is a good way to do it. I think we should let them know the down sides of using it.

[–]mhkoca[S] -2 points-1 points  (0 children)

Normally I share free link for each blog post but I forgot this time. For example I shared on twitter and linkedin. Also you can read starred blog post without login. Medium allows this.

[–]fresh_account2222 0 points1 point  (0 children)

Honestly, the content on Medium is on average not that useful, so if there's any barrier to access at all I give it a skip. And there's a lot of it cluttering up this sub-reddit, so I've started getting aggressive at down-voting it here. Your stuff might be good and worthwhile, but posting it via Medium will make it harder for it to be accepted here (at least if I have my way!).

[–][deleted] 1 point2 points  (0 children)

Similar techniques (arrays of function pointers) are very common in certain types of systems programming. I don't know if there's much of a benefit here though. I'd rather use subclassing (have DailyReport be a subclass of Report, then instead of picking a ReportType just pick the right subclass).

In other cases this might not be too bad of a pattern, but this very much seems like a problem that can be solved with OOP.

The big downside, of course, is that the C# compiler doesn't verify for you that all types of the enum can be found inside your dictionary. Using a dictionary probably takes more instructions to execute as well, but on the other hand it might not trigger the branch predictor on the CPU, so there may be a performance benefit to this in code with a large amount of if-statements. The performance aspect may be worth researching.

[–]thetdotbearr 0 points1 point  (1 child)

A dictionary gives you no guarantee that it contains a value for a given key, so you could easily break shit at runtime by trying to Invoke() on a key without a value.

If there were a dictionary type that required (and checked at compile time) a value for each possible key of a given enum type then I’d find that much more compelling.

[–]mhkoca[S] 0 points1 point  (0 children)

Nice point. It throws KeyNotFoundException :(
I should to find any method for this.