This post aims to explore some tricks to increase code compactness on a low level. Some tricks here may drastically reduce code readability(or even screw code performance and/or the AST), so using any tricks here should be backed up with really solid reasons, otherwise doing so may end up being extremely bad practices(like increasing code compactness for the sake of increasing code compactness).
Whether or not you're going to use any such dirty little tricks, you may want to know them as some other scripts may use some of them and sometimes you may have to read some of those scripts.
Example 1
Example 2
Example 3
Example 4
Example 5
Example 6
Example 7
Example 8
Example 9
Again, using any of these tricks should be backed up with very, very solid reasons, or the increased code compactness could be outweighed by far more important factors.
What do you think about code compactness in general? What do you think about these dirty little code reduction tricks? Do you have more such tricks to share here?
Also, feel free to correct me if I made any mistake 😀
Edit 1: The original Example 1 is wrong. It only works when both a and c only care if they're TrueClass or FalseClass. Thanks Galenmereth(#6) for pointing that out 🙂
Edit 2: The edited Example 1 is still not precise enough. All a, b, c and d are just variables and I should've stated that explicitly. Now they've changed to be var_a, var_b, var_c and var_d respectively. Thanks KilloZapit(#5 in the VXAN version of this topic) for pointing that out 😀
Whether or not you're going to use any such dirty little tricks, you may want to know them as some other scripts may use some of them and sometimes you may have to read some of those scripts.
Example 1
var_a = true if var_bvar_c = false unless var_dIt assigns true to var_a and false to var_c if var_b is a TrueClass and var_d is a FalseClass respectively.
As long as both var_a and var_c only care if they're TrueClass or FalseClass, the below reduced form will work:
var_a ||= var_bvar_c &&= var_dIf var_b is a TrueClass, var_a will be true as now var_a || var_b returns a TrueClass, otherwise the value of var_a will be unchanged as now var_a || var_b returns the "truth value" of var_a; If var_d is a TrueClass, var_c will be unchanged as now var_c && var_d returns the "truth value" of var_c, otherwise var_c will be false as now var_c && var_d returns a FalseClass.
Note: ||= and &&= are so useful that any scripter should at least know what they do.
As long as both var_a and var_c only care if they're TrueClass or FalseClass, the below reduced form will work:
var_a ||= var_bvar_c &&= var_dIf var_b is a TrueClass, var_a will be true as now var_a || var_b returns a TrueClass, otherwise the value of var_a will be unchanged as now var_a || var_b returns the "truth value" of var_a; If var_d is a TrueClass, var_c will be unchanged as now var_c && var_d returns the "truth value" of var_c, otherwise var_c will be false as now var_c && var_d returns a FalseClass.
Note: ||= and &&= are so useful that any scripter should at least know what they do.
$imported ||= {}$imported[:script] = verIt first creates a hash for $imported if it's not already done, then maps the key :script to value ver in $imported.
The below is 1 way to make it more compact:
($imported ||= {})[:script] = verAs contents inside (), which are $imported ||= {}, are run first, ($imported ||= {}) return $imported. Then the key :script will be mapped to value ver in $imported, making it a reduced form of the original codes.
Note: This reduced form is so common among custom RGSS3 scripts that any scripter should at least know what it does. I don't know if the same applies to RGSS and/or RGSS2 though 🙂
The below is 1 way to make it more compact:
($imported ||= {})[:script] = verAs contents inside (), which are $imported ||= {}, are run first, ($imported ||= {}) return $imported. Then the key :script will be mapped to value ver in $imported, making it a reduced form of the original codes.
Note: This reduced form is so common among custom RGSS3 scripts that any scripter should at least know what it does. I don't know if the same applies to RGSS and/or RGSS2 though 🙂
obj_1 ? obj_1 : obj_2It returns obj_1 and obj_2 if obj_1 is a TrueClass and FalseClass respectively.
The below is 1 way to make it more compact:
obj_1 || obj_2If obj_1 is a TrueClass, obj_1 will be returned as now obj_1 || obj_2 is sure to return a TrueClass, otherwise obj_2 will be returned to determine what obj_1 || obj_2 will return. So it's a reduced form of the original code.
Note: The default RMVXA Game_Variables has this method:
#-------------------------------------------------------------------------- # * Get Variable #-------------------------------------------------------------------------- def [](variable_id) @data[variable_id] || 0 endWhich uses this reduction trick.
The below is 1 way to make it more compact:
obj_1 || obj_2If obj_1 is a TrueClass, obj_1 will be returned as now obj_1 || obj_2 is sure to return a TrueClass, otherwise obj_2 will be returned to determine what obj_1 || obj_2 will return. So it's a reduced form of the original code.
Note: The default RMVXA Game_Variables has this method:
#-------------------------------------------------------------------------- # * Get Variable #-------------------------------------------------------------------------- def [](variable_id) @data[variable_id] || 0 endWhich uses this reduction trick.
a += bexpr_1 if a > cIt first increases a by b, then runs expr_1 if a is greater than c.
The below is 1 way to make it more compact:
expr_1 if (a += > cAs contents inside (), which are (a += b ), are run first, (a += b ) returns the new value of a. The that new value of a will be checked if it's greater than c to determine if expr_1 should be run, making it a reduced form of the original codes.
The below is 1 way to make it more compact:
expr_1 if (a += > cAs contents inside (), which are (a += b ), are run first, (a += b ) returns the new value of a. The that new value of a will be checked if it's greater than c to determine if expr_1 should be run, making it a reduced form of the original codes.
a = def_1def_2(a)def_3(a)It first assigns the value returned by def_1 to a, then passes a as the argument of def_2 and def_3.
The below is 1 way to make it more compact:
def_2(a = def_1)def_3(a)As the passed arguments are first run before running the methods passing them, a = def_1 is run before calling def_2. As a still exists after calling def_2, when calling def_3, a still holds that value returned by def_1. So it's a reduced form of the original codes.
The below is 1 way to make it more compact:
def_2(a = def_1)def_3(a)As the passed arguments are first run before running the methods passing them, a = def_1 is run before calling def_2. As a still exists after calling def_2, when calling def_3, a still holds that value returned by def_1. So it's a reduced form of the original codes.
if expr_1 expr_2 expr_3endIt runs expr_2 and expr_3 if expr_1 returns a TrueClass.
As long as expr_2 always returns a TrueClass, the below reduced form will work:
expr_3 if expr_1 && expr_2If expr_1 returns a FalseClass, both expr_2 and expr_3 won't be run as expr_1 && expr_2 are sure to return a FalseClass; If expr_1 returns a TrueClass, expr_2 will be run to determine what expr_1 && expr_2 will return. As expr_2 always return a TrueClass, expr_1 && expr_2 will also return a TrueClass, causing expr_3 to be run.
As long as expr_2 always returns a TrueClass, the below reduced form will work:
expr_3 if expr_1 && expr_2If expr_1 returns a FalseClass, both expr_2 and expr_3 won't be run as expr_1 && expr_2 are sure to return a FalseClass; If expr_1 returns a TrueClass, expr_2 will be run to determine what expr_1 && expr_2 will return. As expr_2 always return a TrueClass, expr_1 && expr_2 will also return a TrueClass, causing expr_3 to be run.
expr_1expr_3 if expr_2First expr_1 is run, then expr_3 will be run if expr_2 returns a TrueClass.
As long as expr_1 always returns a TrueClass, the below reduced form will work:
expr_3 if expr_1 && expr_2First, expr_1 will always be run, causing expr_2 to be run as expr_1 always returns a TrueClass. Then expr_3 will be run if expr_2 returns a TrueClass as now expr_1 && expr_2 returns a TrueClass; Similarly, expr_3 won't be run if expr_2 returns a FalseClass as now expr_1 && expr_2 returns a FalseClass.
As long as expr_1 always returns a FalseClass, the below reduced form will work:
expr_3 if expr_1 || expr_2First, expr_1 will always be run, causing expr_2 to be run as expr_1 always returns a FalseClass. Then expr_3 will be run if expr_2 returns a TrueClass as now expr_1 && expr_2 returns a TrueClass; Similarly, expr_3 won't be run if expr_2 returns a FalseClass as now expr_1 && expr_2 returns a FalseClass.
As long as expr_1 always returns a TrueClass, the below reduced form will work:
expr_3 if expr_1 && expr_2First, expr_1 will always be run, causing expr_2 to be run as expr_1 always returns a TrueClass. Then expr_3 will be run if expr_2 returns a TrueClass as now expr_1 && expr_2 returns a TrueClass; Similarly, expr_3 won't be run if expr_2 returns a FalseClass as now expr_1 && expr_2 returns a FalseClass.
As long as expr_1 always returns a FalseClass, the below reduced form will work:
expr_3 if expr_1 || expr_2First, expr_1 will always be run, causing expr_2 to be run as expr_1 always returns a FalseClass. Then expr_3 will be run if expr_2 returns a TrueClass as now expr_1 && expr_2 returns a TrueClass; Similarly, expr_3 won't be run if expr_2 returns a FalseClass as now expr_1 && expr_2 returns a FalseClass.
old_var = varvar += var_incrementvar_change = var != old_varIt first uses old_var to store the old value or var, then adds var by var_increment, and finally assigns var_change as the truth value of whether the new value of var is different from its old value.
The below is 1 way to make it more compact:
var_change = var != (var += var_increment)First, var != (var += var_increment) is run, and the left hand side, var, returns the old value of var, while the right hand side, (var += var_increment), returns the new value of var, causing the whole check to be checking if the old value of var is different from its new value. Then, the result of the check will be assigned to var_change, making it a reduced form of the original codes.
Note: This reduced form uses 1 less variable as well.
The below is 1 way to make it more compact:
var_change = var != (var += var_increment)First, var != (var += var_increment) is run, and the left hand side, var, returns the old value of var, while the right hand side, (var += var_increment), returns the new value of var, causing the whole check to be checking if the old value of var is different from its new value. Then, the result of the check will be assigned to var_change, making it a reduced form of the original codes.
Note: This reduced form uses 1 less variable as well.
@obj = obj@boolean = trueIt assigns obj to @obj and true to @boolean.
As long as obj is always a TrueClass and @boolean always only cares if it stores a TrueClass or FalseClass, the below reduced form will work:
@boolean = @obj = objIt's because now obj, which is always a TrueClass, is assigned to @boolean, making it a TrueClass.
As long as obj is always a TrueClass and @boolean always only cares if it stores a TrueClass or FalseClass, the below reduced form will work:
@boolean = @obj = objIt's because now obj, which is always a TrueClass, is assigned to @boolean, making it a TrueClass.
What do you think about code compactness in general? What do you think about these dirty little code reduction tricks? Do you have more such tricks to share here?
Also, feel free to correct me if I made any mistake 😀
Edit 1: The original Example 1 is wrong. It only works when both a and c only care if they're TrueClass or FalseClass. Thanks Galenmereth(#6) for pointing that out 🙂
Edit 2: The edited Example 1 is still not precise enough. All a, b, c and d are just variables and I should've stated that explicitly. Now they've changed to be var_a, var_b, var_c and var_d respectively. Thanks KilloZapit(#5 in the VXAN version of this topic) for pointing that out 😀