The encoding specification for C is detailed

  • 2020-05-17 06:12:23
  • OfStack

The first thing we need to understand is 1,
1. Writing code has a very, very important function besides making it run, which is maintenance, because without 1 percent constant code, requirements change and code inevitably changes.
2. You are not one person writing code. There is a team behind you, and any one person in the team may change your code. For example, the initial code might look like this,

if (OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "CZ-SP")
                {
                    OrderCostAndWeightEntity e = new OrderCostAndWeightEntity();
                    e.FlightAgency = InsuranceCommon.GetStrategyFlightAgencyNew(OrderInfo.O_OrdersEntity.DirectFlightChannel);
                    e = ReGetFlight.GetDirectFlightHighWeightEntity(OrderInfo.O_FlightEntity, "", e.FlightAgency);
                    e.Sendticketcity = int.Parse(ConfigurationManager.AppSettings["SendticketcityOfDirectFlights_HU"].ToString());
                    regetFlightInitData.HighCostAndWeightEntity = e;
                    regetFlightInitData.TicketType = "0001";
                    // If it's hna , There is no need for policy verification 
                    regetFlightInitData.ReSearchNoFlight = false;
                }
                else
                {
                    OrderCostAndWeightEntity e = new OrderCostAndWeightEntity();
                    e.Sendticketcity = int.Parse(ConfigurationManager.AppSettings["SendticketcityOfDirectFlights_HU"].ToString());
                    e.FlightAgency = InsuranceCommon.GetStrategyFlightAgencyNew(OrderInfo.O_OrdersEntity.DirectFlightChannel);
                    e.CostRate1 = 1.0M;
                    regetFlightInitData.HighCostAndWeightEntity = e;
                    // If it's hna , There is no need for policy verification 
                    regetFlightInitData.ReSearchNoFlight = false;
                }

But after 1 time comes another requirement, for "MU-WS" also needs to go to the above logic, what would you write, if you just completed the task might be like this

if (OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "CZ-SP" || OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "MU-WS")
                {
                    OrderCostAndWeightEntity e = new OrderCostAndWeightEntity();
                    e.FlightAgency = InsuranceCommon.GetStrategyFlightAgencyNew(OrderInfo.O_OrdersEntity.DirectFlightChannel);
                    e = ReGetFlight.GetDirectFlightHighWeightEntity(OrderInfo.O_FlightEntity, "", e.FlightAgency);
                    e.Sendticketcity = int.Parse(ConfigurationManager.AppSettings["SendticketcityOfDirectFlights_HU"].ToString());
                    regetFlightInitData.HighCostAndWeightEntity = e;
                    regetFlightInitData.TicketType = "0001";
                    // If it's hna , There is no need for policy verification 
                    regetFlightInitData.ReSearchNoFlight = false;
                }
                else
                {
                    OrderCostAndWeightEntity e = new OrderCostAndWeightEntity();
                    e.Sendticketcity = int.Parse(ConfigurationManager.AppSettings["SendticketcityOfDirectFlights_HU"].ToString());
                    e.FlightAgency = InsuranceCommon.GetStrategyFlightAgencyNew(OrderInfo.O_OrdersEntity.DirectFlightChannel);
                    e.CostRate1 = 1.0M;
                    regetFlightInitData.HighCostAndWeightEntity = e;
                    // If it's hna , There is no need for policy verification 
                    regetFlightInitData.ReSearchNoFlight = false;
                }

If you write it like this you're the one who started it, and everyone else is going to be working on it like you, and the code might end up looking something like this

if (OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "CZ-SP" || OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "MU-WS" || OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "MU-WS" || OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "MU-SP" || OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "CZ-WS" || OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "XT-WS")
                {
                    OrderCostAndWeightEntity e = new OrderCostAndWeightEntity();
                    e.FlightAgency = InsuranceCommon.GetStrategyFlightAgencyNew(OrderInfo.O_OrdersEntity.DirectFlightChannel);
                    e = ReGetFlight.GetDirectFlightHighWeightEntity(OrderInfo.O_FlightEntity, "", e.FlightAgency);
                    e.Sendticketcity = int.Parse(ConfigurationManager.AppSettings["SendticketcityOfDirectFlights_HU"].ToString());
                    regetFlightInitData.HighCostAndWeightEntity = e;
                    regetFlightInitData.TicketType = "0001";
                    // If it's hna , There is no need for policy verification 
                    regetFlightInitData.ReSearchNoFlight = false;
                }
                else
                {
                    OrderCostAndWeightEntity e = new OrderCostAndWeightEntity();
                    e.Sendticketcity = int.Parse(ConfigurationManager.AppSettings["SendticketcityOfDirectFlights_HU"].ToString());
                    e.FlightAgency = InsuranceCommon.GetStrategyFlightAgencyNew(OrderInfo.O_OrdersEntity.DirectFlightChannel);
                    e.CostRate1 = 1.0M;
                    regetFlightInitData.HighCostAndWeightEntity = e;
                    // If it's hna , There is no need for policy verification 
                    regetFlightInitData.ReSearchNoFlight = false;
                }

To see the effect, you need to take off the bottom strip of 1 to see the entire code, and this is the fruit of your "cause" when you plant it. So we need to improve, a common improvement is to press enter to wrap the lines, and make sure that all the code is within 1 eye of you. The improved code is as follows:

if (OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "CZ-SP" ||
                    OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "MU-WS" || 
                    OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "MU-WS" || 
                    OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "MU-SP" || 
                    OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "CZ-WS" || 
                    OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper() == "XT-WS")
                {
                    OrderCostAndWeightEntity e = new OrderCostAndWeightEntity();
                    e.FlightAgency = InsuranceCommon.GetStrategyFlightAgencyNew(OrderInfo.O_OrdersEntity.DirectFlightChannel);
                    e = ReGetFlight.GetDirectFlightHighWeightEntity(OrderInfo.O_FlightEntity, "", e.FlightAgency);
                    e.Sendticketcity = int.Parse(ConfigurationManager.AppSettings["SendticketcityOfDirectFlights_HU"].ToString());
                    regetFlightInitData.HighCostAndWeightEntity = e;
                    regetFlightInitData.TicketType = "0001";
                    // If it's hna , There is no need for policy verification 
                    regetFlightInitData.ReSearchNoFlight = false;
                }
                else
                {
                    OrderCostAndWeightEntity e = new OrderCostAndWeightEntity();
                    e.Sendticketcity = int.Parse(ConfigurationManager.AppSettings["SendticketcityOfDirectFlights_HU"].ToString());
                    e.FlightAgency = InsuranceCommon.GetStrategyFlightAgencyNew(OrderInfo.O_OrdersEntity.DirectFlightChannel);
                    e.CostRate1 = 1.0M;
                    regetFlightInitData.HighCostAndWeightEntity = e;
                    // If it's hna , There is no need for policy verification 
                    regetFlightInitData.ReSearchNoFlight = false;

Is relaxed and 1 point, good-looking 1 point, if you write in the first time I modify the code so that the people behind will follow like this, they might think, one in front of the writing should be no problem, write like this, although the code looks a bit awkward, then we can take the appreciation view of the code, if you want to 1 to think there's a better way, why not use switch?

switch (OrderInfo.O_OrdersEntity.DirectFlightChannel.Trim().ToUpper())
                {
                    case "CZ-SP":
                    case "MU-SP":
                    case "XT-WS":
                        {
                            OrderCostAndWeightEntity e = new OrderCostAndWeightEntity();
                            e.FlightAgency = InsuranceCommon.GetStrategyFlightAgencyNew(OrderInfo.O_OrdersEntity.DirectFlightChannel);
                            e = ReGetFlight.GetDirectFlightHighWeightEntity(OrderInfo.O_FlightEntity, "", e.FlightAgency);
                            e.Sendticketcity = int.Parse(ConfigurationManager.AppSettings["SendticketcityOfDirectFlights_HU"].ToString());
                            regetFlightInitData.HighCostAndWeightEntity = e;
                            regetFlightInitData.TicketType = "0001";
                            // If it's hna , There is no need for policy verification 
                            regetFlightInitData.ReSearchNoFlight = false;
                            break;
                        }
                    default:
                        {
                            OrderCostAndWeightEntity e = new OrderCostAndWeightEntity();
                            e.Sendticketcity = int.Parse(ConfigurationManager.AppSettings["SendticketcityOfDirectFlights_HU"].ToString());
                            e.FlightAgency = InsuranceCommon.GetStrategyFlightAgencyNew(OrderInfo.O_OrdersEntity.DirectFlightChannel);
                            e.CostRate1 = 1.0M;
                            regetFlightInitData.HighCostAndWeightEntity = e;
                            // If it's hna , There is no need for policy verification 
                            regetFlightInitData.ReSearchNoFlight = false;
                            break;
                        }
                }

If you write it this way, the people behind you will not hesitate to add an case "" : because this is a familiar syntax, there is no doubt that this is correct, so we can look at this code with appreciation.
3. Admit that there is only so much of our mind and so much of our brain that we can't read a single line or ten lines of code, so don't write a single line of code that doesn't make sense.
The above ideas are based on the maintenance of other people's code pain, loss, frustration, all kinds of discomfort, all kinds of ridicule, so the code specification is that we need to pay attention to all the time, here I summarize 1 specification, warned myself not to be the first one.
1.1 files only put 1 class, class name and file name, do not write several classes in a file, so that you can see clearly.
2. Do not write more than 1000 lines of code in a single file, except for large entity classes. I would also say that more than 500 lines of code looks a little tired, but we have more than 10,000 lines of code on our system.
3.1 don't have more than 100 lines of code for a method, but I would say that a method with more than 50 lines looks a little tired. But in our system there are more than 200 lines of code for the methods everywhere.
4. Don't write more than 100 lines of stored procedure code. Don't write too much business logic in the stored procedure.
5. Avoid writing methods with more than 5 arguments. Use 1 class or structure if you have one.
6.1 only 1 return result method; , do not return results multiple times, it is better to assign the value of the returned results, finally return result;
7. Don't comment very simple code. It can be noisy and misleading because most of what you write is one-sided.
8. When logging, don't write it everywhere. Only record one log for one operation (such as placing an order) if possible.
In addition, here is a list of their own code to do a number of improvements, shortcomings are welcome to point out.
1. Use indentation when parameters are too long

a.
                    // Obtain insurance policy information 
                    insuranceStrategy = InsuranceCommon.GetStrategyInsurance(appFltEntity.ProductSource.ToString(), strategyFlightAgency.ToString(), "", appFltEntity.Price.ToString(), appFltEntity.DirectFlightChannel, appFltEntity.Airline.DibitCode, appFltEntity.Flight, appFltEntity.DepartAirport.Code, appFltEntity.ArriveAirport.Code, appFltEntity.SubClass, appFltEntity.DepartTime, appFltEntity.DepartTime.Date, null);
b.
                    // Obtain insurance policy information 
                    insuranceStrategy = InsuranceCommon.GetStrategyInsurance(appFltEntity.ProductSource.ToString(), 
                                                                             strategyFlightAgency.ToString(), 
                                                                             "", 
                                                                             appFltEntity.Price.ToString(), 
                                                                             appFltEntity.DirectFlightChannel, 
                                                                             appFltEntity.Airline.DibitCode, 
                                                                             appFltEntity.Flight, 
                                                                             appFltEntity.DepartAirport.Code, 
                                                                             appFltEntity.ArriveAirport.Code, 
                                                                             appFltEntity.SubClass, 
                                                                             appFltEntity.DepartTime,
                                                                             appFltEntity.DepartTime.Date, 
                                                                             null);

2. Use indentation when if condition is too long

a.
                if (IsDirectFlight && (this.CorpPayType == ExpenseType.OWN || this.AccountInfo.DirectOrdersQuoteMode == "I"))
                {
                    strategyFlightAgency = InsuranceCommon.GetStrategyFlightAgencyNew(appFltEntity.DirectFlightChannel);
                    // Get the city of the ticket counter according to the ticket counter 
                    strategyCityID = InsuranceCommon.GetFlightAgencyCity(strategyFlightAgency);
                 }
b.
                if (IsDirectFlight &&
                    (this.CorpPayType == ExpenseType.OWN ||
                    this.AccountInfo.DirectOrdersQuoteMode == "I"))
                {
                    strategyFlightAgency = InsuranceCommon.GetStrategyFlightAgencyNew(appFltEntity.DirectFlightChannel);
                    // Get the city of the ticket counter according to the ticket counter 
                    strategyCityID = InsuranceCommon.GetFlightAgencyCity(strategyFlightAgency);
                 }

3. Use 3-yuan expressions

a.
                        if (InsuranceInfoList != null && InsuranceInfoList.Count > 0 && InsuranceInfoList[0].GeneralDescription.Contains("e Taikang road "))
                            collections.Set("IsElderCanBuyInsurance", "T");
                        else
                            collections.Set("IsElderCanBuyInsurance", "F");
b.
collections.Set("IsElderCanBuyInsurance", InsuranceInfoList != null && InsuranceInfoList.Count > 0 && InsuranceInfoList[0].GeneralDescription.Contains("e Taikang road ") ? "T" : "F");

4. Use blue expressions

a.
                        InsuranceInfoList = InsuranceInfoList.FindAll(delegate(BookingInsuranceInfo iInfo)
                        {
                            return iInfo.TypeID == "C2C30";
                        });
b.
InsuranceInfoList = InsuranceInfoList.FindAll(a => a.TypeID == "C2C30");

5. Use the switch expression, see above.
6. Use array include

a.
            if (wsFlt.ProductSource == 4 && !string.IsNullOrEmpty(wsFlt.DirectFlightChannel) && (wsFlt.DirectFlightChannel.ToUpper() == "HO-WS" or wsFlt.DirectFlightChannel.ToUpper() == "ZH-WS" or wsFlt.DirectFlightChannel.ToUpper() == "XT-WS")
                && directFlightCorporationList != null && directFlightCorporationList.Count > 0 && !directFlightCorporationList.Contains(corporationid))
            {
                return false;
            }
b.
            if (wsFlt.ProductSource == 4 &&
                new List<string>() { "HO-WS", "CZ-WS", "XT-WS" }.Contains(wsFlt.DirectFlightChannel.Trim().ToUpper()) &&
                directFlightCorporationList != null && 
                directFlightCorporationList.Count > 0 &&
                !directFlightCorporationList.Contains(corporationid))
            {
                return false;
            }

You can see that indentation is also used in the code.
You are welcome to list your own examples of code simplicity.

Related articles: