if {$subport != $name}: '!=' vs. 'ne'

Ryan Schmidt ryandesign at macports.org
Wed Dec 4 23:41:11 PST 2013


On Dec 5, 2013, at 01:10, Mojca Miklavec wrote:

> On Fri, Nov 29, 2013 at 9:06 AM, Ryan Schmidt wrote:
>> 
>>> +if {${subport} != ${name}} {
>> 
>> Remember to use eq and ne for string comparisons instead of == and !=.
> 
> But someone should commit massive changes to the ports then.
> 
> Running "grep -r subport ." shows that just about any given port uses
>    if {$subport != $name}
> 
> (Next time when I would create a new port, I would open a "random"
> other port and copy the line from there, so the same mistake would
> keep proliferating.)

That would be a good idea but identifying all occurrences of the mistake is not easy. Finding ${name} == ${subport} and ${name} != ${subport} and ${subport} == ${name} and ${subport} != ${name} is easy, but any other string comparison should also be changed. But there’s no way to identify which variables are being used and strings and which are being used as numbers without manually looking at each one.

Most of MacPorts base, many of the portgroup, and some of contrib still has this issue too, since the existence of the eq and ne operators was only recently brought to our attention.

Meanwhile, it’s not actually causing any problems. == and != will look at the values, and if they’re numbers, will handle them as numbers, otherwise it will handle them as strings. If you already know they’re strings, you may as well write eq or ne to convey that and to be slightly more efficient about the comparison.

If we can all take a moment and fix a few of these whenever we spot them, that will eventually get us there. Often I sneak this change into another commit when I’m already updating a port. Adding port lint checks for the most common offenders would also remind us about this issue.



More information about the macports-dev mailing list