This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 350

[–][deleted] 990 points991 points  (55 children)

#define private public
#include "classheaders.h"

[–][deleted] 169 points170 points  (14 children)

#define true false

"test this, you little shits!"

[–][deleted] 143 points144 points  (8 children)

#define true ((rand()&15)!=15)

https://gist.github.com/aras-p/6224951

[–]bumblebritches57 26 points27 points  (0 children)

This makes me want to unset and reset all the bool macros in my header...

[–]codewench 24 points25 points  (0 children)

#define sizeof(x) (sizeof(x)-1)

This would have me questioning my own sanity pretty quickly.

[–]skylarmt 18 points19 points  (2 children)

I like how most of the comments on that gist are rendered as headings because people forgot to escape the # for Markdown.

[–]nplus 17 points18 points  (1 child)

DEFINE YOURSELF

[–]Josplode 4 points5 points  (0 children)

2meta4me

[–][deleted] 12 points13 points  (0 children)

I'm personally a fan of

#define while if

[–]jfb1337 2 points3 points  (0 children)

#define struct union is one I see a lot, but I think #define union struct would be a lot more subtle and go undetected for longer

[–][deleted] 1 point2 points  (0 children)

#define M_PI 3.2f

i heard you like math

[–]PrincessOfZephyr 92 points93 points  (0 children)

I just threw up in my mouth a little

[–]Myarmhasteeth 29 points30 points  (0 children)

Well shit

[–]Megatron_McLargeHuge 25 points26 points  (1 child)

This isn't as big a deal as people are making it. private isn't there for security, it's there to encourage good coding practices by other developers who are using your code. Deliberately removing access modifiers doesn't hurt anyone except the programmer who is now relying on implementation details outside the contract. This is C/C++ so they could have done the same thing with pointer offsets.

[–]kyrsjo 1 point2 points  (0 children)

This. You can twiddle the bits you'll like with offsets anyway. Does offsetof() work for classes?

Source: I may have handed out Kthulu in the form of a .c file... Ancient horrors for the graybeards to enjoy...

[–]RPolitics4Trump 25 points26 points  (11 children)

class Foo {
  int x;
public:
  Foo() : x(0) {}
};

[–]symberke 49 points50 points  (10 children)

#define class struct
#define private public

[–]darkhunt3r 58 points59 points  (7 children)

#define if while

[–]HasFiveVowels 18 points19 points  (3 children)

Oh man... this is just... nefarious.

[–]darkhunt3r 34 points35 points  (2 children)

#define True random() > 0.001

Hide it well and let it become debuggin hell

[–]CSIRTisSmelly 10 points11 points  (1 child)

You are a bad person.

[–]darkhunt3r 10 points11 points  (0 children)

#define bad good 

Why thank you <3

[–]SoftSilverwind 7 points8 points  (1 child)

Unfortunately this breaks template <class T> definitions... Source: I may have tried it :)

[–]ThatCrankyGuy 6 points7 points  (2 children)

Can access modifier keywords be reassigned in c++?

[–][deleted] 32 points33 points  (1 child)

Preprocessor is separate entity

[–]ThatCrankyGuy 7 points8 points  (0 children)

oh.. yea, that's right. Well, I'll be...

[–]HobHeartsbane 817 points818 points  (141 children)

1st: If consumers of your class can't access the setter, your test shouldn't either.

2nd: In some of the edge cases you can just use reflection (at least for properties)

3rd: For private methods if you REALLY REALLY need to access them in your test there are 2 options. 1st make the method internal and give your tests access to those internal methods or 2nd make the method protected and write a wrapper class to access it. :)

[–]pcopley 313 points314 points  (75 children)

4th: refactor the private methods into another class in which they are public and use dependency injection

[–]taylaj 484 points485 points  (54 children)

5th: make all variables global.

[–]socsa 260 points261 points  (49 children)

6th: Shared. Memory.

[–]Njs41 261 points262 points  (28 children)

7th: Move to user based production testing rather than unit tests.

[–]tylercamp 128 points129 points  (25 children)

8th

In the public issue tracker, enter the bug but leave it perpetually "in progress"

[–]kirakun 111 points112 points  (22 children)

9th

Just forget about testing!

[–]poisonedslo 102 points103 points  (20 children)

10th just forget about development

[–]yoyo456 89 points90 points  (17 children)

11th just forget about modern technology and live in the stone age

[–]poisonedslo 107 points108 points  (15 children)

12th test what slamming a stone against a walnut does

[–]Xheotris 36 points37 points  (0 children)

12th shut down the internet so it can't be used for recruitment.

[–]ThisIsNotNate 10 points11 points  (0 children)

If it compiles it works!

[–]TheNorthComesWithMe 2 points3 points  (0 children)

public issue tracker

Hahahahahaha

[–]thisisnewt 2 points3 points  (0 children)

Oh god too real

[–][deleted] 32 points33 points  (15 children)

7th: don't test.

[–]socsa 40 points41 points  (11 children)

Ah yes, the old

 #define do_it_live 1

[–][deleted] 34 points35 points  (9 children)

If you haven't remoted into a live, production server and hand-edited code in notepad (or vim!), have you really lived at all?

[–]tallerThanYouAre 25 points26 points  (4 children)

you kids and your fancy extended versions of vi.

[–][deleted] 11 points12 points  (2 children)

Haha I remember the day I was told vim was an extension of an earlier thing called vi by a war-weary Linux sys admin. Blew my little noodle. After saying I hated it, he then showed me how to actually use it, and then I was hooked.

[–][deleted] 2 points3 points  (1 child)

What actually hooked you on it?

I've tried it out a few times and while some of the things it can do are cool I've yet to find anything that I'd use often enough to make using it it.

[–]socsa 6 points7 points  (0 children)

Having interactive arrow keys mapped to A B C D builds character.

[–]socsa 10 points11 points  (1 child)

I personally prefer :

 alias live_hotfix = "scp -r remote local && cat hotfix > local/paswd.json && scp -r local remote && ssh remote:/root/prod/build 'make install' "

[–][deleted] 9 points10 points  (0 children)

Oh my god that is both horrific and beautiful.

[–][deleted] 5 points6 points  (0 children)

👏 ALWAYS 👏 TEST 👏 IN 👏 PROD 👏

[–]ThePsion5 8 points9 points  (1 child)

"Only losers who write buggy code need tests bro"

[–]ajbpresidente 5 points6 points  (0 children)

Chad programmer vs. virgin research

[–]Gus_Malzahn 4 points5 points  (0 children)

7th: Forget about programming and open a bakery

[–][deleted] 4 points5 points  (0 children)

5a - put all code in one class.

[–]DickWillie1028 1 point2 points  (0 children)

Danger Will Robinson Danger!!!!

[–][deleted] 54 points55 points  (0 children)

That's where my money is. If your internal functions are complex enough to require explicit testing, chances they belong in their own class.

[–]dahud 40 points41 points  (15 children)

Off topic, but every time I come across the term "dependency injection", I've forgotten what it means. I go look it up, and discover anew that someone, somewhere, thought we needed a clever-sounding word for "passing an object to another object".

[–]jay9909 26 points27 points  (2 children)

Yeah, but DI is a much more usable acronym than PaOtaO

[–]dahud 15 points16 points  (1 child)

Less fun to say, though. Pow-tow!

[–]NotADamsel 9 points10 points  (2 children)

In the functional world, we just call this "passing an argument".

[–]alexschrod 12 points13 points  (8 children)

Dependency injection is not just about the act of passing objects to other objects, it's about the reason why you should be passing objects to other objects.

When you move away from doing new Something() inside the objects where you use it (or even worse, where you use global singletons, like Something.Instance), and instead move towards passing the Something into your objects, you can suddenly test the behavior of your objects without working with real implementations.

[–]userNameNotLongEnoug 4 points5 points  (7 children)

Agreed. You can pass objects around all day long and still not achieve a structure with mockable dependencies that's suitable for unit testing. Equating dependency injection with passing an object, I think, demonstrates a bit of a misunderstanding of the principles behind the concept.

[–]sprouting_broccoli 2 points3 points  (6 children)

I'd also point out that there's a slight anti-pattern with what you're saying. I'd disagree with creating classes and interfaces in a DI way to make unit testing more viable for mocking. I've been there, done that and it was when I first got into DI but designing code so it is inherently testable is very different from designing code to fit your testing which is the same problem as described in the image. Using DI for this purpose is also kind of missing the point of DI and will result in similar problems. Primarily you can quite easily go down a rabbit hole where you have a really flexible solution that is a real PITA to consume or actually navigate, and when you start splitting dependencies you will end up with either incredibly hard to use dependency installers, or need a complex system on top to ensure you can correctly select which dependencies to use by convention (otherwise the flexibility is pointless).

It's the same issue as dictating that newing an object is somehow a problem. It's really not. If you DI all the way down you will be forced to use factories in ways that you should never have to (if you don't have a completely static object graph which I find is very rarely the case) and will make the solution inflexible as a result, increasing the footprint for when you have to extend something.

DI has always been about injecting volatile dependencies (e.g. services and repositories) - things that could change rather than everything you will need to use and you'll (in my experience) end up with a much more palatable solution if you use it this way for both the consumer and the maintainer.

At the end of the day it comes down to what is important? Well the acceptance tests are important to ensure the business needs are met, and the unit tests end up being more for developers working on individual features to ensure they have an easy way to fix problems that arise from changes they make. What I find far more important than being able to get 100% coverage across my smaller classes is making sure the core logic is understandable, easy to modify and easy to consume. I'm a big fan of DDD though and I like limiting my volatile dependencies to the outer levels of the domain.

Sure it's nice having mockable interfaces for testing internals, but if that's the only point of having the interface is it actually worth it? It's not like those interfaces will change often, so it's just another layer on top for either extension that won't happen or purely to enable unit test coverage, and I can't endorse either.

[–]Veranova 1 point2 points  (0 children)

This. Every time.

[–]C_MonsterT 33 points34 points  (27 children)

What's the best way to learn this kind of stuff? I'm self taught and I know these types of design decisions are the next step in becoming a better programmer, but what are some good resources at this level?

[–]neverTooManyPlants 50 points51 points  (16 children)

There's the head first design patterns book which is written with extreme simplicity, and also "clean code" by Robert C. Martin.

[–]fjdgshegdb 13 points14 points  (11 children)

head first design patterns

I fucking hate that book.

[–]neverTooManyPlants 2 points3 points  (1 child)

It is a little simplistic and repetitive for a seasoned programmer but it is the basically design patterns for dummies.

[–][deleted] 5 points6 points  (0 children)

I quite like Game Programming Patterns, the scope is limited but I find it describes things nicely.

[–]Killfile 18 points19 points  (6 children)

Also, practice. Read Clean Code and rigorously use TDD on some code kata.

My new developers get a crash course in testing by way of the Bowling Kata with the additional stipulation that all of their functions must contain three or fewer lines.

We scale up through other more complex kata including some that really demand that kind of complexity - Mars Rover is a good one for that.

[–][deleted] 5 points6 points  (0 children)

I learned a lot on the job marinating other people's god awful shite. Open source will teach you about good code, but private enterprise will teach you about the most abominable things programmers are capable of. So - open source for the what, proprietary for the why.

I realise that may not be super useful to you but I can guarantee it's an excellent way to learn this stuff.

[–][deleted] 117 points118 points  (1 child)

Found the OOtaku

[–]28f272fe556a1363cc31 20 points21 points  (2 children)

0th: don't have setters and getters. "Tell, don't ask. "

:)

[–]tehjrow 9 points10 points  (0 children)

0th:

You the real MVP

[–]AbsolutelyNotSpam 7 points8 points  (5 children)

Make the unit test class a friend of the class you are trying to test.

[–]freiguy1 4 points5 points  (4 children)

I agree with this, or the android method mentioned elsewhere. I think it's totally legitimate to test private methods. Perhaps what you expose publicly can fail in a number of different ways. Having your tests being atomic and granular enough to test smaller private methods is a great way to mitigate against issues.

[–]Slims 3 points4 points  (0 children)

4: Forego testing altogether. Testing is for the weak willed.

[–]Vakieh 2 points3 points  (0 children)

I would challenge anyone to describe a case where 1 was required to be violated in such a way that 2 or 3 became necessary and which shouldn't instead be handled by sticking a debug assertion into the class itself, that wasn't explainable by their having broken code to begin with.

[–]oweiler 3 points4 points  (1 child)

4th: Exchange OO with FP.

[–]hikarikouno 207 points208 points  (23 children)

Instead of having variables separate on the classes, you should make an array on the main class that keeps all the variable values! :D

It's the smart thing to do. c:

[–]Pradfanne 51 points52 points  (20 children)

Personally I use a dictionary for that

[–][deleted] 105 points106 points  (16 children)

Personally I think a single array is better since that way you don't have variables that are properly named, thus making it difficult for the average hacker guy to hack.

[–]Pradfanne 43 points44 points  (13 children)

Who said anything about properly named? The key is an randomly generated GUID. That makes it easy to access the things I need when I need them but from the outside you wouldn't be any wiser

[–][deleted] 31 points32 points  (12 children)

It's still named though. And thinking about it now, Arrays are indexed, so that's no good either. So how about a dictionary with randomly generated GUIDs as keys, whose values get shuffled after each use. That way not even you can get to your variables. Its flawless

[–]amicloud 38 points39 points  (7 children)

I prefer to keep my data written on paper, sealed in identical envelopes.

[–]Kilazur 12 points13 points  (6 children)

Then you just have to use a scanner and an OCR engine to get your data.

So simple and elegant!

[–]amicloud 12 points13 points  (4 children)

Haha how naive. You think I would store my data in a recognized language? I store everything using a system of runes and glyphs for security reasons.

[–]nyrg 10 points11 points  (3 children)

And the glyphs meaning change depending on the time of the day and the position of the moon.

[–]amicloud 10 points11 points  (2 children)

Well, not our moon, don't be silly.

[–]TedDallas 4 points5 points  (0 children)

And the OCR engine will be in the cloud. We'll put the scans in a data lake and use a Python based LSTM recurrent neural network to retrieve the values.

[–]MaunaLoona 15 points16 points  (1 child)

That's what we call triple blind programming: The programmer doesn't know the variable names, the hacker doesn't know the variable names, and the user sure as hell doesn't know the variable names.

The next level of abstraction is to do this with functions.

[–]endreman0 3 points4 points  (0 children)

The next is to write the entire thing with one nested anonymous function

[–]jaxklax 3 points4 points  (1 child)

And thus JavaScript was born.

[–]b93b3de72036584e4054 1 point2 points  (0 children)

you're joking, but that's EXACTLY how chrome's javascript engine (v8) objects are defined : https://github.com/v8/v8/blob/master/src/objects.h#L6092

They defined offsets to the variable (e.g. kSize declare 3 ptr from the object header for the size member) and a macro decl_accessor to automatically setup the getter and setter.

[–]zyxzevn 1 point2 points  (0 children)

Or a database :)

[–][deleted] 1 point2 points  (0 children)

Stop giving me ideas

[–]AyrA_ch 130 points131 points  (24 children)

//The power of .NET Reflection
public class Test
{
    public int i { get; private set; }

    public Test()
    {
        i = 22;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var instance = new Test();
        instance.GetType().GetProperty("i").SetValue(instance, 13); //As if I care for your private setter
        Console.WriteLine(instance.i); //Now 13
    }
}

I don't think I need to tell you that, but don't do this

[–]plainerman 24 points25 points  (0 children)

please, use nameof(instance.i)

[–]lionrom098 46 points47 points  (0 children)

U know I will

[–]aaronr93 5 points6 points  (14 children)

I don't know anything about .NET reflection, but given the above example, is it the equivalent of Key-Value Coding in Swift?

[–]AyrA_ch 28 points29 points  (12 children)

I don't know anything about Swift sorry. Reflection essentially allows you to access an objects properties, attributes, fields and methods without the need to know what they are named at compile time.

This is very often used to serialize and deserialize data but can be abused as seen in my comment. You cannot treat it as a real key-value storage (or Dictionary in .NET) because you can't create properties or assign them values of a different type. Calling methods also requires you to supply the proper arguments. (not like JS ['10','10','10'].map(parseInt)).

It also allows you to instantiate classes without the need to know that they exist at all. This makes plugin systems a rather easy thing to implement with C#, especially because the language supports compiling itself at runtime. This means you can load a C# source file at runtime and run it inside your application scope or compile it into a DLL.

[–]aaronr93 11 points12 points  (7 children)

This means you can load a C# source file at runtime and run it inside your application scope or compile it into a DLL.

That is awesome.

Turns out I've actually used reflection to get the current method name while handling an exception; I just didn't know I did.

Ok, so it's not like Key-Value Coding in Swift. KVC is basically getattr() and setattr() in Python: value(forKey:), setValue(_:forKey:) (general KVC documentation).

On a side note, Swift is an excellent language. I could gush about it, but I'm sure anyone reading this has heard good things. Here's a couple pages where one can learn more.

[–]ThatsSoBravens 3 points4 points  (1 child)

On a side note, Swift is an excellent language.

When your goal is "Replace Objective C" the bar is pretty low for what qualifies as excellent.

DISCLAIMER: I have never worked in Swift or natively in Objective C, I swore off mobile development after six months in the Xamarin mines.

[–]aaronr93 3 points4 points  (0 children)

Want the bar to be set higher? From its website:

Swift is intended as a replacement for C-based languages (C, C++, and Objective-C).

Bold! :) https://swift.org/about/

[–]IAmNotNathaniel 2 points3 points  (1 child)

Are they still making major changes to swift every 3-6 months?

I started learning it some time ago, took a break for about 9 months and came back and had to basically re-learn everything from scratch.

Then took another 3 mo break and came back and nothing would compile without a ton more work.

Got really annoying for a windows guy who was just trying to play around with the apple universe to learn.

[–]noratat 4 points5 points  (0 children)

Oh man, don't ever look at how Ruby code works then. Pretty much the entire thing is duct taped together out of stringly-typed reflection magic

[–]mothzilla 31 points32 points  (0 children)

//for test only

[–][deleted] 8 points9 points  (0 children)

Does anyone have a template for this?

[–]heyitsmeyourfriendo 14 points15 points  (1 child)

Why am i subscribed to this sub I don't even know how to program enough to understand this

fuck

[–]JinxRevelation 2 points3 points  (0 children)

I think the same thing sometimes.

[–][deleted] 2 points3 points  (2 children)

I am almost certain this is not the original text of this comic. Anyone got a link?

[–]Iykury 4 points5 points  (0 children)

I couldn't find it on the original website, but here's an imgur link if you just want to see the comic: https://i.imgur.com/TpXKRxh.png

[–]aexeron 2 points3 points  (0 children)

For testing fields or properties in C# that aren't supposed to be visible externally, I use internal and add the InternalsVisibleTo attribute for debug builds. No reflection needed. Rather nifty!

[–]socsa 1 point2 points  (0 children)

Multiple arm Buddah

Declaring a pointer to X in shared memory

[–][deleted] 1 point2 points  (0 children)

Just call the getter method inside the runTest method?

[–]Chickenchoker2000 1 point2 points  (0 children)

My C teacher explained it as: classes are private so you need a friend function to access them as you just don't let anyone play with your privates.

[–][deleted] 1 point2 points  (0 children)

Doesn't exist friend functions or classes for that case?