RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:9:30-18:00
你可能遇到了下面的问题
关闭右侧工具栏
Scientific Computing in Java
  • 作者:xiaoxiao
  • 发表时间:2020-12-23 11:03
  • 来源:未知

Scientific Computing in Java (Part 2): Writing Scientific Programs in JavaBy Ken Ritley

We live in a technological world, at the heart of which are scientists and engineers. They need programming tools to help make important discoveries and bring the next generation of technology to market.

Part 1 of this article discussed how scientists can benefit from Java. We've said that despite a few pitfalls which scientists should watch out for, the future of Java as a scientific programming language looks bright. Here in Part II we examine the structure of a scientific program more closely. We'll define a few scientific OOP design patterns in Java, and we'll give you a short style guide that can help scientists write good Java programs (please see the sidebar "A Style Guide for Scientific Programs in Java").

A Style Guide for Scientific Programs in Java

The Golden Rule of Programming states that computer programs should be understandable by the people who write and maintain them. But what's good programming practice for business is not necessarily what's good for science.

Here are some ideas scientists can use for making Java programs more readable and easier to maintain.

Translating Formulas to Source Code

1. Use short variable names like e, m and c, to mirror the variables which appear in the original equations.

Clearly e = m*c*c; is much easier to compare with the original equation and debug than energy=mass*speedOfLight*speedOfLight;.

2. Hungarian notation in Java can be helpful.

It's not necessary to precede every variable with a letter which indicates its type: d=double, i=integer, etc. But because Java convention is to start variable names with lowercase letters, sometimes Hungarian notation can be more aesthetically pleasing, especially for loop control variables; for example iNumberOfElectrons vs. numberOfElectrons. It also helps make clear when a seemingly integer-like variable has been converted to a different type, as in double dNumberOfElectrons = (double) iNumberOfElectrons;.

3. Use descriptive variable names for controlling loops and all other variables.

Remember when looking through source code, loops are what the eye sees first. It's important that the physical meaning of the loop be clear. For example,

// Eq. (5) Loop over all electrons for (i = 0; i < iNumberOfElectrons; i++) E[i] = m[i]*c*c;