[Tutor] Discussion about Chaining in comparison operators

Alan Gauld alan.gauld at yahoo.co.uk
Tue Oct 6 19:57:28 EDT 2020


On 06/10/2020 18:32, Manprit Singh wrote:

> now if i have to check that if all three are equal or not, what should i
> prefer from the below given choices :
> 
> 1) a == b  and a == c
> or it chained version
> b == a == c
> 
> 2)   a == b and b == c
> or its chained version
>  a == b == c
> 
> Since a == b == c seems more  readable than  b == a == c, I will prefer
> this .

Our objective should *always* be to write the most readable(*)
code we can.

So yes, a==b==c wins.

(*) The reason for this is that in any serious code, maintenance
accounts for 80% of the total project cost. Therefore anything
which improves the maintainability of the code is a big win
 - and readability makes code much more maintainable.

For example, if a bit of code increases coding time by 10% but
reduces maintenance costs by 10% the net effect is +2% in
development but -8% in maintenance = an overall 6% reduction
in cost! Software engineering, like any other kind of engineering,
is all about building solutions at lowest cost.
The rare exception to this rule is where performance is critical
and maintainability may need to be compromised, but that hardly ever
happens in the real world because there are usually better solutions
to fixing performance.

-- 
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