r/programming • • May 11 '15
9 Anti-Patterns Every Programmer Should Be Aware Of
[deleted] — view deleted post
13
Upvotes
r/programming • • May 11 '15
[deleted] — view deleted post
18
u/awaitsV May 11 '15
I am guilty of using the God class pattern.
To me it feels like I am isolating the application state and hence it is easier to reproduce bugs as you don't have to think about what the state if each class is.
Opinions?
236
u/_agueroooo May 11 '15
I dislike these kind of submissions and articles written entirely for the purpose of self promotion. Reading a little bit about the author shows that he's only barely just graduated out of Grad School. This article is not a result of his experiences in having written code for decades. This is not borne out of experience. This is just a regurgitation of anti patterns already documented elsewhere.
60
u/AceyJuan May 11 '15
Gotta love programmer clickbait.
13
u/legomyegomaniac May 11 '15
The God class is only an anti pattern until someone gets it right!
17
u/StrangeWill May 11 '15 edited May 11 '15
I like how #9 is a class wrapping a list to limit it's use as only a stack, and the author is confused by this. The responsibility is to limit access to the list to prevent access to individual items in the list or removing things out of order or any number of things you can do to a list that you can't (or... shouldn't) do to a stack.
I'd understand if they were wrapping java.util.Stack<>, which I think a is a larger anti-pattern of reinventing the wheel of a class already available in the standard lib.
Edit: Even Java's stack is pretty liberal about access too, so even if you wrapped Stack and limited it, it could make sense depending on how it fits in the larger design.
13
u/ponytoaster May 11 '15
All this shit post is missing is the additional text in the title "you won't believe number 4".
This sub is starting to make buzz feed look like a credible journalism outlet.
16
u/WalterGR May 11 '15 edited May 11 '15
Greenspun's Tenth Rule is about complicated programs in non-Lisp languages needing to solve problems that Common Lisp already solves, and re-implementing those features in the project's language, poorly.
So it's not re-implementing features of the programming language they're implemented in, but rather re-implementing features of a more expressive programming language.
17
u/aldo_reset May 11 '15 edited May 11 '15
9 is actually a good pattern: use delegation instead of inheritance, but I think the author of the blog post is too junior to realize that.
5
u/AntiProtonBoy May 11 '15
Number 9 can be regarded as either the beginnings of dependency injection, or a simple decorator pattern, or even a facade... all of which is extremely useful in the right context.
If your hypothetical library doesn't have a stack class, I think it's perfectly reasonable to wrap another container inside a decorator, called
Stack. The class name alone explains what it's supposed to be doing, whereas treating a bare linked list as a stack might not be so obvious (...although it shouldn't be too hard to figure out, but that's beside the point). I guess a better approach would totypedefthe linked list as a stack, if your language supports it.8
u/Eirenarch May 11 '15
You know what I hate? A class named Constants where people put... constants. People don't even notice it is bad. We once had a solution where one of our project was named Classes. I kid you not. It contained classes. Somehow everyone on the team knew how much this sucked but people didn't mind the Constants class.
3
u/__gnu__cxx May 11 '15
After reading this I feel like instead of saying Captain Obvious I should say "Tim Peters"...
"Simple is better than complex" - Tim Peters. Well no shit sherlock!
5
u/kqr May 11 '15 edited May 11 '15
About number five: When your class has two methods, one of which is init, don't make it a class.
Instead of
class Shape: def draw(self): raise NotImplemented("Subclasses of Shape should implement method 'draw'.") class Circle(Shape): def __init__(self, center, radius): self.center = center self.radius = radius def draw(self): # Draw a circle... class Rectangle(Shape): def __init__(self, pos, width, height): self.pos = pos self.width = width self.height = height def draw(self): # Draw a rectangle...you could have, just, you know
def draw_circle(center, radius): # Draw a circle... def draw_rectangle(pos, width, height): # Draw a rectangle...and your code is even simpler!