12.07.2015 Views

ILOG CPLEX 11.0 User's Manual

ILOG CPLEX 11.0 User's Manual

ILOG CPLEX 11.0 User's Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Avoid Side-EffectsIt is good idea to minimize side-effects by avoiding expressions that produce internal effects.In C, for example, try to avoid expressions of this form:a = c + (d = e*f); /* A BAD IDEA */where the expression assigns the values of d and a.Don’t Change Argument ValuesA user-defined function should not change the values of its arguments. Do not use anargument to a function on the lefthand side of an assignment statement in that function.Since C and C++ pass arguments by value, treat the arguments strictly as values; do notchange them inside a function.Declare the Type of Return ValuesAlways declare the return type of functions explicitly. Though C has a “historical tradition”of making the default return type of all functions int, it is a good idea to declare explicitlythe return type of functions that return a value, and to use void for procedures that do notreturn a value.Manage the Flow of Your CodeUse only one return statement in any function. Limit your use of break statements to theinside of switch statements. In C, do not use continue statements and limit your use ofgoto statements to exit conditions that branch to the end of a function. Handle errorconditions in C++ with a try/catch block and in C with a goto statement that transferscontrol to the end of the function so that your functions have only one exit point.In other words, control the flow of your functions so that each block has one entry point andone exit point. This “one way in, one way out” rule makes code easier to read and debug.Localize VariablesAvoid global variables at all costs. Code that exploits global variables invariably producesside-effects which in turn make the code harder to debug. Global variables also set uppeculiar reactions that make it difficult to include your code successfully within otherapplications. Also global variables preclude multithreading unless you invoke lockingtechniques. As an alternative to global variables, pass arguments down from one function toanother.Name Your ConstantsScalars—both numbers and characters—that remain constant throughout your applicationshould be named. For example, if your application includes a value such as 1000, create aconstant with the #define statement to name it. If the value ever changes in the future, itsoccurrences will be easy to find and modify as a named constant.<strong>ILOG</strong> <strong>CPLEX</strong> <strong>11.0</strong> — USER’ S MANUAL 137

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!