Switching to C# and curious if there's anything I could improve on in this very basic math script.
10 Replies
Depends what you want to do...
However, I would assign the results of the methods to variables.
You could also create an Enum for the "CalculationMethods".
What's your level of programming? You could take this to the next level ... I would consider the SOLID principles as well as other principles. That means I would try separate the responsibilities the best way I could.. try to put together what's common and only specify what's different, finding a good "mechanism" to be opened for alterations ... for example
yeah you could improve this essentially ad infinitum
Depends what you mean.
Can more functionality be added? Sure.
Can the code be shortened? Also yes.
Could use a switch expression for example
Expression-bodied methods
Top-level statements
And so on, and so forth
Thanks for your reply, I think what you've provided is what I was looking for, a direction to start with to improve my usage of C# and the various paradigms/design patterns. I have 3 years of experience writing in Python and have an undergrad in CS. In general I'd say I'm a novice/beginner nearing intermediate programmer.
For the specific script in question I was hoping for general critique which you've provided nicely.
Examples are more helpful than vague and absolute statements like this FWIW. I've provided additional context if that helps.
"could this be improved?"
"yes"
that's kinda what i could tell you with the informations i had; as Z said it depends, do you want to keep the code simple? do you have any other constraint? do you want more features or more abstractions? you could add ui (whether textual or graphical), you could have history of the operations and undo/redo, you could have api, you could have more operation types, you can have translation, you could have expression parsing, and so on
for example for sure 1st thing i would do is
- removing
static
- using sealed
- adding parameters checksThanks for your time I appreciate it. It will take a bit for me to consider everything everybody has said, it's difficult to chat between work and home life.
do you want to keep the code simple? do you have any other constaint?...My apologies, I really did provide horrific context didn't I. The only goal of this exact "project" is to explore the different areas of C# that Python doesn't offer and felt that some more experienced programmers than myself would shed some light into what parts of the language they'd recommend. I don't intend to make use of anything I'm doing beyond pure enjoyment for what it's worth. My only prior programming experience is Python and some very basic C in university, but I am more interested in learning as a personal interest but will be working with lots of math and problems involving things like Bayes algorithm and the explore/exploit dilemma and other similar problems. To be specific on 3 points I'd choose: 1) What ways could the way I am writing script right now come to be problematic in the future? 2) How would someone in a more experienced situation approach the methods and classes and why would they do it that way? 3) What resources would be better than the Microsoft C# docs for reference if any? I'm off for the night, but I will read and reply as soon as I can.
if you want to work with math and numbers then learning about the various interfaces implemented by int and other primitive types could be useful
3) What resources would be better than the Microsoft C# docs for reference if any?for math? probably tanner in the allow-unsafe-blocks channel 😆 1) What ways could the way I am writing script right now come to be problematic in the future? it's a bit early for this, depends what the project should become/do
I'll look into all of your suggestions, and fair point for #1. Is there anything else you would add before I move on from this thread?
i feel like stuff i would say at this point would be akin to premature optimization
I can appreciate that. Take care thanks again for your time.