避免在条件语句中同时嵌套if / else if和避免InvalidOperationException

时间:2019-05-13 12:00:14

标签: c# anti-patterns

我遇到了一个巨大的方法,其中有很多类型的if else语句

else if (ptName == "Missile" ||
         ptName == "Telephone" ||
        (ptName.StartsWith("Car") && gasTank.ValueFloat1.Value < 90))
        {
         foo_1 = "some_string";
        }

不幸的是,此代码并不完全正确,因为变量gasTank仅在ptName以字符串“ Car”开头的情况下定义。因此,在运行时我得到了

  

发生InvalidOperationException:空对象必须具有一个值。

这可以通过嵌套ifs和else ifs以及重复条件ptName.StartsWith("Car")来解决,尽管这是非常不希望的。

将方法拆分为许多小方法或在每个条件之后添加return都不是一种选择,因为该方法的return是各种foo_i的串联。

先谢谢您。

1 个答案:

答案 0 :(得分:4)

条件

0000A2   slice_layer_without_partitioning (IDR) - 0 (0x0) - Frame 0 - slice_type I - frame_num 0 - DTS 00:00:00.000 - PTS 00:00:00.017 (141867 bytes)
0000A2    Header (5 bytes)
0000A2     zero_byte:                          0 (0x00)
0000A3     start_code_prefix_one_3bytes:       1 (0x000001)
0000A6     nal_ref_idc:                        3 (0x3) - (2 bits)
0000A6     nal_unit_type:                      5 (0x05) - (5 bits)
0000A7    slice_header (3 bytes)
0000A7     first_mb_in_slice:                  0 (0x0)
0000A7     slice_type:                         7 (0x07) - I
0000A8     pic_parameter_set_id:               0 (0x0)
0000A8     frame_num:                          0 (0x0)
0000A8     idr_pic_id:                         0 (0x0)
0000A8     no_output_of_prior_pics_flag:       No
0000A8     long_term_reference_flag:           No
0000A9     slice_qp_delta:                     -5 (0xFFFFFFFB)
0000AA     disable_deblocking_filter_idc:      0 (0x0)
0000AA     slice_alpha_c0_offset_div2:         0 (0x0)
0000AA     slice_beta_offset_div2:             0 (0x0)
0000AA    slice_data (141856 bytes)
0000AA     (ToDo):                             (Data)
022ACD   slice_layer_without_partitioning (IDR) - 0 (0x0) - Frame 0 - slice_type I - frame_num 0 - DTS 00:00:00.000 - PTS 00:00:00.017 - first_mb_in_slice 8040 (2248 bytes)
022ACD    Header (5 bytes)
022ACD     zero_byte:                          0 (0x00)
022ACE     start_code_prefix_one_3bytes:       1 (0x000001)
022AD1     nal_ref_idc:                        3 (0x3) - (2 bits)
022AD1     nal_unit_type:                      5 (0x05) - (5 bits)
022AD2    slice_header (6 bytes)
022AD2     first_mb_in_slice:                  8040 (0x001F68)
022AD5     slice_type:                         7 (0x07) - I
022AD6     pic_parameter_set_id:               0 (0x0)
022AD6     frame_num:                          0 (0x0)
022AD6     idr_pic_id:                         0 (0x0)
022AD6     no_output_of_prior_pics_flag:       No
022AD6     long_term_reference_flag:           No
022AD7     slice_qp_delta:                     -5 (0xFFFFFFFB)
022AD8     disable_deblocking_filter_idc:      0 (0x0)
022AD8     slice_alpha_c0_offset_div2:         0 (0x0)
022AD8     slice_beta_offset_div2:             0 (0x0)
022AD8    slice_data (2237 bytes)
022AD8     (ToDo):                             (Data)
023395  1 (36212 bytes)
023395   slice_layer_without_partitioning (non-IDR) - 2 (0x2) - Frame 1 - slice_type P - frame_num 1 - DTS 00:00:00.000 (36017 bytes)
023395    Header (5 bytes)
023395     zero_byte:                          0 (0x00)
023396     start_code_prefix_one_3bytes:       1 (0x000001)
023399     nal_ref_idc:                        3 (0x3) - (2 bits)
023399     nal_unit_type:                      1 (0x01) - (5 bits)
02339A    slice_header (3 bytes)
02339A     first_mb_in_slice:                  0 (0x0)
02339A     slice_type:                         5 (0x5) - P
02339A     pic_parameter_set_id:               0 (0x0)
02339A     frame_num:                          1 (0x1)
02339B     num_ref_idx_active_override_flag (0 bytes)
02339B      num_ref_idx_active_override_flag:  Yes
02339B      num_ref_idx_l0_active_minus1:      0 (0x0)
02339B     ref_pic_list_modification_flag_l0:  No
02339B     adaptive_ref_pic_marking_mode_flag: No
02339C     cabac_init_idc:                     0 (0x0)
02339C     slice_qp_delta:                     -3 (0xFFFFFFFD)
02339C     disable_deblocking_filter_idc:      0 (0x0)
02339C     slice_alpha_c0_offset_div2:         0 (0x0)
02339D     slice_beta_offset_div2:             0 (0x0)
02339D    slice_data (36012 bytes)
02339D     (ToDo):                             (Data)
02C046   slice_layer_without_partitioning (non-IDR) - 2 (0x2) - Frame 1 - slice_type P - frame_num 1 - DTS 00:00:00.000 - first_mb_in_slice 8040 (195 bytes)
02C046    Header (5 bytes)
02C046     zero_byte:                          0 (0x00)
02C047     start_code_prefix_one_3bytes:       1 (0x000001)
02C04A     nal_ref_idc:                        3 (0x3) - (2 bits)
02C04A     nal_unit_type:                      1 (0x01) - (5 bits)
02C04B    slice_header (6 bytes)
02C04B     first_mb_in_slice:                  8040 (0x001F68)
02C04E     slice_type:                         5 (0x5) - P
02C04E     pic_parameter_set_id:               0 (0x0)
02C04E     frame_num:                          1 (0x1)
02C04F     num_ref_idx_active_override_flag (0 bytes)
02C04F      num_ref_idx_active_override_flag:  Yes
02C04F      num_ref_idx_l0_active_minus1:      0 (0x0)
02C04F     ref_pic_list_modification_flag_l0:  No
02C04F     adaptive_ref_pic_marking_mode_flag: No
02C050     cabac_init_idc:                     0 (0x0)
02C050     slice_qp_delta:                     -3 (0xFFFFFFFD)
02C050     disable_deblocking_filter_idc:      0 (0x0)
02C050     slice_alpha_c0_offset_div2:         0 (0x0)
02C051     slice_beta_offset_div2:             0 (0x0)
02C051    slice_data (190 bytes)
02C051     (ToDo):                             (Data)
02C109  1 (26280 bytes)
02C109   slice_layer_without_partitioning (non-IDR) - 4 (0x4) - Frame 2 - slice_type P - frame_num 2 - DTS 00:00:00.000 (26157 bytes)
02C109    Header (5 bytes)
02C109     zero_byte:                          0 (0x00)
02C10A     start_code_prefix_one_3bytes:       1 (0x000001)
02C10D     nal_ref_idc:                        3 (0x3) - (2 bits)
02C10D     nal_unit_type:                      1 (0x01) - (5 bits)
02C10E    slice_header (3 bytes)
02C10E     first_mb_in_slice:                  0 (0x0)
02C10E     slice_type:                         5 (0x5) - P
02C10E     pic_parameter_set_id:               0 (0x0)
02C10E     frame_num:                          2 (0x2)
02C10F     num_ref_idx_active_override_flag (0 bytes)
02C10F      num_ref_idx_active_override_flag:  Yes
02C10F      num_ref_idx_l0_active_minus1:      0 (0x0)
02C10F     ref_pic_list_modification_flag_l0:  No
02C10F     adaptive_ref_pic_marking_mode_flag: No
02C110     cabac_init_idc:                     0 (0x0)
02C110     slice_qp_delta:                     -2 (0xFFFFFFFE)
02C110     disable_deblocking_filter_idc:      0 (0x0)
02C110     slice_alpha_c0_offset_div2:         0 (0x0)
02C111     slice_beta_offset_div2:             0 (0x0)
02C111    slice_data (26152 bytes)
02C111     (ToDo):                             (Data)
032736   slice_layer_without_partitioning (non-IDR) - 4 (0x4) - Frame 2 - slice_type P - frame_num 2 - DTS 00:00:00.000 - first_mb_in_slice 8040 (123 bytes)
032736    Header (5 bytes)
032736     zero_byte:                          0 (0x00)
032737     start_code_prefix_one_3bytes:       1 (0x000001)
03273A     nal_ref_idc:                        3 (0x3) - (2 bits)
03273A     nal_unit_type:                      1 (0x01) - (5 bits)
03273B    slice_header (6 bytes)
03273B     first_mb_in_slice:                  8040 (0x001F68)
03273E     slice_type:                         5 (0x5) - P
03273E     pic_parameter_set_id:               0 (0x0)
03273E     frame_num:                          2 (0x2)
03273F     num_ref_idx_active_override_flag (0 bytes)
03273F      num_ref_idx_active_override_flag:  Yes
03273F      num_ref_idx_l0_active_minus1:      0 (0x0)
03273F     ref_pic_list_modification_flag_l0:  No
03273F     adaptive_ref_pic_marking_mode_flag: No
032740     cabac_init_idc:                     0 (0x0)
032740     slice_qp_delta:                     -2 (0xFFFFFFFE)
032740     disable_deblocking_filter_idc:      0 (0x0)
032740     slice_alpha_c0_offset_div2:         0 (0x0)
032741     slice_beta_offset_div2:             0 (0x0)
032741    slice_data (118 bytes)
032741     (ToDo):                             (Data)
0327B1  1 (21125 bytes)

应该成为

ptName.StartsWith("Car")

那将使您摆脱ptName?.StartsWith("Car") ?? false 的价值。

如果您使用的是低于6的C#版本,则应使用:

null

另一个更新:

ptNAme == null ? false : ptName.StartsWith("Car")

(gasTank.ValueFloat1?.Value ?? 0) < 90