DEV Community

Discussion on: Area of intersecting circles

Collapse
 
moopet profile image
Ben Sinclair • Edited

No real comments on making it more optimal, but I'd press them for some code cleanliness.

I'd consolidate my variable names if I were you. You use r1, rad1, radiusCircle1, rad1sqr and radius - and d and distance for that matter. I'd not be able to guess whether "d" meant distance or diameter without inspecting the code further.

The position of the numbers in your variable names is inconsistent meaning that it takes me a second longer to parse between rad1sqr (lowercase, number in middle) and radiusCircle1 (camelCase, number at end)

You switch between lowercase and camelCase for function names, sometimes abbreviating parts of compound names ("calc") and sometimes opting for the more verbose.

I'd probably make the first function shorter:

def smallercirclearea(r1, r2):
    return math.pi * (min(r1, r2) ** 2)


`
because the additional variable doesn't really help with readability, and this replacement follows the familiar pi - r - squared more closely. In fact, I'd be tempted to split it into

python
def circleArea(radius):
"""Calculate the area of a circle from its radius."""
return math.pi * (radius ** 2)
``
and call it with

python
circleArea(min(r1, r2))
``

when you need the smaller area, so that everything is a little more general and it can be used to get the larger area without having to duplicate the function. I've replaced the comment with a docstring so you can inspect it with other tools - in the simplest case in a debugger you can ask for "help circleArea" and it will come back with that message.

You cast the program's arguments to integers but the only place you use them you re-cast them to floats. You don't need to do this; either ints or floats will work fine.

I'd also suggest putting the main code inside a main function, which isn't absolutely required but it's traditional.

Collapse
 
bacchu profile image
Triv Prasad

Thank you. Appreciate the detailed feedback.