简体   繁体   English

在公式中键入值是否需要硬编码?

[英]Is it hardcoding to type out values in a formula?

I am working on a class assignment and one of the things covered in this weeks text is hardcoding and how it is frowned upon. 我正在做一个班级作业,本周文字中介绍的内容之一是硬编码及其使用方式。

My question is if I physically put in a value that is part of the formula, is that considered hardcoding? 我的问题是,如果我实际输入的值是公式的一部分,那么是否认为该值是硬编码的?

example: the volume of a sphere is (4/3) * PI * r^3 should I declare (4/3) as variable in the beginning of my block code? 例如:一个球体的体积为(4/3)* PI * r ^ 3我应该在块代码的开头声明(4/3)为变量吗? or should I take it a step further and declare the whole formula? 还是应该更进一步并声明整个公式?

I hope what I am asking makes sense to everyone, and for a little extra information here is the my code: 我希望我的要求对每个人都有意义,这里还有一些额外的信息是我的代码:

  package area_volume_surfacearea;

/**
 * Area_Volume_SurfaceArea (Assignment number 9)
 * For CSCI 111
 * last modified sept 15 12 p.m.
 * @author Jeffrey Quinn
 */
        //import Scanner class
import java.util.Scanner;

public class Area_Volume_SurfaceArea 
{

    /**
     * This method finds the Area, Volume and Surface Area of geometric shapes 
     based off a value that the user inputs (in inches)
     */


    public static void main(String[] args) 
    {
        double distance; //the distance the users inputs to be used in the formulas (raidus or side)
        double areaCircle; //the area of the circle
        double areaSquare; //the area of the square
        double volumeSphere; //the volume of the sphere
        double volumeCube; //the volume of the cube
        double surfaceAreaSphere; // the surface area of the sphere
        double surfaceAreaCube; //the surface area of the cube

        //declare an instance of Scanner class to read the datastream from the keyboard
        Scanner keyboard = new Scanner(System.in);

        //get the value of the radius of the circle (in inches)
        System.out.println("Please enter a distance (in inches) to be used:");
        distance = keyboard.nextDouble();

        //calculate area of a circle (in inches)
        areaCircle = Math.PI * Math.pow(distance,2);

        //calculate area of a square (in inches)
        areaSquare = Math.pow(distance,2);

        //calculate volume of a sphere (in inches)
        volumeSphere = (4/3) * Math.PI * Math.pow(distance,3);

        //calculate volume of a cube (in inches)
        volumeCube = Math.pow(distance,3);

        // calulate surface area of a sphere (in inches)
        surfaceAreaSphere = 4 * Math.PI * Math.pow(distance,2);

        //calculate surface area of a cube (in inches)
        surfaceAreaCube = 6 * Math.pow(distance,2);

        //results
        System.out.println(" The area of a circle with the radius of " + distance + "in, is " + areaCircle);
        System.out.println(" The area of a square whos sides measures " + distance+ "in, is " + areaSquare);
        System.out.println(" The volume of a sphere with the radius of " + distance + "in, is " + volumeSphere);
        System.out.println(" The volume of a cube whos sides measures " + distance + "in, is " + volumeCube);
        System.out.println(" The surface area of a sphere with the radius of " + distance + "in, is " + surfaceAreaSphere);
        System.out.println(" The surface area of a cube whos sides measures " + distance + "in, is " + surfaceAreaCube); 
    } //end main()

}// end class Area_Volume_SurfaceArea

In the spirit of self documenting code you could create a variable: 本着自我记录代码的精神,您可以创建一个变量:

double SPHERE_VOLUME_RATIO = 4.0d/3.0d;

BTW, applying your studies your code is very easy to read. 顺便说一句,应用您的研究代码很容易阅读。

These are formulas that will never change. 这些是永远不会改变的公式。 There is absolutely no issues to hard-code such algorithms. 对此类算法进行硬编码绝对没有问题。 Adding unessary variables will just make your code more complex without giving any benefits. 添加不必要的变量只会使您的代码更复杂,而不会带来任何好处。 However, DRY code is just as important so if you are finding yourself writing this ratio over and over in many places, it could then be a good idea to store it in a variable. 但是,DRY代码同样重要,因此,如果您发现自己在许多地方一遍又一遍地编写此比率,那么将其存储在变量中可能是个好主意。 For instance, if I had to use the number 666 at many places in my code, even if that number would never ever change, I would say it's better to define it as a constant since it's less error-prone. 例如,如果我不得不在代码的许多地方使用数字666 ,即使该数字永远不会改变,我会说最好将其定义为常量,因为它不容易出错。

final int NUMBER_OF_THE_BEAST = 666;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM