[Tutor] Finding biggest of three numbers
Alan Gauld
alan.gauld at yahoo.co.uk
Wed Oct 7 04:31:39 EDT 2020
On 07/10/2020 04:03, Manprit Singh wrote:
> clean and clear solution to this problem is using max( ) function.
Correct so that is the correct solution.
But for the sake of debate, let's assume max didn't exist....
> if a > b and a > c:
> x = a
> elif b > c:
> x = b
> else:
> x = c
> if b < a > c:
> x = a
> elif b > c:
> x = b
> else:
> x = c
> Here in the second version, I have used chaining in the condition written
> with if .
The two versions are identical except the initial if.
> So what are your views about this second version, which one must
> be preferred ? - second or first version of the solution to the problem .
Which is the most readable.
The second version explicitly states the requirement and eliminates
unneeded logic operations.
It's both clearer and more performant.
It is therefore preferable.
> Just need to know in which circumstances I should prefer chaining, and in
> which circumstances I should avoid it .
Use it when it is more readable. There are no absolutes and sometimes
it is a subjective choice.
But, as I previously stated, our goal is always to write code
that is clean, readable and reliable, and therefore maintainable.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list