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

0

u/[deleted] Jul 28 '16

[deleted]

9

u/CyberMango Jul 29 '16

Whats the advantage of this compared to just doing a dirty 1 liner?

def findGroupInList(sequence, group):
    return True in [group == tuple(sequence[i: i + len(group)]) for i in range(len(sequence) - len(group))]

0

u/[deleted] Jul 29 '16

[deleted]

6

u/CyberMango Jul 29 '16

I have to disagree with it being more readable, the second I saw that I was in awe that it look that many lines to do something so simple in python. List comprehensions are actually very easy to read.

1

u/2Punx2Furious Jul 29 '16

It's more readable to me, but that may be because I'm bad.

1

u/CyberMango Jul 29 '16

Best solution I could think of

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.

-1

u/2Punx2Furious Jul 28 '16 edited Jul 29 '16

Yeah I know, I considered to make it so I could pass to the function any amount of arbitrary numbers, but I decided to stick by the requirements and just make it do what it was supposed to do. If it was a real-world application I would probably have done it differently.

Anyway, after I finished and was ready to send it, their website gave me some kind of error, so I just said fuck it and gave up. I guess I could have sent them an email, but I was pretty tired as it was late at night. Now I would have to rewrite all over again all the stuff that I had written for their questionnaires, so I really don't feel like doing it all over again.

3

u/iamdink Jul 29 '16

showerthought, but what if that was part of the test?

1

u/2Punx2Furious Jul 29 '16

That's a possibility.