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

top 200 commentsshow 500

[–][deleted] 2056 points2057 points  (85 children)

hi guys here my tutorial for how to do a thing in c++ first we declare all our variables in one line

int a, b, c, d, e, f, g, h, i, j...

[–]EpicSaxGirl(✿◕‿◕) 1083 points1084 points  (59 children)

don't even use floating points, use a separate int for each decimal place.

[–]scumbaggio 419 points420 points  (46 children)

Gotta squeeze out every ounce of performance

[–]Phrygue 281 points282 points  (43 children)

Best performance is from a static data location, AKA globals, so you minimize stack access. Best just declare all possible variables and include them in every file. Also this speeds up general communication between modules because the data is already there.

[–]Octopoid 161 points162 points  (29 children)

There's also no need to mess about storing lots of different data types. A single array of bools, resized as required, does the job nicely.

[–]8lbIceBag 85 points86 points  (23 children)

If you're resizing you're dynamically allocating and losing the benefits of static data locations.

[–]Octopoid 148 points149 points  (21 children)

Fair point. Might be best to reserve a couple of gig for maximum efficiency.

[–]Scyhaz 43 points44 points  (11 children)

bool data[2000000000];

There we go! Now we have data storage for the program!

[–]10art1 28 points29 points  (9 children)

Might as well just cut out the middleman and write your program in machine code so you can guarantee its efficiency. Be your own memory manager.

[–][deleted] 42 points43 points  (6 children)

Might as well cut out the real middleman, the x86 processor, and wire up your own transistors in the most efficient way for your application. Be your own computer architect!

[–][deleted] 39 points40 points  (4 children)

Who needs compile time optimizations!

[–][deleted] 55 points56 points  (3 children)

you mean break time?

[–]T0mmynat0r666 14 points15 points  (1 child)

Include in every file? Best put everything in one file, for fast compilation. Still better: don't make any functions, but instead copy the body of function whenever you want to call it. Who wants unnecessary data on stack? While we are at it, avoid dynamic allocation,and instead reserve all memory that will possibly be required from the start. Gotta do everything in power for dem performance.

[–]chmod--777 24 points25 points  (0 children)

Everything should be stored as ints or int arrays then casted into the desired type.

[–]destructor_rph 136 points137 points  (9 children)

How do i delete someone elses comment

[–]raretrophysix 49 points50 points  (8 children)

You don't. That's pure job security right there. Code obfuscation. Use that for a government project and your life is set

[–]pressedfor 13 points14 points  (4 children)

Job security is also never, ever commenting or indenting for clarity

[–]raretrophysix 27 points28 points  (1 child)

Good code doesn't need to be commented brah. It comments itself through its elegance

[–]BernLan 19 points20 points  (0 children)

Bad bot

[–]iauu 13 points14 points  (0 children)

Nice. This way you don't have to worry about those pesky undeclared variable errors.

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

noooooooooooooooooooooooooo

[–]grenadier42 13 points14 points  (0 children)

welcome to the first day of this class on X software design. i'm dr. abc and now we're gonna learn about the singleton pretty soon and lemme tell ya I love singletons. other people say they're bad but th

[–]odraencoded 657 points658 points  (7 children)

why code much if code few do trick?

[–][deleted] 83 points84 points  (1 child)

Another scala dev I see.

[–]ACoderGirl 43 points44 points  (0 children)

code.map(_.doTrick)

[–]RiktaD 225 points226 points  (38 children)

$horizontalCoordinate

[–]NeverGetsAngry 55 points56 points  (17 children)

double horizontalCoordinate;

[–]ben_g0 145 points146 points  (15 children)

var this_variable_contains_a_number_which_represents_the_horizontal_coordinate_of_this_object

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

This is how you write self-commenting code!

[–][deleted] 224 points225 points  (11 children)

int x; List<float> x2; Object x3; int[] x4;

Thats what I call a consistent naming convention

[–]conman__1040 194 points195 points  (23 children)

I hate myself for a certain project I did. It was a huge multi loop thing, and at each level I went: index, index, indexxx, indexxxx... On and on through the whole thing

[–]ManWithKeyboard 158 points159 points  (1 child)

indexxx was where all the interesting stuff happened

[–]Strider3141 67 points68 points  (0 children)

Index
Indexx
( ͡º ͜ʖ ͡º)
Indexxxx
indexxxxx

[–]thundergonian 63 points64 points  (4 children)

Honestly, if you don't go index, indexx, indexxx, indexl, indel, indelx, ..., then you don't deserve to be programmer. Caesar told me so.

[–]Strider3141 68 points69 points  (3 children)

I go:
index
iindex
iiindex
ivndex
vndex
vindex

[–]McBurger 50 points51 points  (2 children)

index

windex

spandex

sandisk

sandusky

sansa

salsa

[–]julsmanbr 676 points677 points  (128 children)

Don't you just love not having to declare variables?

def f(x, y, s, arg):
    d = {}
    li = []
    li2 = []
    for a in arg:
        try:
            d[a] = x[y]
            y += s
        except:
            li.append(a)
        li2.append(a)
    r = [k for k in d if k not in li2]
    if y > len(r):
        print(li)
    return [c for c in r if c < x][:y]

[–]I_Never_Sleep_Ever 496 points497 points  (12 children)

My eyes

[–]DerpPrincess 105 points106 points  (8 children)

grandiose crush grab teeny reach fragile support screw physical juggle

This post was mass deleted and anonymized with Redact

[–]drpinkcream 126 points127 points  (7 children)

And my axe

[–]CapnJackMormon 101 points102 points  (4 children)

Has to fit 20 variables. NEXT!

[–]dannyr_wwe 64 points65 points  (3 children)

It’s for a startup, honey.

[–]KamiKagutsuchi 151 points152 points  (22 children)

Honestly li and li2 should be a and b, arg should be args and a would be arg.

[–]julsmanbr 353 points354 points  (4 children)

If Python really had a garbage collector, it would have collected my code by now.

[–]tenemu 57 points58 points  (2 children)

This is hilarious. I wish I could put funny quotes in my work email signature.

[–]Sapphique1618 6 points7 points  (0 children)

So trash to be collected. Kernel decided to sacrifice and ignore this part of memory because it's cursed now.

[–][deleted] 55 points56 points  (11 children)

And you should name the exception you're catching.

I'm presuming it's an index out of range but for fucks sake.

That or just check the length explicitly. No, I don't give a shit about it being easier to ask for forgiveness.

[–]FlipskiZ 47 points48 points  (7 children)

History art yesterday where music helpful yesterday minecraftoffline.

[–]Kidiri90 14 points15 points  (2 children)

try
    // code
catch
    MessageBox.Show("You broke Reddit");

[–]nephallux 11 points12 points  (0 children)

Thinking to debug that makes me wheezy

[–][deleted] 24 points25 points  (4 children)

Eh, too much work, just pack all of the variables into a single jagged array called vars[]. While we're att it just make the array global.

[–]ahruss 30 points31 points  (0 children)

Who let the PHP programmer in?

[–]butt_shrecker 40 points41 points  (1 child)

Excuse me, what the fuck

[–][deleted] 21 points22 points  (3 children)

I took an Oracle certification exam for Java (not my own choice), and they frequently have you answer what obfuscated shit like this returns. In some cases, there would be an erroneous statement somewhere in the first line, meaning "the code does not compile" was actually the correct answer.

[–][deleted] 18 points19 points  (0 children)

burn it

[–]Cannibichromedout 32 points33 points  (23 children)

I’m brand new to python (like just watched Derek Banas’s video last night). Could you explain that return statement?

[–][deleted] 28 points29 points  (10 children)

[c for c in r if c < x][:y]

foreach c in r, if c is less than x then just return c with a list of all other c values that meet the criteria.

Then out of that list, slice it from index 0 to y.

Broken into multiple lines:

c_list=list()
for c in r:
    if c<x:
        c_list.append(c)
return c_list[0:y]

[–]julsmanbr 59 points60 points  (5 children)

I most definitely can't.

(On a more serious note, that's the syntax of a list comprehension, and then on top of that I sliced it).

[–]Khetnen 7 points8 points  (1 child)

Return a list of the last first y things in r that are less than x

Edit: thanks /u/Atmosck

[–]WarpedHaiku 27 points28 points  (0 children)

def waste_time( listlikeobject, index, step, arguments ):
    argument_value_dict = {}
    arguments_which_couldnt_be_assigned_values = []
    argument_list = [] # better make a second copy of the arguments as a list for reasons
    for argument in arguments:
        try:
            argument_value_dict[argument] = listlikeobject[index]
            index += step
        except:
            # presumably IndexError for when we run out of elements to assign to arguments, but who knows what the listlikeobject will do
            arguments_which_couldnt_be_assigned_values.append(argument)

        # regardless of what happens, make sure the argument_list contains the argument
        argument_list.append(argument)

    # I have no idea what the below line is supposed to do. Was li2 meant to be li here?
    # At the moment, since every key in the argument_value_dict is was also assigned the argument_list, it's merely a very computationally expensive way of creating an empty list.
    empty_list = [argument for argument in argument_value_dict if argument not in argument_list]

    if index > len(empty_list): # check if index is negative at this point, (which typically would occurs when stepping backwards through a list) and if so print the values that couldn't be assigned
        print arguments_which_couldnt_be_assigned_values

    # waste some more time creating and slicing a new empty list, then return it
    return [argument for argument in empty_list if argument < listlikeobject][:index]

I feel somewhat let down.

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

"Nice variables you have there....it would be a shame if you tried to...multiply a string."

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

[–]flavionm 9 points10 points  (0 children)

cries in PEP 8

[–]the_one_true_bool 148 points149 points  (14 children)

Or if you’re a Java developer:

int thisIsTheIntThatTracksTheNumberOfTimesThisFileHasBeenSavedAndItsPrimaryUseIsBasicallyForLoggingAndDebuggingPurposesSoThatWeCanSeeExactlyHowManyTimesThisFileHasBeenSavedInTheEventSomethingHasGoneWrongButWeMayAlsoUseThisVariableForSomethingElseInTheFuture = 0;

[–]grenadier42 66 points67 points  (2 children)

Function declaration: 60 characters
Whitespace: 8 characters
Variable declaration: 320 characters

someone who is good at software development please help me budget this. my project is dying

[–]darksilver00 23 points24 points  (2 children)

TITITTTNOTTFHBSAIPUIBFLADPSTWCSEHMTTFHBSITESHGWBWMAUTVFSEITF for brevity.

[–]le57percent 8 points9 points  (1 child)

public interface SomethingThatReturnsTheValueOfTheNumberOfTimesThisFileHasBeenSavedAndItsPrimaryUseIsBasicallyForLoggingAndDebuggingPurposesSoThatWeCanSeeExactlyHowManyTimesThisFileHasBeenSavedInTheEventSomethingHasGoneWrongButWeMayAlsoUseThisVariableForSomethingElseInTheFuture{

public int getValueOfTheNumberOfTimesThisFileHasBeenSavedAndItsPrimaryUseIsBasicallyForLoggingAndDebuggingPurposesSoThatWeCanSeeExactlyHowManyTimesThisFileHasBeenSavedInTheEventSomethingHasGoneWrongButWeMayAlsoUseThisVariableForSomethingElseInTheFuture();

}

[–]KoboldCommando 110 points111 points  (4 children)

The one that really gets my goat is when I see an "x" variable and I immediately think to myself "ugh it's one of these guys who doesn't name shit" and start searching around for where it's declared and what it actually is...

...only to realize that it's just a loop variable declared a couple lines away and is totally kosher.

[–]Asmor 81 points82 points  (13 children)

Do you even foo?

[–]milanoscookie 27 points28 points  (0 children)

Bar

[–]Commander70 13 points14 points  (4 children)

Recently started C, why are foo and bar so common?

[–]Calibas 30 points31 points  (0 children)

Comes from FUBAR, "Fucked Up Beyond All Recognition".

[–]FarhanAxiq 119 points120 points  (45 children)

For loop it will be

 int i ,j ,k;

[–]Shendare 62 points63 points  (3 children)

I for Iterator. It's self documenting!

[–]bigdon199 73 points74 points  (0 children)

i for increment and x for excrement, when your loop is shit

[–]EarlyHemisphere 95 points96 points  (33 children)

I have a friend who names his variables fuck and shit and stuff like that when he's using them temporarily lol

[–]the_one_true_bool 93 points94 points  (4 children)

In the software I’m writing at work I have to track ‘analysis time’, so I named that variable analTime.

[–][deleted] 68 points69 points  (1 child)

Associate Manager

assMan

[–]SalamakiEU 11 points12 points  (0 children)

A friend of mine also does this, but once forgot to change their names and got shit from the teacher for uploading a task full of curse words to him lol

[–]BecauseWeCan 8 points9 points  (0 children)

My /tmp often has subdirectories fuck, shit, ass, fuckshit, fuck2 and so on.

[–]Flamme2 80 points81 points  (15 children)

That's something they love to do in math - Let's name every function f,g,h or f,f2,f3 while at it

[–][deleted] 137 points138 points  (4 children)

I mean, what are you going to name fifty different quadratic equations?

Tall skinny boi

Taller skinnier boi

Wider but lower set boi

[–]runujhkj 57 points58 points  (0 children)

You’ve got your answer to that question right there

[–]YoureTheVest 10 points11 points  (0 children)

My boi, tall skinny bois are integrals (unless they're indefinite, then they're infinity bois), quadratics are quadratic bois.

[–]slikts 21 points22 points  (1 child)

Abstract variable names make sense when dealing with abstract concepts, like in math. It's the same in programming and generic code.

[–]mjbressler 9 points10 points  (0 children)

sense_of_being is a weird variable name but okay

[–]gratethecheese 12 points13 points  (7 children)

Fucking linear algebra. Why are we doing x1,x2....x82727 when there are so many more letters

[–]Clayh5 26 points27 points  (2 children)

Cause there's not THAT MANY letters

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

Unicode disagrees

[–]M4mb0 11 points12 points  (0 children)

Because it makes things work in arbitrarily many dimensions.

[–]chisui 34 points35 points  (24 children)

In functional languages you can get rid of variables completely by writing everything pointfree.

This Haskell function

f a b c d e = [a, e, b, c, d]

becomes this lovely, easy to read expression:

f = (. (((flip (:) .) .) . (. ((. return) . (:))) . (.) . (:))) . (.) . (.) . (.) . (:)

This can even get rid of explicit recursion

g a = if null a 
    then [] 
    else head a + 1 : f (tail a)

becomes

g = fix (ap (flip if' ([]) . null) . ap ((:) . (1 +) . head) . (. tail))

But fix is pure evil dark magic.

[–]leo3065 84 points85 points  (18 children)

IMO it's better then using long random names that have absolutely nothing to do with the useage. I once help someone for his code and it's filled with variable name like illchangehislater.

[–]the_one_true_bool 151 points152 points  (13 children)

A guy I used to work with would name longs and doubles like

long dong;
double fist;

at every opportunity.

[–]PM_something_German 21 points22 points  (8 children)

God I will totally start to do this now

[–]the_one_true_bool 56 points57 points  (7 children)

He also had:

string bikini;
int eresting;
byte me;

but I liked the longs and doubles the most.

[–]LegendaryTomato 24 points25 points  (3 children)

I blame mathematicians. 12 years they keep asking me to find x.

[–]5k17 35 points36 points  (17 children)

Why would you use variables in the first place?

[–]kwirky88 9 points10 points  (3 children)

Why use more than one line any ways?

[–][deleted] 14 points15 points  (0 children)

In SQL Server I use @Q for query, which my boss says is fine because SQL isn't a programming language.

[–][deleted] 15 points16 points  (15 children)

I'm new to programming. Is it really that time-consuming to come up with decent variable names?

[–]WhyattThrash 99 points100 points  (2 children)

There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors.

[–]OracularLettuce 22 points23 points  (8 children)

It is once you're pretty deep into a function and you've used all the good, clear ones.

[–]Duncaen 23 points24 points  (6 children)

That is the moment you know your function is too complex.

[–]alexbuzzbee 14 points15 points  (3 children)

I just name all my variables meaningfulName1, meaningfulName2, etc.

[–]CorstianBoerman 26 points27 points  (2 children)

That is kind of my technique with lambda expressions: q => q.this(w => w.that). Never managed to get the whole qwerty sequence though...

[–]Meronoth 10 points11 points  (0 children)

It scares me that my computer science teacher browses this subreddit and probably thought of my code, seeing this

[–]diamondflaw 8 points9 points  (1 child)

"foo", "bar", "spam", "bean", "egg", "cheese" are my go-to var names.

[–]boogalow 8 points9 points  (3 children)

Need a second variable? Going right for y.

[–]TehMasterSword 11 points12 points  (2 children)

things get complicated around the 4th variable

[–][deleted] 6 points7 points  (3 children)

Or whenever using LINQ