How split a big method to smaller several methods in C# [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How can I optimally split one big, difficult method to several smaller methods in C#? Is there any perception or functionality for this issue?

Assuming you are using Visual Studio to edit your C# code, there is built-in functionality for what you are trying to do: Extract Method Refactoring. Note that this is still a manual process - there is no automatic tool which knows how to take your entire method apart.
What to keep in mind while refactoring? Taken from Robert C. Martin's Clean Code:
The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that.
Functions should do one thing. They should do it well. They should do it only.
We want the code to read like a top-down narrative.
Use descriptive names.
The ideal number of arguments for a function is
zero (niladic). Next comes one (monadic), followed
closely by two (dyadic). Three arguments (triadic)
should be avoided where possible. More than three
(polyadic) requires very special justification

Related

When do I split up classes into different scripts? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have been working on a project in Unity, and was trying to figure out how to abbreviate a large number into a more readable format. I found somebody who asked the same question and got some code, but the person who gave that code had 2 classes in the same C# script. I am new to Unity and C# in general, so this was not something I had seen before.
What I would like to know is when to put classes in different scripts, when to put multiple classes in the same script, and if I do put multiple classes in the same script how that affects that script and other scripts in the project.
From a C# logical point of view, it does not matter where a class is. From the practical perspective, it is usual to put every type (class, struct) in its own code file. I often make an exception for enums and put enums belonging to the same realm into the same file, e.g. things like DisplayStyle, SortOrder, Visibilty could be in a file named AppearanceEnums.cs. Enums are mostly small and don't contain logic.
for Unity, see: How to architect code as your project scales

Is there a way to create an exact copy of "if" statement but with a different name? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to create an "if" statement but with a different name. Something that I could type instead of "if" but would work exact the same way.
I'm pretty sure you could do this in C and C++ by use of macros.
Such a usage could have looked something like this;
#define FagCelDev if
This is not available in C#. According to this source this was a decision made to help keep the language readable. I would suggest that they made a good decision. There is no good reason to do what you are proposing - it will just make your code less readable.
Doing things like this is a highly effective way to confuse yourself when you read your code in a year.

Calling another language with MQL5 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I would like to scan an economic calendar (and in a second step possibly a news source) like this one for use in MetaTrader/MQL5. I guess I would need another programming language than MQL5 for that, possibly (but not necessarily) C#? (How) can it be done in principle?
Thank you very much in advance!
You can check whether you can access that web page through WebRequest() function available in MT4/5. Alternative way is to write a DLL (or to find one) and access the link above through the REST api (but it doesnt make sense as WebRequest provides it) or somehow else. The easiest way is to check all calendars you may find (myfxbook, mt5.com, fxfactory) and find the easiest page to parse with MT5 methods, then try to collect data and process it. If your skills include some other languages, it might make sense to collect data with REST and then parse it with Jsoup/soup/beautiful-soup (the library that is designed for your language) - that will help with tests (to clear the data faster), and WebRequest() for live.

C# Windows Application: handle large code in one form application with multiple tabs [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm going to develop a C# windows application which hosts several tabs in one form. Since components inside each tab are complicated enough, having all codes stored in one file Form1.cs is make it hard to handle all methods and code snippets.
I want to know are there any good practices to manage code in such condition?
I have made Forms Applications like that before, and I know what you mean about the code getting cumbersome for the .cs file.
Assuming you're not doing this already:
What I would do is compartmentalize some of the methods and members to separate classes that you call when needed via access operators and/or references. If you find yourself reusing the same set of lines dozens of times, put it in a static method that you can call everywhere without having to declare class objects... Each Tab can call some kind of method like RunTab1(); that will access the appropriate objects, classes, members and so forth. This way, when you want to work on Tab2, you can go to that class and ruffle through there, instead of going through one GIANT file looking for one small thing.
I know that sounds like a bit of a generic answer, but I've done it and it worked for me for a multilingual translator among many other things. Even what I'm working on now - some of the files are over 30k lines and I don't get any lag at runtime. Hope that helps.

Can I use unmanaged C++ code to reduce calculation costs in a C# project? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I want to use a c++ project to do some calculations for a c# project and return the results.
I was wondering if I would benefit a more efficient calculation speed in c++ if I do so?
Would still be efficient if I wrapped the native code in c++/cli?
Are there any examples out there?
Just as simple example say you have two double values A and B in C#, how would you have c++ project to receive A and B and a string value "plus" or "times" to calculate and return A + B or A * B?
Use Process.Start(); to spawn your optimized program. You will be able to pass parameters and even read the output. Start here: https://msdn.microsoft.com/en-us/library/53ezey2s(v=vs.110).aspx
You've got two separate issues in your question: "How do I", and "Should I".
If you're having problems with the "How do I", please post a question with the specific code you have, and what problems you're having.
"Should I" is somewhat of a nebulous question: It depends a lot on the type of calculations you're trying to do. These questions often have no one right answer. (Also note that this type of question is often offtopic for Stack Overflow for that very reason, so this question may be closed.)
For some types of calculations, the C++ compiler might produce more efficient code than the .Net Jitter. For some types, it won't make a difference. C++ would also let you do things like using the GPU to perform the calculations.
Also, consider how long it will take you to write this optimized code, and how often you're going to run it. If this needs to run overnight once a month, maybe a couple hours to run is fine.

Categories