![]() |
|
Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Spoiled by Perlby afoken (Chancellor) |
on Oct 31, 2021 at 01:10 UTC ( [id://11138265]=perlmeditation: print w/replies, xml ) | Need Help?? |
Some context: My initial problem of making sense of a pile of Excel VBA junk (see [OT] Finding similar program code) is largely solved, and I have started re-implementing the calculations in C#. C# is obviously not my first language, neither the second or third one. It's more somewhere around number 20. I've lost count. Ranting about a certain Hunchback-Gollum-Salvatore C derivate caused a misunderstanding and triggered a reply by jdporter: wait - do you mean C#? Because imho C# is a great language. Jdporter is not alone, I've already discussed a lot with my coworker, who really likes C#. The re-implementation is my first C# project, after a tiny "hello world" command line program just to test the VS2019 installation. I have already helped to debug some C# software, but rarely on the code level, more on the algorithm level, and of course some rubber duck debugging like "a < 10 looks wrong if you want to check that a is larger than 10". C# is no huge surprise. It is just another language derived from C and some other languages, with OOP, and with its own set of quirks, strengths, and weaknesses. C# is designed for OOP, and supports OOP much better than some other langauges. OOP is not an afterthought, it is clearly part of the original design. Over the years, some syntatic sugar was added to the existing syntax, allowing to write quite short, but still readable code. But I miss some really trivial things I'm used to in Perl: The <=> and cmp operators together with || In C#, many classes (e.g. List<T>) implement a Sort() method that expects the C# equivalent of a compare function. You end writing something like this for a simple compare:
Well, CompareTo() is not pretty, but still acceptable. I would prefer the (invalid) C# example below, perhaps using operator overloading to call CompareTo() behind the scenes. Unfortunately, C# has no <=> operator that could be overloaded.
Compare to Find:
Compare to sort in Perl:
Things get ugly if you want to sort by more than one property:
In C#, I ended up with that:
You can't use a.Foo.CompareTo(b.Foo) || a.Bar.CompareTo(b.Bar) in C#, because || is strictly boolean. (Yes, I know that LINQ exists and has its own syntax plus extension methods to work with data in a way similar to SQL.) redo Restart a loop block, bypassing the conditional. See redo. It simply does not exist in C#. You have to use goto, which is at least ugly; or use an inner loop, which opens another can of worms (see below). I rarely need redo, but some of the calculations need a redo. In VBA, it was simply implemented via goto jumping backwards over some hundred lines of code. My C# code also uses goto, for the lack of a better loop control. next LABEL, last LABEL, redo LABEL Again, those do not exist in C#. You can not get out of an inner loop AND start the next iteration of an outer loop, exit the outer loop, or restart an outer loop. Again, you have to use goto. If you need all three variants in a single set of two nested loops, you need three labels: One at the start of the outer loop to simulate redo, one at the end of the outer loop to simulate next, and one after the outer loop to simulate last. You could also break out of the inner loop and use a bunch of helper variables, which don't make the code more readable. Again, I rarely need to control an outer loop from an inner loop, but some of the calculations need that, too. Again, VBA used goto and jumped wildly around, and my C# code also uses goto where I could not avoid it. Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Back to
Meditations
|
|