HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Is this code too messy to send to an employer as a code sample?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thisemployersamplemessytoocodesend

Problem

A prospective employer asked me for a code sample, but unfortunately this is the best project I've done, most of my other programs were very short (~100 lines). Should I send this in/try to refactor it? Or would it be best to send a shorter sample, or quickly write a new project?

```
#!/usr/bin/python

import curses
import letters # Letter scores and distributions
import libsquares # Locations of bonus squares
import random
import re
from optparse import OptionParser

# Command-line options
parser = OptionParser()
parser.add_option("-c", "--challenge", action="store_true", dest="challenge",
default=False, help="Enable challenges (disabled by default)")
parser.add_option("-d", "--dictionary", dest="dict", default="twl",
help="Choose dictionary (pick \"twl\" or \"sowpods\")")
(options, args) = parser.parse_args()

# Choose your dictionary
if options.dict == "sowpods":
dictionary = open('sowpods.txt', 'r') # SOWPODS dictionary - International
else:
dictionary = open('TWL06.txt', 'r') #TWL dictionary - American

# --------------------------------------------
# Defines a "player" class
class Player:
def __init__(player, name):
player.name = name
player.score = 0
player.rack = []
player.index = -1

# Defines a class for a square on the board
class Square:
def __init__(square, x, y, multiplier):
square.x = x
square.y = y
# Note that the multiplier gets set to 1 when a tile is placed
# on the board
square.multiplier = multiplier
square.letter = ""
square.age = -1

# Defines a class for a tile that hasn't yet been placed on the board
class Tile:
def __init__(tile, x, y, letter):
tile.x = x
tile.y = y
tile.letter = letter

# Defines a class for a turn. A turn object gives you the player, the
# tiles used, the squares used on the board, the score of the move,
# and its validity.
class Turn:
def __init_

Solution

I have little experience with python, so I can't offer critique on the finer points of your code, just the big picture. That said, several things stick out to me.

First, you have some large chunks which are duplicated two, three, or even four times (see lines 198-215). Scour through the code and extract these into their own functions.

Second, several of your functions are massively too long: findAndScore(161 lines), playTiles(97 lines), oneTurn(228 lines). findAndScore, once you get rid of the previously mentioned duplication, could easily be broken into two functions: the finding and the scoring. oneTurn is too long simply because it contains all of the input handling. The section within the block starting at line 435 absolutely should be its own function, if not more since it clocks in at 144 lines.

Finally, and this is more observation than critique, your classes do nothing but operate as organized data structures. This isn't necessarily a wrong approach, but it would look good if you were to refactor your code to be a bit more object oriented, especially since this is intended as a code sample.

Context

StackExchange Code Review Q#14801, answer score: 11

Revisions (0)

No revisions yet.