Skip to content
Physics and Astronomy
Home Our Teaching Resources C programming silly1.html
Back to top

A silly example

These two fragments of code bothcalculate the distance from the origin of a point on a plane with gradient 1 in both the x and y directions, but one is much clearer than the other:

Clear

  double x, y, z, distance;

  x = 2.1;
  y = 1.3;
  z = x + y;
  distance = sqrt(x*x + y*y + z*z);

Unclear

  double variable1, variable2, variable3;
  double I_feel_like_a_banana;

  variable1 = 2.1;
  variable2 = 1.3;
  variable3 = variable1 + variable2;
  i_feel_like_a_banana = sqrt(variable1*variable1 
    + variable2*variable2 + variable3*variable3);
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Share
Back to top