检查和更正代码样式

时间:2015-03-19 14:49:17

标签: java android

我是android developpement的新手 创建计算器应用程序。
想要提高我的代码质量 将有助于任何建议,使代码更好,更专业 (整体结构,按逻辑单元划分,符合代码约定,方法和变量名称,注释等) 这是我的代码:

public class OperationsAndFunctions {

    static enum OperEnum {
        SUM, SUBSTRACT, MULTIPLY, DIVIDE, EQUAL
    }

    static enum FuncEnum {
        RESET, BACKSPACE, SIGN, PERCENT, POINT
    }
}

public class LCalculator extends Activity {

    private TextView mDisplay;
    private boolean mDisplayIsEmpty = true;
    private boolean mFirstNumberReceived;
    private boolean mNumberEntered;
    private boolean mPointEntered;

    private double sNum1;
    private double sNum2;

    private static OperationsAndFunctions.OperEnum sOperation;
    private static OperationsAndFunctions.FuncEnum sFunction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        mDisplay = (TextView) findViewById(R.id.tvDisplay);
        mDisplay.setText("0");
    }

    // listening for number buttons pressing
    public void num_Clicked(View view) {

        Button button = (Button) view;
        int number = 0;

        // checking button ID
        switch (button.getId()) {

        case R.id.button1:
            number = 1;
            break;

        case R.id.button2:
            number = 2;
            break;

        case R.id.button3:
            number = 3;
            break;

        case R.id.button4:
            number = 4;
            break;

        case R.id.button5:
            number = 5;
            break;

        case R.id.button6:
            number = 6;
            break;

        case R.id.button7:
            number = 7;
            break;

        case R.id.button8:
            number = 8;
            break;

        case R.id.button9:
            number = 9;
            break;

        case R.id.button0:
            if (mDisplay.getText().toString().equals("0")) {
                return;
            }
            number = 0;
            break;
        }

        // setting a new digit onto display if it is empty
        if (mDisplayIsEmpty) {

            if (!(number == 0)) {
                mDisplayIsEmpty = false;
            }
            mDisplay.setText(Integer.toString(number));
        }

        // appending digit if display isn't empty
        else {
            mDisplay.append(Integer.toString(number));
        }
        mNumberEntered = true;
    }

    // listenning for operational buttons pressing
    public void op_Clicked(View view) {
        Button button = (Button) view;

        // calculating and printing result
        // if a number has been entered before
        // (Not entering here if another operation has been pressed before)
        if (mNumberEntered) {
            // if received first number and saved to sNum1
            if (mFirstNumberReceived) {
                sNum2 = Double.parseDouble(mDisplay.getText().toString());
                printResult(calculateResult(sNum1, sOperation, sNum2));
                sNum1 = Double.parseDouble(mDisplay.getText().toString());
            }

            // if not received first number - receiving it now
            else {
                sNum1 = Double.parseDouble(mDisplay.getText().toString());
                mFirstNumberReceived = true;
            }
            mNumberEntered = false;
        }

        // checking operational buttons id
        // and assigning appropriate operations enum value
        switch (button.getId()) {

        case R.id.buttonPlus:
            sOperation = OperationsAndFunctions.OperEnum.SUM;
            break;

        case R.id.buttonMinus:
            sOperation = OperationsAndFunctions.OperEnum.SUBSTRACT;
            break;

        case R.id.buttonMultiply:
            sOperation = OperationsAndFunctions.OperEnum.MULTIPLY;
            break;

        case R.id.buttonDivide:
            sOperation = OperationsAndFunctions.OperEnum.DIVIDE;
            break;

        case R.id.buttonEqual:
            sOperation = OperationsAndFunctions.OperEnum.EQUAL;
            break;
        }

        // next digit entered will be a new number
        mDisplayIsEmpty = true;
        mPointEntered = false;
    }

    // listening for functional buttons pressing
    public void func_Clicked(View view) {
        Button button = (Button) view;

        // checking operational buttons id
        // and assigning appropriate functions enum value
        switch (button.getId()) {

        case R.id.buttonAc:
            sFunction = OperationsAndFunctions.FuncEnum.RESET;
            break;

        case R.id.buttonC:
            sFunction = OperationsAndFunctions.FuncEnum.BACKSPACE;
            break;

        case R.id.buttonSign:
            sFunction = OperationsAndFunctions.FuncEnum.SIGN;
            break;

        case R.id.buttonPercent:
            sFunction = OperationsAndFunctions.FuncEnum.PERCENT;
            break;

        case R.id.buttonPoint:
            sFunction = OperationsAndFunctions.FuncEnum.POINT;
            break;
        }

        // implementing chosen function
        implementFunction(sFunction);
    }

    // implementing function
    private void implementFunction(OperationsAndFunctions.FuncEnum function) {
        sFunction = function;

        switch (function) {

        case RESET:
            reset();
            break;

        case BACKSPACE:
            printResult(Double.parseDouble(backSpace(mDisplay.getText())
                    .toString()));
            break;

        case SIGN:
            printResult(readDisplayState() * (-1));
            break;

        case PERCENT:
            printResult(readDisplayState() / 100);
            break;

        case POINT:
            if (!mPointEntered) {
                if (!mDisplayIsEmpty) {
                    mDisplay.append(".");
                } else {
                    mDisplay.setText("0.");
                    mDisplayIsEmpty = false;
                }
                mPointEntered = true;
            }
            break;
        }
    }

    // reseting the calculator
    private void reset() {
        mDisplay.setText("0");
        sNum1 = sNum2 = 0;
        mDisplayIsEmpty = true;
        mFirstNumberReceived = false;
        mNumberEntered = false;
        mPointEntered = false;
    }

    // deleting the last symbol on display
    private CharSequence backSpace(CharSequence displayState) {
        CharSequence result = displayState;

        if (displayState.length() > 1) {
            result = displayState.subSequence(0, displayState.length() - 1);
        }

        else if (displayState.length() == 1) {
            result = "0";
        }

        return result;
    }

    // calculating the result
    private double calculateResult(double num1,
            OperationsAndFunctions.OperEnum operation, double num2) {

        sNum1 = num1;
        sNum2 = num2;
        sOperation = operation;
        double result = 0;

        switch (operation) {
        case SUM:
            result = num1 + num2;
            break;

        case SUBSTRACT:
            result = num1 - num2;
            break;

        case MULTIPLY:
            result = num1 * num2;
            break;

        case EQUAL:
            mDisplayIsEmpty = true;
            mNumberEntered = false;
            mFirstNumberReceived = false;
            break;

        case DIVIDE:
            // division by zero
            if (num2 == 0) {
                showError();
                break;
            }

            result = num1 / num2;
            break;
        }
        return result;
    }

    // reading current displayNumber
    private double readDisplayState() {
        return Double.parseDouble(mDisplay.getText().toString());
    }

    // printing the result
    private void printResult(Double result) {
        // checking if the result having decimal part
        // if not then we print integer value of it without point and zero(s)
        if (result % 1 == 0) {
            mDisplay.setText(Integer.toString(result.intValue()));
        }

        else {
            mDisplay.setText(Double.toString(result));
        }
    }

    // Error message (in case of division by zero)
    private void showError() {
        // printing error message
        mDisplay.setText("Error");
        // and reseting current state
        sNum1 = sNum2 = 0;
        mDisplayIsEmpty = true;
        mFirstNumberReceived = false;
        mNumberEntered = false;
        mPointEntered = false;
    }
}

非常感谢你!

1 个答案:

答案 0 :(得分:0)

代码样式可以是个人偏好,也可以是组织指南。如果为自己工作,做最适合自己的事情。从一些已发布的标准代码指南(Android Style Guide)开始,然后更改您不喜欢的内容。如果您在一个团队中工作,无论是开源项目还是作为雇主项目的一部分,通常都有他们提出的编码标准。即使您不是所选风格的粉丝,也要遵守它。仅仅因为您重新格式化样式以匹配您的首选项而使整个文件显示为更新在团队环境中不能很好地接收。就代码质量而言,您可以针对代码运行工具(静态分析),以便为您提供可能导致问题的地方的建议。 (Android Static Analysis