Feature suggestion: Provide visual cues for readonly/const/non-mutating methods

One of the huge benefits of immutability is being able to reason that an operation is 'safe' because it cannot produce non-local side-effects or that a read/write access is idempotent.  It seems to me that Reharper could provide a very helpful hint here by, for example, rendering const/readonly variables in bold/different color or by doing further analysis to show that a method is non-mutating. To provide a concrete example, for the (bad!) class below it would be highly desirable if references to "Multiply" and "HalfMultiply" had different visual cues so that I didn't have to devote brainpower to worrying that the Multiply method might change the internal state of the multiplier I happen to be using at the time.

I do appreciate that solving this fully is a "hard problem" (TM) but even an incomplete solution that gives assurance over the things you _can_ prove are non-mutating would be beneficial imho.

 


public class Multiplier
{  
private readonly int  _multiplier;
public Multiplier(int m)
{
_multiplier=m;
}
//'good' method - no side-effects :-)   
public int Multiply(int input)
{
return input * _multiplier;
}
//'bad' method - changes internal state of object non-obviously :-(
public int HalfMultiply(int input)
{
_multiplier=_multiplier/2;
return input *_multiplier;
}
}

 

0
1 comment

Even if there was some analysis done on methods marked as [Pure] would be nice.

0

Please sign in to leave a comment.