我应该忽略 GUI class 的自动代码审查错误吗?
Should I ignore automated code review errors for GUI class?
我应该忽略 GUI class 的自动代码审查错误吗?这个 class 是由 Windowbuilder 生成的。我是否应该以不同的方式使它在自动代码审查中获得 A 级,我应该怎么做,或者我应该忽略它们,因为这种类型的 classes 没有真正的复杂性?仅描述 window 个组件,仅此而已。代码审查告诉我:
- 赋值分支条件
- 实例变量太多
- 函数太长
- 总复杂度太高
这是代码
package gui.dialogs;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import data.elements.Property;
import data.elements.Model;
import gui.text.DeviceDefaultTextUtils;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Button;
import java.awt.Dialog;
import javax.swing.SwingConstants;
public class DeviceDialog extends JDialog
{
private static final long serialVersionUID = -7176709114506808074L;
public JPanel contentPane = new JPanel();
public JLabel lblDeviceIDValue = new JLabel();
public Button btnAddNewModel;
public JTextField textFieldSerial = new JTextField();
public Button btnAdd;
public Button btnCancel;
/**
* Creates frame "Device Registration Form".
*/
public DeviceDialog(Dialog parentWindow)
{
createJDialogView();
createDeviceIDView();
createDeviceTypeView();
createDeviceManufacturerView();
createDeviceModelView();
createDeviceSerialView();
createButtonsView();
}
private void createJDialogView()
{
setTitle(DeviceDefaultTextUtils.TITLE);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 300, 420);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
}
private void createDeviceIDView()
{
JLabel lblDeviceID = new JLabel(DeviceDefaultTextUtils.DEVICE_ID_LABEL);
lblDeviceID.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblDeviceID.setBounds(20, 10, 75, 25);
contentPane.add(lblDeviceID);
lblDeviceIDValue.setHorizontalAlignment(SwingConstants.CENTER);
lblDeviceIDValue.setFont(new Font("Segoe UI Symbol", Font.BOLD, 15));
lblDeviceIDValue.setBounds(200, 10, 75, 25);
contentPane.add(lblDeviceIDValue);
}
private void createDeviceTypeView()
{
JLabel lblDeviceType = new JLabel(DeviceDefaultTextUtils.DEVICE_TYPE_LABEL);
lblDeviceType.setBounds(20, 50, 255, 15);
contentPane.add(lblDeviceType);
JComboBox<Property> comboBoxDeviceType = new JComboBox<Property>();
comboBoxDeviceType.setBounds(20, 75, 255, 25);
contentPane.add(comboBoxDeviceType);
}
private void createDeviceManufacturerView()
{
JLabel lblManufacturer = new JLabel(DeviceDefaultTextUtils.MANUFACTURER_LABEL);
lblManufacturer.setBounds(20, 115, 255, 15);
contentPane.add(lblManufacturer);
JComboBox<Property> comboBoxManufacturer = new JComboBox<Property>();
comboBoxManufacturer.setBounds(20, 140, 255, 25);
contentPane.add(comboBoxManufacturer);
}
private void createDeviceModelView()
{
JLabel lblModel = new JLabel("Model");
lblModel.setBounds(20, 175, 255, 15);
contentPane.add(lblModel);
JComboBox<Model> comboBoxModel = new JComboBox<Model>();
comboBoxModel.setBounds(20, 200, 255, 25);
contentPane.add(comboBoxModel);
btnAddNewModel = new Button(DeviceDefaultTextUtils.ADD_NEW_MODEL_BUTTON);
btnAddNewModel.setBounds(95, 234, 110, 25);
contentPane.add(btnAddNewModel);
}
private void createDeviceSerialView()
{
JLabel lblSerial = new JLabel(DeviceDefaultTextUtils.SERIAL_NUMBER_LABEL);
lblSerial.setBounds(20, 265, 255, 14);
contentPane.add(lblSerial);
textFieldSerial.setBounds(20, 290, 255, 25);
contentPane.add(textFieldSerial);
textFieldSerial.setColumns(10);
}
private void createButtonsView()
{
btnAdd = new Button(DeviceDefaultTextUtils.ADD_DEVICE_BUTTON);
btnAdd.setFont(new Font("Dialog", Font.PLAIN, 15));
btnAdd.setBounds(20, 345, 110, 25);
contentPane.add(btnAdd);
btnCancel = new Button(DeviceDefaultTextUtils.CANCEL_BUTTON);
btnCancel.setFont(new Font("Dialog", Font.PLAIN, 15));
btnCancel.setBounds(165, 345, 110, 25);
contentPane.add(btnCancel);
}
}
一般而言,所有生成的代码都应排除在自动代码审查之外。
当然,这意味着您编写的任何自定义代码都不存在于与生成的代码相同的源文件中。如果您在将生成的代码提供给您的审查过程之前修改生成的代码,您应该将此文件视为未完整生成,除非您明确标记生成的部分并且您的工具允许仅排除文件部分(例如,在 Sonar 中,您可以将事物标记为 "won't fix" 并在这种情况下给出 "generated code" 的原因作为评论)。
我应该忽略 GUI class 的自动代码审查错误吗?这个 class 是由 Windowbuilder 生成的。我是否应该以不同的方式使它在自动代码审查中获得 A 级,我应该怎么做,或者我应该忽略它们,因为这种类型的 classes 没有真正的复杂性?仅描述 window 个组件,仅此而已。代码审查告诉我:
- 赋值分支条件
- 实例变量太多
- 函数太长
- 总复杂度太高
这是代码
package gui.dialogs;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import data.elements.Property;
import data.elements.Model;
import gui.text.DeviceDefaultTextUtils;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Button;
import java.awt.Dialog;
import javax.swing.SwingConstants;
public class DeviceDialog extends JDialog
{
private static final long serialVersionUID = -7176709114506808074L;
public JPanel contentPane = new JPanel();
public JLabel lblDeviceIDValue = new JLabel();
public Button btnAddNewModel;
public JTextField textFieldSerial = new JTextField();
public Button btnAdd;
public Button btnCancel;
/**
* Creates frame "Device Registration Form".
*/
public DeviceDialog(Dialog parentWindow)
{
createJDialogView();
createDeviceIDView();
createDeviceTypeView();
createDeviceManufacturerView();
createDeviceModelView();
createDeviceSerialView();
createButtonsView();
}
private void createJDialogView()
{
setTitle(DeviceDefaultTextUtils.TITLE);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 300, 420);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
}
private void createDeviceIDView()
{
JLabel lblDeviceID = new JLabel(DeviceDefaultTextUtils.DEVICE_ID_LABEL);
lblDeviceID.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblDeviceID.setBounds(20, 10, 75, 25);
contentPane.add(lblDeviceID);
lblDeviceIDValue.setHorizontalAlignment(SwingConstants.CENTER);
lblDeviceIDValue.setFont(new Font("Segoe UI Symbol", Font.BOLD, 15));
lblDeviceIDValue.setBounds(200, 10, 75, 25);
contentPane.add(lblDeviceIDValue);
}
private void createDeviceTypeView()
{
JLabel lblDeviceType = new JLabel(DeviceDefaultTextUtils.DEVICE_TYPE_LABEL);
lblDeviceType.setBounds(20, 50, 255, 15);
contentPane.add(lblDeviceType);
JComboBox<Property> comboBoxDeviceType = new JComboBox<Property>();
comboBoxDeviceType.setBounds(20, 75, 255, 25);
contentPane.add(comboBoxDeviceType);
}
private void createDeviceManufacturerView()
{
JLabel lblManufacturer = new JLabel(DeviceDefaultTextUtils.MANUFACTURER_LABEL);
lblManufacturer.setBounds(20, 115, 255, 15);
contentPane.add(lblManufacturer);
JComboBox<Property> comboBoxManufacturer = new JComboBox<Property>();
comboBoxManufacturer.setBounds(20, 140, 255, 25);
contentPane.add(comboBoxManufacturer);
}
private void createDeviceModelView()
{
JLabel lblModel = new JLabel("Model");
lblModel.setBounds(20, 175, 255, 15);
contentPane.add(lblModel);
JComboBox<Model> comboBoxModel = new JComboBox<Model>();
comboBoxModel.setBounds(20, 200, 255, 25);
contentPane.add(comboBoxModel);
btnAddNewModel = new Button(DeviceDefaultTextUtils.ADD_NEW_MODEL_BUTTON);
btnAddNewModel.setBounds(95, 234, 110, 25);
contentPane.add(btnAddNewModel);
}
private void createDeviceSerialView()
{
JLabel lblSerial = new JLabel(DeviceDefaultTextUtils.SERIAL_NUMBER_LABEL);
lblSerial.setBounds(20, 265, 255, 14);
contentPane.add(lblSerial);
textFieldSerial.setBounds(20, 290, 255, 25);
contentPane.add(textFieldSerial);
textFieldSerial.setColumns(10);
}
private void createButtonsView()
{
btnAdd = new Button(DeviceDefaultTextUtils.ADD_DEVICE_BUTTON);
btnAdd.setFont(new Font("Dialog", Font.PLAIN, 15));
btnAdd.setBounds(20, 345, 110, 25);
contentPane.add(btnAdd);
btnCancel = new Button(DeviceDefaultTextUtils.CANCEL_BUTTON);
btnCancel.setFont(new Font("Dialog", Font.PLAIN, 15));
btnCancel.setBounds(165, 345, 110, 25);
contentPane.add(btnCancel);
}
}
一般而言,所有生成的代码都应排除在自动代码审查之外。
当然,这意味着您编写的任何自定义代码都不存在于与生成的代码相同的源文件中。如果您在将生成的代码提供给您的审查过程之前修改生成的代码,您应该将此文件视为未完整生成,除非您明确标记生成的部分并且您的工具允许仅排除文件部分(例如,在 Sonar 中,您可以将事物标记为 "won't fix" 并在这种情况下给出 "generated code" 的原因作为评论)。