Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
which code is more readable?
#1
i was recoding, today, a file of a project i am working on.  that file had some code like example number 0.  so i decided to recode it like example number 1.  note that if the () are omitted, it is a syntax error where the interpreter is treating it as an assignment of three values (with the middle value being a conditional choice) to two variables, which does not make any sense.

so which of the two examples do you consider to be more readable?

example #0

if remote_hash > local_hash:
    use_key = remote_key
    use_name = remote_name
else:
    use_key = local_key
    use_name = local_name
print( 'using', use_name )
example #1

use_key, use_name = (remote_key, remote_name) if remote_hash > local_hash else (local_key, local_name)
print( 'using', use_name )
Skaaaaaperen........
Quote
#2
The first one is better, it can be read in one glance.
The second one you have to scroll your eyes right along it to figure out what it is doing.
sparkz_alot, metulburr, buran And 1 others like this post
Quote
#3
I also vote for example 0. From "The Zen of python": 

Quote:Readability counts.
Quote
#4
I would vote for using (key,name) tuples instead of separate variables, and then the first choice becomes as palatable as the second.

I'm surprised at the use of '>' on hashes, so either the operator is the wrong one, or your hashes aren't really hashes and your variable names are misleading :)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Quote
#5
Definitely #0. The structure is obvious, so you can just fit what you're reading into the structure. In #1 you have to figure out the structure and what's in the structure.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#6
(Nov-13-2016, 05:16 PM)Ofnuts Wrote: I would vote for using (key,name) tuples instead of separate variables, and then the first choice becomes as palatable as the second.

I'm surprised at the use of '>' on hashes, so either the operator is the wrong one, or your hashes aren't really hashes and your variable names are misleading :)

so, you mean:


if remote_hash > local_hash:
    use_key, use_name = remote_key, remote_name
else:
    use_key, use_name = local_key, local_name
?

what this part of the code is doing is selecting whether remote or local is to be used "at random".  both hosts will be doing this and they need to be "in sync" by making the same choice.  they are symmetrical in the sense that neither has any predominant state or attribute over the other.  each has the other's key.  each knows the other as the remote, so in this sense they are making opposite choices.  the hash for a host is a hash of it's key and name.  both hosts thus can generate the same hashes which provide psuedo-random (weak, but good enough to not be systematically making the same choice too often) decision making.

...
remote_key = secure_key_transfer_from_peer_host()

remote_hash = hashlib.sha224( remote_name + remote_key ).hexdigest()
local_hash = hashlib.sha224( local_name + local_key ).hexdigest()
this hash exists only for making this common choice, so each host is using the same shared key, one or the other
Skaaaaaperen........
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  io.UnsupportedOperation: not readable RedSkeleton007 1 641 Apr-05-2018, 01:53 AM
Last Post: wavic
  Time Difference in Epoch Microseconds then convert to human readable firesh 4 374 Feb-27-2018, 09:08 AM
Last Post: firesh

Forum Jump:


Users browsing this thread: 1 Guest(s)