c# - Using ref - Good programming practice or not -
is using ref
bad programming practice? doing refactoring of old code uses ref lot. have turned on code analysis microsoft rules set , rules "do not pass types reference"
cause: public or protected method in public type has ref parameter takes primitive type, reference type, or value type not 1 of built-in types.
why bad? can use them in private methods or internal methods? using ref in private/internal methods programming practice or nor?
edit: here samples,
public void doautoscrollreverse(rectangle rectangle, int xposition, int yposition, ref int deltax, ref int deltay) { } public bool getpointcoords(graphics g, point pmouse, displayblock2d ablock, ref point3md pt, ref displaypoint2d 2dpoint, ref double gappos) { }
what happening inside these function being initialized, set, or whatever.
update: why using ref? not. old code need refactor. got rid of of methods, complex functions in second example given cannot. there function returns bool (tells operation successful or not) , has 3 ref values of different objects. should here? make private/internal (is using ref in private/internal practice?)
i think better not use ref unless necessary. can lead errors hard detect if local variables can changed in other methods.
you examples can rewrite don't need use ref anymore.
public void doautoscrollreverse(rectangle rectangle, int xposition, int yposition, ref int deltax, ref int deltay) { }
create small class:
public class delta { int x { get; set; } int y { get; set; } }
without ref:
public delta doautoscrollreverse(rectangle rectangle, int xposition, int yposition) { return new delta(deltax, deltay); }
now more clear returning , what returning.
Comments
Post a Comment