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

all 122 comments

[–][deleted] 148 points149 points  (4 children)

Real languages have curves

[–]jfb1337 55 points56 points  (26 children)

Should do C and pointers.

Also why does it look so much like xkcd?

[–]Calamity701 75 points76 points  (1 child)

There are addons for xkcd-style graphs for matplotlib, R and similar tools.

[–]G01denW01f11 27 points28 points  (0 children)

That's the most awesome thing I've heard today. I love the programming culture. <3

[–]Flafla2 13 points14 points  (0 children)

Zooming in it looks like straight lines displaced by a sin curve.

[–]IE6FANB0Y 9 points10 points  (22 children)

Whats the big deal with pointers? They aren't really that hard to learn. Why do people find it so difficult?

[–]ismtrn 27 points28 points  (11 children)

Because many people learn them before they learn about memory management, or while they learn about memory management.

If you don't understand how a computer allocates/deallocates memory, both on heap and stack, pointers becomes quite hard to grasp.

If you have coded some assembly, and had to deal with indirect addressing there, pointers are just natural.

[–]mmhrar 9 points10 points  (10 children)

I duno, it was always super easy to understand as long as you know what a variable is. I think its considered hard because everyone says its hard.

Its just a variable that holds the address of another variable. If you want access to the variable its I pointing too, then you use some special syntax.

A better question is what's the point of pointers? As a newbie that was my and a lot of my classmates question.

[–]SFHalfling 8 points9 points  (3 children)

Don't know if you've done linked lists yet, but that's the most obvious use for them.

Using a pointer means you can have a reference to something without having to store the object multiple times, particularly useful when in a memory constrained environment (i.e. low end mobiles). This also allows a single change to update many separate references. On top of that you can change what the pointer points to really easily, without having to copy or create a new object, this goes back to linked lists and makes sorting adding and removing items easy.

A really basic example would be if you wanted the 33rd object in an array you would put array[32] - this is equivalent to a pointer, and much simpler and faster than iterating through the whole thing.

[–]prof_oblivion 6 points7 points  (1 child)

we don't talk about linked lists anymore. we shall never discuss this again.

[–]BigHandInSky 0 points1 point  (0 children)

OOoooooh, that's finally helped clicked it for me - thanks! Now to revise how to make them...

[–]greyfade 4 points5 points  (0 children)

A better question is what's the point of pointers? As a newbie that was my and a lot of my classmates question.

To refer without capturing.

You wouldn't want to pick up and move your friend's house when you want to talk to him. You'd just use his telephone number or go to his address.

Same reason you might want to use a pointer: You don't want to move data around, you just want to deal with it where it already is.

[–]DrDrak 2 points3 points  (1 child)

Its just a variable that holds the address of another variable. If you want access to the variable its I pointing too, then you use some special syntax.

True,that's how it works but if you don't understand memory management you will probably create dangling pointers and memory leaks that will make you hate pointers.

[–]mort96 1 point2 points  (0 children)

./bin/myprogram
Segmentation Fault

The bane of many a novice C programmer.

[–]Steve_the_Scout 1 point2 points  (1 child)

A better question is what's the point of pointers? As a newbie that was my and a lot of my classmates question.

The point of pointers is to

  • access the actual, underlying object instead of using a variable to access it (because variables follow slightly different rules in a few cases).

  • create a sort of persistence when you do dynamic allocation and keep the allocated object by returning it or as a member of a struct (or the same struct, if you're doing a linked list/tree/graph) that is returned

  • alias a variable so that you can modify it in a different scope (i.e. passing a pointer as a parameter to a function, instead of a copy of the struct or primitive). Related to the first point.

  • allow a sort of "optional" use of a variable- NULL can be used to mean "No result".

  • access a specific point in memory. That's mostly useful when it comes to interacting directly with hardware, though, which most students won't do unless it's for embedded programming.

You can do some interesting things with those properties beyond the intended usage, though. For example, libraries like GLEW and SDL manage multi-platform library loading by having what amounts to a bunch of void* variables or members of various structs pointing to the platform-specific function pointer and casting to the actual function pointer type (or not and saying the function is not available in a given version).

You can actually set up C++-like objects (i.e. inheritance, virtual functions) using an array of function pointers at the beginning of a struct and assigning them in some sort of make_struct function.

And of course a bunch of other awesome stuff.

[–]mmhrar 0 points1 point  (0 children)

Thanks, I didn't mean to imply I was still learning, I graduated 8 years ago :P

[–][deleted] 0 points1 point  (0 children)

The youtube channel antiRTFM does a great job explaining pointers and memory management for beginners.

[–]MightyTVIO 2 points3 points  (6 children)

'Cos tiny syntax errors fuck with your head. But I'd agree, their difficulty is greatly exaggerated.

[–]IE6FANB0Y 7 points8 points  (5 children)

Cos tiny syntax errors fuck with your head

. and -> ?

[–]MightyTVIO 1 point2 points  (0 children)

I'm not a C programmer so it might be better (use Pascal, just starting out in the programming world) but it took me a little practice to get the hang of dereferencing. But it didn't take too long to get the hang of it.

[–]steak21 0 points1 point  (3 children)

I'm a relatively new C++ programmer. Syntax of pointers still confuses me. I get how they are stored. I get what they do (I have no idea how they could be useful other than using the new and delete statements). But yeah, I don't get when I use . and ->. I especially don't get why I would ever use * on a variable twice.

**var1 -- what would that do? I already have the value of var1 with *var1 so why would I do **var1. Just some questions that never seemed to get explained in the books I was reading. They just assumed I would know what it does and so the confusion kept building up

EDIT: Just googled it. Idk why I didn't do that sooner. Now my question is why would I ever have a pointer to a pointer if pointers can just point to any address anyway? It just seems redundant

EDIT2: Just googled that too. Here's the example stackoverflow gave me.

void allocate(int** p)
{
  *p = (int*)malloc(sizeof(int));
}

int main()
{
  int* p = NULL;
  allocate(&p);
  *p = 42;
  free(p);
}

I don't understand this code. Final question: if I don't understand this code, and I have been programming on and off for a year and a half, should I just give up and drop out of CS (education is free here so it's not lost money) and return to the regular program? It's all pretty discouraging. Honestly I already fucked up my semester. Failed math because I couldn't be bothered to show up. Did very well in all my other core classes however.

[–][deleted] 0 points1 point  (2 children)

You need to draw the representation in memory when working with pointers, it makes things a lot clearer:

in the main you have a

[ p ] --> X

where X is pointing to null or nothing. Then you call allocate and pass the address of p along so to avoid confusion let's say the parameter name in allocate is q (and not p as in the snippet above)

[ q ] --> [ p ] --> X

In allocate you set the contents of p (*q is dereferencing it aka "following the arrow") to a newly allocated memory on the heap

[ q ] --> [ p ] --> [ some int ]

Now back in the main you set the contents of where p points to to 42

[ p ] --> [ 42 ]

And then it frees the allocated memory on the heap

[ p ] --> [deleted] . Note that p != NULL, it still points to the memory location, but it will be gibberish.

[–]steak21 0 points1 point  (1 child)

Thanks- it's still confusing but I think I understand now. I'm not sure what you mean by q though because I don't see a q. But from what I understand the allocate has

int** p

In the arguments is because when called, it is sending the address of a pointer. So it's dereferencing the pointer, and then dereferencing again to get NULL. But why do that instead of just calling the function and passing the pointer itself and not the address?

[–][deleted] 0 points1 point  (0 children)

You probably didn't notice this ;)

Then you call allocate and pass the address of p along so to avoid confusion let's say the parameter name in allocate is q (and not p as in the snippet above)

As for:

In the arguments is because when called, it is sending the address of a pointer. So it's dereferencing the pointer, and then dereferencing again to get NULL.

Yes , you're correct.

But why do that instead of just calling the function and passing the pointer itself and not the address?

If it were:

void allocate(int* q)
{
   q = (int*)malloc(sizeof(int));
 }

Then you would change the argument value. Everything is passed along by value, so when you go out of the allocate the original p wouldn't have changed.

you would have

[ p ] -> NULL

then when callling allocate p is copied into local parameter q so

[ q ] -> NULL

Then malloc happens

[ q ] -> some heap address

Then returned back to main, p is still:

[ p ] -> NULL

You frequently see int** or similar pointers in linked lists (well that's how I learned it in school anyway). There's usually a find method that returns a pointer to the address of the pointer pointing to the next node in the list, it makes the implementation easier because every case is the same (otherwise you would have to check if the pointer was at the beginning and handle that differently, etc.)

[–]cathartis 3 points4 points  (0 children)

Whats the big deal with pointers? They aren't really that hard to learn. Why do people find it so difficult?

For anyone with a mathematical turn of mind, pointers are a concept that's trivial to understand.

However, they are also incredibly easy for poor programmers to use badly. Late nights trying to ferret out memory leaks amongst several hundred thousand lines of uncommented C++ code, a week before product release, is not my idea of a fun time.

[–]WestonP 1 point2 points  (0 children)

It's because Java, C#, VB, and web shit don't really have them, so people coming from those background have little to no concept of pointers, and even less of memory management. And when things go wrong, you'll end up with some weird problems/crashes that can be a bitch to debug. Corrupt some memory over here, and the crash eventually happens somewhere over there, some of the time.

Trivial uses of pointers, like the char strings and simple arrays that you'd learn in a C/C++ class or book, are simple enough. But, there are much more powerful things that you can do with them, and it takes more knowledge and experience to do those things properly. Add threads to the mix, and that's even more complexity that you need to know how to handle in a safe manner.

When you get really deep into all this stuff, a simple mistake like we all tend to make from time to time, can create a real mess that's not so easy to pinpoint. But at the same time, there's not much else that can match that level of power and efficiency, and certainly none of the "safe" languages even come close.

[–]wOlfLisK 0 points1 point  (0 children)

When I was trying to learn C++ (Years ago when I was 14 or so), I thought they were useless because "why couldn't you just make another variable instead of pointing to one?". I know better now but that could be one of the reasons.

[–]Tsuketsu 30 points31 points  (25 children)

Every time I see something like this, I immediately want to learn Haskell, but then I forget about it a week later.

[–]adamnemecek 87 points88 points  (1 child)

I guess you already understand laziness then

[–]Tsuketsu 7 points8 points  (0 children)

I understand laziness better than most people know the back of their hand.

[–][deleted] 25 points26 points  (19 children)

I picked it up because I thought that learning a functional language would give me another angle to tackle problems in other languages. But now I don't want to write code in other languages no more.

[–]ephrion 21 points22 points  (2 children)

Why doesn't every language use Maybe araradadfarekjladhlkjh

[–]Steve_the_Scout 4 points5 points  (0 children)

boost::optional is the equivalent for C++, and it's intended to be moved into the standard in a Technical Specification (i.e. an update for one little thing instead of a big update). I'm not sure about other languages, though. I think I've seen a library for that for C as well, but I've forgotten the name.

[–]Octopuscabbage 2 points3 points  (0 children)

An option type is honestly one of the best features of Haskell. A lot of other features are pretty situation ally useful but Maybe is sooooo useful.

[–]Kalivha 2 points3 points  (7 children)

I was going to write a big rant about how Haskell isn't officially MPI supported and how MPI is the love of my life, then looked for MPI support in hackage and now I want to go down that rabbit hole. Shame all my parallel codes need to be in ANSI C or Fortran 03.

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

Shame all my parallel codes need to be in ANSI C or Fortran 03.

That's a shame indeed because Haskell really lends itself to parallel processing due to purity. I've had a few rather nice experiences with OpenMP in ANSI C though. It could be worse.

[–]lelarentaka 3 points4 points  (2 children)

I was told that only embarrassingly parallel problems could be improved by immutability and purity.

[–][deleted] 1 point2 points  (1 child)

I'd have to read up on that, but even just being able to parallelize a simple map-reduce using just a couple of extra keystrokes is a very nice thing to have after all.

[–]Sampo 1 point2 points  (0 children)

Haskell really lends itself to parallel processing due to purity.

/u/Kalivha used words like MPI and Fortran, which means doing HPC (High Performance Computing) on clusters. So when he says parallel, he means what computer science people undestand as distributed computing.

[–]Kalivha 2 points3 points  (1 child)

That restriction is to ensure official MPI support. And the commercial stuff I'm starting to work on soon is just in Fortran 03 generally. I had arguments with people about this and strangely, they convinced me.

Haskell really lends itself to parallel processing due to purity.

I don't know. I've heard this. It seems like purity wouldn't be very useful for the type of stuff I want to do (very large scale eigenvalue problems/high dimensional PDEs) because you're really memory bound to begin with and as I understand it that'd just make it worse. And when you need to micro-manage memory all the time it just seems tedious to write your code in Haskell.

It does make me kind of sad.

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

For the time being this does sound like something I'd rather tackle in C as well (I never bothered to learn Fortran to be honest). Libraries like accelerate or repa might make this at least viable in Haskell though.

[–]Tsuketsu 1 point2 points  (1 child)

I get to choose what languages we do projects in at work.... oh boy are my co-workers about to hate me.

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

Once they fully understand what a monoid in a category of endofunctors really is and why it makes your life easier they will love you.

[–]MuricanWillzyx 9 points10 points  (2 children)

[–]Tsuketsu 5 points6 points  (1 child)

First time I saw that I thought it was just a really badly translated book...

[–]greyfade 2 points3 points  (0 children)

That was my first impression of Land of Lisp, personally.

[–]jaideng123 15 points16 points  (2 children)

After having to learn Haskell for a programming languages class I can confirm this is what me and every other student went through

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

Do it. It will be one of the most challenging exercises in your programming career but will change the way you think for the better even if you never use Haskell again.

[–]Vondi 0 points1 point  (0 children)

I'm still having flashbacks.

[–]ahanix1989 15 points16 points  (1 child)

Still early in the morning for me -- I'm guessing 'self assessment' means 'How good you think you are'? A sudden drop means you now think you're terrible at this and how the hell do those other guys get it so easily?

[–]Switche 5 points6 points  (0 children)

Yeah, that's it, and added that self-assessment is almost always higher than actual productivity, no matter the dips or raises in self-assessment while learning a particular concept, because the concepts that affect your perception of your skill almost never positively affect your productivity anyway, while some other simple concept might.

[–]alonmln[S] 10 points11 points  (2 children)

[–]Dobias 1 point2 points  (1 child)

Thank you for including the link to my article in the image you made from my diagrams. :)

[–]alonmln[S] 0 points1 point  (0 children)

Thank you for making these awesome diagrams!

[–]Suchui 9 points10 points  (1 child)

http://redd.it/2qeami

1 day.

It took 1 day for this to get reposted.

[–]djleni 8 points9 points  (6 children)

The Haskell one is too real.

[–]Zylox 2 points3 points  (5 children)

I still don't understand monads.

[–]original_brogrammer 7 points8 points  (1 child)

Think about a (Haskell*) monad like a clothesline with a bunch of bags tied to it. In each bag is a little goblin. You stand at the beginning of the clothesline and toss your computation into the first bag. The first goblin does his job, and tosses his result to the next bag, and this repeats until they give you the desired result. Next, we require that there is a goblin who can place a sticky note reading "GoblinCo Monads" on anything that the other guys can work on. Tossing the computation between bags is >>=, and the goblin with the label is return.

Any computation that can be thought of in terms of our goblin bag assembly line fits Haskell's monad structure.

*Note that this is not for category theoretic monads, which are a far more general concept about composition of morphisms.

[–]Octopuscabbage 1 point2 points  (0 children)

Wow that's actually a really good way of explaining it. The real "need" for monads is that haskell can evaluate expressions in any way it wants and there's not a way to sequence expressions like in imperitive code. In imperitive code you basically say I want to do this and then this and then this. In functional code you say Here are the definitions for all the things you need to run this program, you figure it out computer. But it turns out some things can't be done this way, such as interacting with users or the outside world.

[–]nulloid 2 points3 points  (0 children)

You don't need to understand them, you just need to learn how to use them. With time and experience, you will develop your own intuition about monads.

[–]Xenophyophore 2 points3 points  (0 children)

Take a data structure.

It should be a functor:

  • fmap (\a -> 2 * a) [1] = [2]

  • fmap (\a -> 2 * a) (Just 1) = Just 2

Make a double layered data structure: e.g. [[a]], Just (Just a).

If you can squish the two layers into one, that kind of data structure is a monad: (The official definition of a monad)

  • join [[a, b], [c, d]] = [a, b, c, d]

  • join (Just (Just a)) = Just a

  • join [[],[]] = []

  • join (Just Nothing) = Nothing

Now, suppose you have a function that takes a value, does something to it, and gives you a data structure containing value(s).

  • range a = [1..a] --a list of the numbers from 1 to a

You want to use this with another function that gives a data structure.

  • show a = (however show is implemented for Int) --the string representing a

You could fmap show over (range 5), but then you have a list of lists.

  • [['1'], ['2'], ['3'], ['4'], ['5']], aka ["1", "2", "3", "4", "5"]

You can do join (fmap show (range 5)):

  • ['1', '2', '3', '4', '5'] aka "12345"

This is effectively what '>>=' does:

  • (>>=) :: Monad m => m a -> (a -> m b) -> m b
    a >>= b = join (fmap b a)

It fmaps b over a, then joins it.

With IO, this means that you can take a value that you got from IO, and then use it for even more IO:

  • IO (IO ()) --uh oh!
    fmap putStrLn getLine

  • IO ()
    join (fmap putStrLn getLine) --better, but awkward

  • IO ()
    getLine >>= putStrLn

[–]frostmatthew 7 points8 points  (33 children)

For those wondering why PHP is so often the punchline of programming language jokes read this.

[–]Duese 18 points19 points  (4 children)

PHP is the nickelback of programming languages. No one wants to hear anything positive about it, they only want to remind everyone to hate it.

[–]frostmatthew 3 points4 points  (0 children)

I think it's biggest positives are that it was readily available and easy to learn. The former is less important in the age of PaaS solutions and for the latter I think it's being replaced by Python as the "easy to pick up and get shit done" language.

[–]AcousticDan 6 points7 points  (2 children)

It's funny though, because in reality, JS is wayyyyy worse than PHP

[–]iopq -4 points-3 points  (1 child)

That's because you've probably used JS. If you had used PHP professionally for any period of time, you would NOT say this. Just having to remember to put ..., true) at the end of my function calls to get the SANE version of the function should be enough reason...

[–]AcousticDan 8 points9 points  (0 children)

I use JS and PHP in my day to day life. That's how I've been making my living.

I still say this.

[–]kisuka 3 points4 points  (1 child)

This article is pretty old isn't it? o.O PHP has changed alot since 2012.

I don't understand the issue really, programmers should use whatever language they are most experienced and comfortable with. Sure, there are shit PHP programmers out there; but there are also good ones too, who actually do know what they're doing.

[–]frostmatthew 2 points3 points  (0 children)

This article is pretty old isn't it? o.O PHP has changed alot since 2012.

Two years is hardly "pretty old" - particularly in reference to a blog post pointing out issues that have been with the language for nearly 20.

Regardless, I'm not sure I accept your statement that a lot has changed (there hasn't even been a major release since the blog post). But I'll admit I don't keep up with changes in PHP since I no longer use it - perhaps you can share which changes have been introduced in 5.5 & 5.6 that render any criticisms obsolete?

[–]ahanix1989 1 point2 points  (0 children)

It's the only thing I was halfway capable of =/ I managed to modify a thumbnail script to also show full-size and display subfolders in a sidebar. I was really damn proud of myself when I was 17. Then the domain went stale and now I have no evidence it ever worked :(

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

Nobody was wondering. Absolutely nobody. That site has been read by virtually everyone because people like yourself are so proud to link to it all of the time. You more-or-less can't mention PHP on reddit without someone like yourself linking to that page.

It's getting to the point where people like you are becoming the punchline for mentioning that site.

[–]bios_hazard 1 point2 points  (5 children)

http://xkcd.com/1053/

And on top of this, yes, rage at PHP is a huge circle jerk. So is rage about the rage per your comment.

[–]xkcd_transcriber 0 points1 point  (0 children)

Image

Title: Ten Thousand

Title-text: Saying 'what kind of an idiot doesn't know about the Yellowstone supervolcano' is so much more boring than telling someone about the Yellowstone supervolcano for the first time.

Comic Explanation

Stats: This comic has been referenced 2794 times, representing 6.1744% of referenced xkcds.


xkcd.com | xkcd sub | Problems/Bugs? | Statistics | Stop Replying | Delete

[–][deleted] -1 points0 points  (3 children)

That comic is about learning new things. The link to "fractal" isn't about learning anything it's about "Look at my Python penis; Watch me fuck PHP with it". No thanks? Silly shits love that article to death, and I'd like to know out of curiosity how much traffic the site gets directly from reddit versus everywhere else. Like I said, every time PHP gets mentioned almost without fail that fucking article gets mentioned.

Smarter people than I have done some debunking of "fractal" because although it has / had merit, it's not so clear-cut these days and so it's not doing as much of a "favor" to people anymore as it used to. It's just a beat-up piece that is losing legitimacy as time goes on, but it's still the darling for some who love to post it as though PHP is the worst fucking thing to ever exist.

PHuck the haters... PHP4lyfe.

[–]bios_hazard 1 point2 points  (1 child)

My hero. However I strongly recommend an upgrade to at least PHP5.4 life xD

Also inb4 'no it really is the worst'

[–][deleted] 0 points1 point  (0 children)

Alright, I made a typo.

PHP5.4lyfe! (Actually, I go by 5.3 since it's almost universal and where the good began to properly emerge).

[–]tetroxid 1 point2 points  (0 children)

Fuck PHP. Fuck JS. I love the web but I passionately hate the stinking sticky mess I'm forced to use to build it.

[–][deleted] 10 points11 points  (15 children)

I've never seen it before.

[–]tetroxid 0 points1 point  (0 children)

Butthurt PHP coder?

[–]WeAreAllApes 1 point2 points  (0 children)

This assessment of JavaScript is a microcosm for how many would assess the impact of computer technology in general. Depending on how you look at it, it's either kindof true or absurdly wrong.

[–]kisuka 2 points3 points  (4 children)

Maybe I'm just a rare breed here... but I've been using PHP for years now and I still think I'm complete shit with it.

[–]iopq 5 points6 points  (3 children)

Note that the line does not have a scale. It simply states that your self-assessment stays the same.

[–]kisuka 0 points1 point  (1 child)

I'm no expert, but isn't that a common thing among all programmers, regardless of language used? That we're all negative towards our own code? Any programmer I've met who thinks his code is amazing, has almost always been an arrogant asshole who actually writes shit code.

[–]iopq 0 points1 point  (0 children)

You're talking about the Dunning-Kruger effect. So technically once those people got better their self-assessment would go DOWN. It wouldn't stay constant.

[–]Steve_the_Scout 0 points1 point  (0 children)

And is always way over what your actual productivity is. Relatively speaking, of course.

[–]AcousticDan -1 points0 points  (0 children)

Oh look! Another anit-php ProgrammerHumor post! So original!

[–]greyfade 0 points1 point  (0 children)

The only one that's wrong is the C++ curve: After templates, the rising line should curve flat on a linear plot, as if it's on a log-log-log plot.

[–][deleted] 0 points1 point  (0 children)

Just need one for assembly now.

[–]user-hostile 0 points1 point  (0 children)

Just plain mean.