How to simplify my code?

I just finished creating my first main application in C # / Silverlight. As a result, the total number of lines was more than 12,000 lines of code. Given this, the php / javascript application was rewritten, which I created 2 years, which was more than 28,000 lines. I am really proud of my achievement.

After reading many questions and answers here on stackoverflow and other sites on the Internet, I followed the advice of many posters: I created classes, procedures, etc. for things that I would copy and paste a year ago; I created logic to define complex functions; make sure there are no crazy hidden characters (tabs are used instead of tabs); and a few more things; post comments if necessary (I have a lot of comments).

My application consists of 4 tiles laid out horizontally that control user elements in each fragment. You can have from one to four sectors loaded at any time. If you once downloaded a fragment, the slice occupies the entire artboard ... if you have 2 downloaded, each takes half, 3 seconds, 4 per quarter.

Each of these sections represents (for this example) light control. Each slice has 3 sliders in it. Now that I have encoded the functionality of the sliders, I used the switch / case statement inside a public function that would run the command on the specified slice / slider. This did for some duplicated code, but I did not see around it, since each fragment was named differently. So I would do slice1.my.commands (); slice2.my.commands (); etc.

My question to you is how to clear my code even further? (Unfortunately, I cannot post any of my codes). Is there any way to do this repetition from my code?

+3
source share
5 answers

What you need is an interface with your friend - a strategy template. For instance:

public interface ISlice 
{
    public Slider Slide {get;set;}
}

public class Slice1 : ISlice 
{
    public Slider Slide { get; set; }
}

public static class SliceSlider
{
    public static void DoSomethingCoolWithTheSliceSlide(ISlice slice) 
    {
        slice.Slide.LookitMeIAmLearningDesignPatterns();
    }
} 
+11
source

Writing less code should not be your goal. As a result, all this relates to TCO (total cost of ownership).

Although owning less code can improve the total cost of ownership, there is one factor that has a much greater impact on TCO: maintainability. You should write the most convenient code. Start by reading the Robert Martin Clean Code .

Update:

: " ". , . , . , " " " ".

2:

, :

  • " [Single Responsibility Principle]" [. 138]
  • " [ ] ". [ 288]
  • " , . , ". [. 34]
  • " 20 " ​​[. 34]
  • " " [. 304]
  • " ". [. 286]
+7

. . , , , , , .

, . , , . , . :

DoFactory

+2

.net.

- , . : / , , . - .

, "" , . .

:

public interface ISlice
{
    bool DoStuff(string someParameter);
}

public class MySpecificSliceOfType : ISlice
{
    // this must have a method implementation for the [bool DoStuff(string)] method
    public bool DoStuff(string mySpecificParameter)
    {
       // LOGIC in the Specific class
       return(true);
    }
}

public class MyOtherSliceOfType : ISlice
{
    // this must have a method implementation for the [bool DoStuff(string)] method
    public bool DoStuff(string myOtherParameter)
    {
       // LOGIC in the Other class
       return(true);
    }
}

, , ISlice "MySpecificSliceOfType" "MyOtherSliceOfType" , DoStuff() , , - :

bool sliceReturn = ((ISlice)currentSlice).DoStuff(currentStringParameterValue);

, :

bool sliceReturn = false;
switch(typeofSlice)
{
    case "other" :
        sliceReturn = MyOtherSliceOfType.DoStuff(currentStrignParamterValue);
        break;
    case "specific" :
        sliceReturn = MySpecificSliceOfType.DoStuff(currentStrignParamterValue);
        break;
}

, > 2 .

#.

Reflection... - , , ... Serialization (a.k.a. Serialization), .

+2

Since you cannot post any of your codes, I might as well throw out a random thought. Can you put these fragments in an array? If so, you can get rid of some redundant code if each of the controls sets a variable (I will name it whichSlice). therefore, all controls are set whichSliceto the desired number 1-4, and then you start the normal switch and callslices[whichSlice].my.commands();

0
source

Source: https://habr.com/ru/post/1738357/


All Articles