3

What's the most idiomatic way to apply various transformations to a single variable in succession?

Here's an example. The following is a code snippet from my implementation of the cryptography algorithm AES:

subkeys = key_schedule(NUMBER_OF_ROUNDS)
matrix = binary_to_matrix(input_block)
matrix = add_round_key(matrix, subkeys[0])
for round in xrange(1, NUMBER_OF_ROUNDS):
    matrix = sub_bytes(matrix)
    matrix = shift_rows(matrix)
    matrix = mix_columns(matrix)
    matrix = add_round_key(matrix, subkeys[round])
matrix = sub_bytes(matrix)
matrix = shift_rows(matrix)
matrix = add_round_key(matrix, subkeys[-1])

Notice how the variable matrix get assigned to over and over again. How would you refactor this section of code to avoid this multi-assignment?

EDIT: One could perhaps approach the problem in this way:

for transformation in [sub_bytes, shift_rows, mix_columns, ...]:
    matrix = transformation(matrix)

But this method doesn't help with functions that take multiple arguments like add_round_key (unless perhaps one used currying).

ElliotPenson
  • 354
  • 1
  • 10
  • 2
    I wouldn't. I use that pattern to reinforce the idea that the data is constant, only the representation changes: `for line in fp: line = line.split(); line = [int(item) for item in line]` – Robᵩ Apr 05 '16 at 22:26
  • I'd change the name of `matrix` to something that expresses that it's a temporary value like `temp_calc_mat`, and then assign a more descriptive name for when the calculations are done that represents the use of the final matrix (`matrix` is pretty much the worse name for that, we want to know what's the role of the matrix) – Jules G.M. Apr 05 '16 at 22:31
  • Maybe use the `Pipe` module as in http://stackoverflow.com/questions/12172934/method-chaining-in-python ? – Lauro Moura Apr 05 '16 at 22:31
  • in [..., lambda m: add_round_key(m, subkeys[round])] – iced Apr 05 '16 at 22:32
  • it's highly subjective; what you already have is easy to read, don't go crazy on this – Jules G.M. Apr 05 '16 at 22:34
  • you are right, I deleted my comment – Jules G.M. Apr 05 '16 at 22:36
  • Is there any particular reason you want to avoid multiple assignments? Those assignments aren't performing expensive copies or anything like that. – user2357112 Apr 05 '16 at 22:38
  • The best code is not necessarily the shortest or free of all duplication. Don't obsess over making code minimal, because this is not the same as readable. Right now an idiot can read your code and that's a very good thing. It's obvious at a glance what's generally happening. Another quality I like is that it's very easy to change or to insert debugging statements in between. – Alex Hall Apr 05 '16 at 22:42
  • 1
    If you find yourself doing this a lot, with variations in the functions you call, I think [function composition](https://mathieularose.com/function-composition-in-python/) could be an elegant solution. For just this single case, putting that exact code in a function seems simpler and easier to understand. – Paulo Almeida Apr 05 '16 at 22:44

0 Answers0