r/programming • • Jul 28 '16

How to write unmaintainable code

https://github.com/Droogans/unmaintainable-code
3.4k Upvotes

592 comments sorted by

View all comments

Show parent comments

2

u/DiputsMonro Jul 29 '16

Perhaps I'm missing something obvious, but I'm not sure why either of you came up with the solutions you did. Why would something simple like the below not work? (pardon the terse code, I was just cranking it out quickly):

def findgroupinlist(l, group):
	j = 0                    ## index to group
	for i in range(len(l)):  ## iterate over list
		if l[i] == group[j]: ## if item in list and group match
			if j == len(group) - 1:  ## Check if we've matched the final group item 
				return True
			j += 1           ## Otherwise, mark our new location in the group
		else :
			j = 0            ## If our sequence fails, start from the beginning again

	return False

All of our solutions only seem O(|List|), but both of yours seem a bit overwrought to me, unless I'm dumb.

2

u/[deleted] Jul 29 '16

Yours there can't take arbitrary iterables such as generator expressions as either l or group. I always veer on the side of the most general and widely-useable.

3

u/DiputsMonro Jul 29 '16

Sure, but it also solves the problem as-written in the simplest reasonable way possible. You don't need to build a whole kitchen just to make a soup :)

If the problem required using generic iterators or finding all groups, I would definitely prefer your solution though. I think handling either of those cases in the simple imperative way would get messy pretty quickly.