当前位置:网站首页>Declare war on uncivilized code II

Declare war on uncivilized code II

2022-06-21 13:42:00 Guo Mahua

Last one , I introduced the transformation process of a function point Declare war on uncivilized code One , Let me show you some details of this article .

Case study 0 Using bit enumerations to represent permutations

The figure below shows the docking document of a certain platform , The orientation of the house is southeast, Northwest , given 16 A combination scheme .

Actually, it can be used 4 Bit binary to represent , such as : The southeast and northwest correspond to 1111, The southeast corresponds to 1100, The northwest corresponds to 0011, East correspondence 1000. However, the sequence number and orientation of the above figure have no rules to follow , It's completely random .

 

Case a     What is this for ?

  Logic is very simple : Analyze... In a set id, Can be converted into guid Is valid id, Add to collection .

I don't make complaints about it , Look directly at the optimized code :

var brokerIds = progress.Result.where(t=> Guid.TryParse(t.UserId, out var _)).select(t=> t.UserId).ToList();

Case 2   It's puzzling

  The original intention of this code is to use ClaimTypes.NameIdentifier obtain BrokerId , If you don't succeed, use "sub" To get , But why should it be foreach Well , In addition to increasing the difficulty of understanding , It doesn't work .

Case three Super long constructor

  You can see that the constructor is written in nearly 100 lines , The part I circled is the processing of parameters , For example, house type : 1-1-1-1 It should be divided into one room, one living room, one kitchen and one bathroom .

This code has no maintainability whatsoever ! You must always pay attention to the order of parameters when modifying ! Like caring for a baby .

It should be transformed , For example, the logic of parameter processing is extracted into methods , Initialize the object in the form of a call chain .

  public SecondHouse SetFloor(string floor)
        {
            if (!string.IsNullOrWhiteSpace(floor))
            {
                try
                {
                    var array = floor.Split('/');
                    this.floor = Convert.ToInt32(array[0].Trim(' layer '));
                    this.totalFloor = Convert.ToInt32(array[1].Trim(' layer '));
                }
                catch (Exception)
                {
                    throw new Push58DomainException(" Wrong floor format ");
                }
            }

            return this;
        }

 

Case four You can pass any parameter you need !

Some students are very confused about the function definition , A good method signature should be understandable at a glance : What does this function do ? What input is ? What's the result ?

for instance , I have a function that needs to get the user Id And mobile phone number , Then you can directly pass these two parameters .

But some students do not know that it is for object reuse , Or the future expansion , Will directly put User Object is passed as a parameter  ,

The function arguments also become (Dto input, User user) Such an ambiguous function .

Originally, you only need two fields , The result was that the user's birthday was given to !

actually , We can integrate the input parameters , Or directly specify the required parameters .

  ending

I really want to make a big change in my project , But this thing has done no good , It's too much to do wrong , difficult .

For the time being, roast is here , The more you think about it, the higher your blood pressure , I'll add later .

原网站

版权声明
本文为[Guo Mahua]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221433592054.html