3
\$\begingroup\$

This code generates a fractal picture. I'm experimenting with using long variable names to see if the code is still readable without comments. I'm a slow reader and loose track a lot and thought to myself why read twice (variable names and comments).

  • (When) is it better to replace comments with longer descriptive variable and function names?
  • Does this code make sense to you?
  • Would it be better off with comments?
from math import *
from graphics import *

def brushes(win, centerx, centery, brushmaximumradius):
    for brush in range(brushmaximumradius, 0, -1):
        spiralrevolutions = 10 * pi 
        spiralanglevector = ((brushmaximumradius - brush) / brushmaximumradius * spiralrevolutions) ** 1.6
        spiralradius= (brushmaximumradius - brush) ** 0.8
        xaxistremble = (spiralradius / 10) * cos(spiralanglevector * 3)
        yaxistremble = (spiralradius / 10) * sin(spiralanglevector * 3)
        xkoordinat = int(spiralradius * cos(spiralanglevector) + xaxistremble) + centerx
        ykoordinat = int(spiralradius * sin(spiralanglevector) + xaxistremble) + centery
        c = Circle(Point(xkoordinat, ykoordinat), brush)
        c.setWidth(0)

        period = 1.3
        perioddisplacement = 0.15
        maximumlightintensity = 223
        lowestlightintensity = 22
        lightintensityatangle = int(sin(brush/brushmaximumradius*pi/2 * period + perioddisplacement) * maximumlightintensity + lowestlightintensity)

        amplitude = amplitudedisplacement = 0.5
        cirkelRGBred = int(lightintensityatangle * (sin(spiralanglevector) * amplitude + amplitudedisplacement))
        cirkelRGBgreen = int(lightintensityatangle * (sin(spiralanglevector + pi/2) * amplitude + amplitudedisplacement))
        cirkelRGBblue = int(lightintensityatangle * (sin(spiralanglevector + pi) * amplitude + amplitudedisplacement))
        c.setFill(color_rgb(cirkelRGBred, cirkelRGBgreen, cirkelRGBblue))
        c.draw(win)

windowwidth = 1350
windowheight = 730
win = GraphWin("My Circle", windowwidth, windowheight)
win.setBackground(color_rgb(55, 55, 55))

centerx = int(windowwidth / 2)
centery = int(windowheight / 2)
windowcornerfitting = 1.04
brushmaximumradius = int(windowcornerfitting * sqrt(centerx**2 + centery**2)) 

brushes(win, centerx, centery, brushmaximumradius)

win.getMouse() 
win.close()

enter image description here

\$\endgroup\$
  • \$\begingroup\$ I'm not convinced that this picture is a fractal. \$\endgroup\$ – 200_success Sep 23 at 14:28
  • \$\begingroup\$ Hi:) How do you mean, in what way? I started to think about the precise definition of a fractal (wich i dont know). Anyhow its a python program that iterates and the picture is the result. \$\endgroup\$ – Progrmming is fun Sep 23 at 14:51
2
\$\begingroup\$

(When) is it better to replace comments with longer descriptive variable and function names?

That's one of the finer arts of programming, and varies by language. Much of the time, code should be clear enough and variable names sensible enough that comments are rarely needed, except for in the important places like method docstrings (which should exist regardless).

Since Python is not a strongly-typed language, it needs a little bit of encouragement to add type information, but it can still be (kind of) done via PEP484. Doing this will further enhance your ability to understand the code without needing to rely on explanatory comments. For example (these are only educated guesses):

def brushes(win: GraphWin, center_x: int, center_y: int, brush_max_radius: float):

Note the underscore convention for Python variable names.

Does this code make sense to you?

I mean... not really? But that's just because I don't understand the fractal algorithm.

Some of the variable names are not English - koordinat? Since most of your other names are English, you should make this English, too. Some people argue that English is the universal language of development. I'm less convinced, but at least you should be internally consistent.

brushes is a confusing name. A method name should be an action - perhaps draw_brushes.

Another trick to get shorter, but still understandable, variable names - drop redundant information. windowwidth is the only width in your code, and it's used right next to the place where it's called, so call it simply WIDTH (capitals because it's a constant).

Would it be better off with comments?

Yes, definitely - but only comments that are useful.

A comment like this:

# spiral revolutions
spiral_revolutions = 10*pi

is useless. However, a comment like this is very important and useful:

"""
This is an implementation of the fractal algorithm xxx from chapter
yyy of Zells' Python Programming.

:param win: The window object to which the fractal will be drawn.
:param center_x: The x-coordinate of the fractal's origin, in window space. 
:param center_y: The y-coordinate of the fractal's origin, in window space.
:param brush_max_radius: The maximum radius of the circles constituting the fractal.
"""
\$\endgroup\$
  • 1
    \$\begingroup\$ It occurd to me that what im asking for is some sort of pointer as to what is common sense and practice so that programs i write can be useful to other programers and i think you gave me some good useful tips. Thank you:) \$\endgroup\$ – Progrmming is fun Sep 23 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.