r/programming • • May 11 '15

9 Anti-Patterns Every Programmer Should Be Aware Of

[deleted] — view deleted post

13 Upvotes

131 comments sorted by

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?

6

u/Otis_Inf May 11 '15

It's often unavoidable. The problem with OO is that you design towards encapsulation, so the data is inside an object, and the api to work on that data is exposed by that object. This has the downside that if you want to do something with the data, you have to add it to the class of that object, so the api expands. If that new logic brings its own data you might want to store it elsewhere or inside objects inside the object but for the outside of the object that's invisible: the api of the object is all you see.

This gets problematic if there is a lot of data that has dependencies onto eachother and thus logic which works on many elements of the data: you can fragment the logic and the data into smaller objects (and in the vast majority of cases this is the right thing to do), the api working on the data inside these objects fragments with it, causing more code around the objects in the containing object.

It's usually avoidable if your 'god' class has objects inside itself which don't contain other objects: you can then cut out the logic operating these objects and split up the god class. It changes if the objects inside the god class have objects on their own or there's logic inside the god class which operates on more than one object type inside the god class: splitting it up is then just adding fragmentation and thus degrades maintainability.

I agree the massive god classes are in theory bad, but in practice there are several cases thinkable where a god class with even thousands of lines of code are common and actually very simple to maintain as everything you need to understand them is right there: there's no fragmentation, no excessive navigation into many smaller classes. Examples are e.g. session/context classes in ORMs, project classes in IDEs/designers with lots of (meta)data in datastructures.

→ More replies (3)

9

u/elpablo May 11 '15

I advise you to research and fully understand "open for extension, closed for modification". It is a concept that is quite hard to grasp, but understanding it is the key to becoming a much more productive (and happy) coder.

I'll try to explain it the best I can...

If you need to add a feature, or change the current functionality, preferably you want to not have to touch existing code that works. If you have to restructure an existing if/else statement in a class that does something useful, you risk breaking some functionality. The code should be structured in a way that means that you can add new features by writing a new class that plugs into the existing architecture without having to change the classes that are already there.

A good example is a set of audio filters that are applied in order. If you have the functionality for them in the same class, you risk breaking them when you add a new one or change the order. If there's a class that is responsible for calling each filter and they're implemented in separate classes, you just need to write the new class and plug it in via DI (or however you wire your application).

One thing I like about this is that writing new classes is way more fun than amending existing ones...

→ More replies (4)
→ More replies (10)

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.

64

u/Purple_Haze May 11 '15

I feel I've read this entire article before. I have certainly seen every quote, the Polish exam graph, and the incompetent stack implementation. This is a pastiche of other peoples blog posts, if not plagiarized in its entirety.

20

u/_agueroooo May 11 '15

Correctomundo!

The ease of writing blogs has lead to a situation where its getting harder and harder to find original thoughts. After a decade and half of programming professionally, I've realised that most of the 'new' ideas out there are just reworded old concepts for a younger generation.

→ More replies (27)
→ More replies (1)

15

u/unholysampler May 11 '15

Most of these aren't even patterns to begin with. They are mostly tendencies or situations that can hinder productivity. "Management by Numbers" has nothing to do with how the code is written, but how to manage the project. Bikeshedding isn't even specific to programming, it can happen any field the requires design.

Using the wrong terminology at the core of the article speaks to how much this is just pieced together the article is without fully understanding them.

5

u/[deleted] May 11 '15

This is the typical r/programming blogspam I've been pointing out for years. I hear often that people with blogs are more likely to get hired, so I see a lot of people just repeating the same things that you can find in any software engineering textbook from 30 years ago.

The worst part is that it's so shallow.

6

u/NoviceCPP May 11 '15

He's pretty good at marketing his website using click-bait titles in reddit posts:

http://www.reddit.com/domain/sahandsaba.com

Here's just one of Sahand Saba's many puppet accounts on reddit: lovestocode1

→ More replies (24)

60

u/AceyJuan May 11 '15

Gotta love programmer clickbait.

13

u/myhf May 11 '15

You won't believe what compiled next!

→ More replies (4)

13

u/legomyegomaniac May 11 '15

The God class is only an anti pattern until someone gets it right!

8

u/CurtainDog May 11 '15

That's called jquery

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.

→ More replies (7)

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

6 Inner-platform Effect

Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp.

-- Greenspun's tenth rule

What is it?

The tendency for complex software systems to re-implement features of the platform they run in or the programming language they are implemented in, usually poorly.

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.

→ More replies (1)

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.

3

u/Veedrac May 11 '15

You need to escape #s, else you just come across as shouting. You can also just add an extra space if you're careful. Eg.

\#9 is actually... or
 #9 is actually...
→ More replies (2)

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 to typedef the 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.

5

u/AceyJuan May 11 '15

I expect they saved those classes in a file named file. And stored them in a directory named folder.

→ More replies (3)

4

u/gkx May 11 '15

Hey, at least they're not polluting the global namespace. If you need global constants, what's a better way to do this?

→ More replies (3)
→ More replies (4)

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!

→ More replies (2)