Reduce implicit parameter passing

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(New page: '''Reduce implicit parameter passing''' This rule suggests method should always use explicit parameters. In some situation, global variables are used as implicit parameter among methods w...)
 
(Example)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''Reduce implicit parameter passing'''
+
This rule suggests methods should always use explicit parameters. In some situations, instance variables are treated like global variables and are used as implicit parameters among methods within the same class. Implicit parameters tie methods to the class, and it also hides the necessary information from outsiders who wish to use the method.
  
This rule suggests method should always use explicit parameters. In some situation, global variables are used as implicit parameter among methods within the same class. Implicit parameter ties methods with the class, and it also hide the necessary information for outsiders who wish to use the method. The results of using implicit parameter are (1) making break a class more diffcult, or (2) even possible to lead other programmers to miuse method (since parameters are implicit passed in which case may not be awared by other programmers).
+
The undesired results of using implicit parameters are  
 +
* making it difficult to split a class into parts
 +
* leading other programmers to misuse methods as they may not be aware of all implicit parameters
 +
 
 +
Changing the implicit parameters to explicit parameters will overcome these problems, although [[Reduce the number of arguments|beware of too many parameters]].
 +
 
 +
== Example ==
 +
The example shows a configuration class and a program which uses these settings. The first example works with the config object as an implicit parameter (passed to the program by calling setConfig()), the second example passes the config object to the functions that need the config settings. Depending on the amount of parameters, execute() or step() of the explicit example might work better (see also [[Long parameter list smell]]).
 +
 
 +
[[Image:ImplicitExplicitParams.png]]
 +
 
 +
[[Category: Johnson and Foote's heuristics]]

Latest revision as of 22:28, 18 August 2009

This rule suggests methods should always use explicit parameters. In some situations, instance variables are treated like global variables and are used as implicit parameters among methods within the same class. Implicit parameters tie methods to the class, and it also hides the necessary information from outsiders who wish to use the method.

The undesired results of using implicit parameters are

  • making it difficult to split a class into parts
  • leading other programmers to misuse methods as they may not be aware of all implicit parameters

Changing the implicit parameters to explicit parameters will overcome these problems, although beware of too many parameters.

Example

The example shows a configuration class and a program which uses these settings. The first example works with the config object as an implicit parameter (passed to the program by calling setConfig()), the second example passes the config object to the functions that need the config settings. Depending on the amount of parameters, execute() or step() of the explicit example might work better (see also Long parameter list smell).

ImplicitExplicitParams.png

Personal tools