News for hackers. Stuff that matters.
Re:What Compsci textbooks don't tell you: Real world code sucks by boothead on Saturday December 22, 2012 @05:30AM (#4956303)
The little bit of haskell FUD in this article made me largely disregard the rest (although I've heard my share of horror stories about the software s big financial institutions). Once you get past the different syntax I find haskell code to be extremely readable.
Re:What Compsci textbooks don't tell you: Real world code sucks by RyanZAG on Saturday December 22, 2012 @05:48AM (#4956332)
Readable to you after years of study of the CS principles and languages behind Haskell, but literally unreadable and undecipherable to the majority of current programmers. This is a major problem, even if you choose to ignore it and remain in an ivory tower.

That said, the actual problem described in the article has this issue at the very core - we have more software we need written than we have people who can write good Haskell. This means that a large percentage of software is going to be written poorly - and the only real solution would be to simply have less software.

Supply-Demand overrides this completely, and so bad software will remain. The true challenge is therefore making software that can be written by the semi clueless and yet still be decipherable enough to maintain. Java and C# seem to have cornered this market to great effect in the business landscape from my experience.

I think current language development trends towards functional programming is a mistake as it caters to such a tiny audience, and more emphasis should be placed on new languages that can force people to write good code.

Is this possible? Dumb example: If someone is hacking together computations of circles without knowing about PI, is there any possible way for a language to help here?

I think a language that can make it clear what the code is trying to do, even when written by someone who has no clue, would go a long way to being able to filter out and fix bad code quicker. If your code is in Haskell, it's going to take a lot of effort to work out what bad code is even trying to accomplish.

Re:What Compsci textbooks don't tell you: Real world code sucks by yummyfajitas on Saturday December 22, 2012 @09:39AM (#4956653)
I disagree. In my experience, competently written Haskell is quite clear to most competent programmers.

I interviewed for jobs a few months back. When people asked me "solve this puzzle in any language you like", I usually used haskell. The solutions were just simpler and cleaner in Haskell than in Python/Java/etc. Apart from minor conventions they were unfamiliar with (e.g., "What's Maybe"), not a single interviewer had a problem understanding what I did, even though most had never used Haskell.

One company even had me demo some code I wrote (great interview technique, BTW), and I picked a Haskell project [1]. They literally stopped my detailed verbal explanations and told me: "no need to explain getFileHandleOrNothing, this all looks pretty clear and obvious. The only thing we needed an explanation of was your monad, the rest is clear."

You advocate that more emphasis should be placed on new languages that force people to write good code. That's what Haskell and other FP languages do.

[1] https://github.com/stucchio/Mp3FS

Re:What Compsci textbooks don't tell you: Real world code sucks by tluyben2 on Saturday December 22, 2012 @05:52AM (#4956336)
So you find the (far more real world) code of Inufu unreadable?

Edit: actually when interviewing people I show them some Haskell code and ask what it does. Most of them do, some answer that they don't know the programming language. Of course I don't give them very difficult stuff, but still; a lot of well written Haskell code is incredible readable, even for people who never saw it. Also, I really assume if you have a CS degree that you played around with functional languages and recursion. My disappointment with a certain country was that almost none (and I interviewed 100s in the past 10 years) of the Masters of CS from that country even have a clue what recursion is, let alone functional or logic programming languages.

Re:What Compsci textbooks don't tell you: Real world code sucks by Woost on Saturday December 22, 2012 @08:13AM (#4956500)
Would something like the above code be on your interview? (pow = 1 : [ 2*x | x <- pow])

I'd probably fail it even though I do understand recursion. I don't know Haskell, and the above is rather cryptic to me. I can infer that it raises 2 to a power, but I'm not sure how it works -- both in how it's called (does that define a function? Pow(4) --> 16?) and the why (the last part defines the recursion I assume, but what happens when you need multiple args, and what does it mean?)

Re:What Compsci textbooks don't tell you: Real world code sucks by tluyben2 on Saturday December 22, 2012 @08:53AM (#4956553)
If the interviewer is not giving the opportunity that the code is just plain wrong and assures it's correct (which I say upfront) then people can be 'wrong' saying it takes an argument x and get the 2^ or it prints the number of 2^argument or that it will just infinitely loop. All of these are fine with me; you read the code and at least got the gist. It's not brilliant because the list comprehension looks like most list comprehensions in other languages so then you can know pow makes a list which elements are (ordered) 2^[1..n] numbers.

Edit: But to answer your question: yes, I would like to be asked. I did a lot of interviews from the employee side just because I was curious, and, at least in the Netherlands, they never gave me a test. Just looked at my (possibly fake if I wanted) credentials and said yes/no. No tests, at all.

Re:What Compsci textbooks don't tell you: Real world code sucks by boothead on Saturday December 22, 2012 @06:06AM (#4956355)
Partially true, it has taken me a long time to learn haskell. However if you want to learn anything there is some upfront effort involved.

I don't get to write haskell at work, but I see its principles everywhere with an additional layer of cruft over the top. Examples include doing functional style programming in python, linq in C#, passing around functions and callbacks in JavaScript and trying to reason about side effects.

Re:What Compsci textbooks don't tell you: Real world code sucks by ps258 on Saturday December 22, 2012 @05:47AM (#4956330)
The code is literally used as an example in Uncyclopedia.

http://uncyclopedia.wikia.com/wiki/Haskell#Haskell_Code_Exam...

It's like using code from the IOCCC to make a point that C code is not maintainable.

Re:What Compsci textbooks don't tell you: Real world code sucks by Inufu on Saturday December 22, 2012 @05:43AM (#4956323)
Especially since this is obviously obfuscated Haskell. A more readable version:

  pow = 1 : [ 2*x | x <- pow]
Re:What Compsci textbooks don't tell you: Real world code sucks by pdw on Saturday December 22, 2012 @08:01AM (#4956484)
Or even

  pow = iterate (* 2) 1
Re:What Compsci textbooks don't tell you: Real world code sucks by Maro on Saturday December 22, 2012 @06:54AM (#4956407)
I recently learned Haskell and am writing code in it on a daily basis, although I'm still a novice.

In my limited experience, Haskell is like any other PL, you can write clean, readable code or a horrible mess.

My major concern with Haskell right now is with maintainability/changability of code. One recent example that bit me in the ass was: I wrote an elegant solution to check whether a list of files exists, using sth like foldr (&&) True doesFileExist. Then I realized I really need to print out which file is actually missing, but only the first N to avoid flooding the user with error messages. At this point FP becomes a pain IMO, and it'd be much easier to add a counter variable and printfs in the imperative program and move on. Actually this is fairly common, think about adding printfs for debugging: not so easy in Haskell, as IO changes the type signatures of the function and its callers. So, in my limited experience, changing code in Haskell is expensive and its effect are not localized.

Re:What Compsci textbooks don't tell you: Real world code sucks by gnuvince on Saturday December 22, 2012 @10:43AM (#4956768)
Regarding your example, I think that one part of the problem (which you mentioned in the first paragraph of your comment) is that you are still a novice. I cannot claim to be an expert in FP or Haskell, but here's how I would go about your problem.

First, I think your problem really has two tasks to perform: getting a list of files that don't exist, and selecting only a part of those inexistant files. It would make sense to decompose these two tasks into two different functions, and since Haskell has non-strict evaluation, we don't have to worry about generating too much data (list of inexistant files) for nothing. Starting with the function to select inexistant files:

    inexistantFiles :: [FilePath] -> IO [FilePath]
    inexistantFiles files = filterM (\f -> doesFileExist f >>= return . not) files
Some Haskellers may prefer to write their functions completely point-free, but I like to have my variables in the code if it helps me read the coder better. And then we can write another small function to select only the `n` first inexistant files:

    takeInexistantFiles :: Int -> [FilePath] -> IO [FilePath]
    takeInexistantFiles n files = takeM n (inexistantFiles files)
        where takeM n = liftM (take n)
(Surely, we'd prefer to just write `takeM` and call it directly on the result of `inexistantFiles`, but I prefered to go with the specialized function.) Someone who knows Haskell better than I do could certainly come up with a cleaner solution, but I think the code is readable, solves the problem you mentioned and isn't any harder to understand than the equivalent imperative code.

Have a Merry Christmas!

Re:What Compsci textbooks don't tell you: Real world code sucks by mifrai on Saturday December 22, 2012 @02:11PM (#4957213)
and since Haskell has non-strict evaluation, we don't have to worry about generating too much data (list of inexistant files) for nothing.

Be careful here. Because you're in the IO monad, the entire list of files will always be computed first (ie. doesFileExist will be called for every file given).

Non-strict doesn't really buy you much here. The "expensive" operation here will always be executed. At most, you're avoiding converting some list-consing-thunks to a list.

Re:What Compsci textbooks don't tell you: Real world code sucks by Maro on Saturday December 22, 2012 @11:22AM (#4956835)
Thanks, great comment.
Re:What Compsci textbooks don't tell you: Real world code sucks by emgreen on Saturday December 22, 2012 @07:53AM (#4956470)
From my experiences with Haskell, it takes a long while to get used to Haskell, but once you do it really pays off, especially in the area of maintainability. It's actually one of the reasons I love Haskell so much. I've been where you are though, and I feel your pain - if you keep at it, it'll get good, promise.

Some things I find help with maintainance : * the type system allows the compiler do a lot of work for you, * types have to be concrete rather than abstract, * if a little effort is applied the unit tests can be amazing, * everything's clearly defined, * codebases tend to be a bit smaller, and once you're used to it, more readable.

In the particular situation you describe, maybe using the "take" function could work?

Yes, the IO stuff can be tricky, I agree, but with practise it's not too bad, and separating IO from pure code gives all sorts of goodness back. There's always Debug.Trace if you need to do a quick bit of debugging by print statements.

I feel the article was being unfair on Haskell there, after all the code example was being deliberately unreadable, and that's possible in any language.

It's not really that hard:

map (2^) [1..]

Re:What Compsci textbooks don't tell you: Real world code sucks by danidiaz on Saturday December 22, 2012 @09:46AM (#4956667)
For printf debugging you can use the "trace" function from Debug.Trace.

A "first n files that don't exist" function could be something like:

nexist :: Int -> [FilePath] -> IO [FilePath]

nexist n l = take n . map fst . filter (not.snd) . zip l <$> mapM doesFileExist l

Re:What Compsci textbooks don't tell you: Real world code sucks by Maro on Saturday December 22, 2012 @11:30AM (#4956853)
I hope you don't take offense at what I'm about to say, but the code snippet you provided is not maintainable in the sense of not being readable. It sacrifices maintainability for cleverness. As someone above pointed out, sometimes it's better to write 5 lines instead of 2 lines.

We don't write code primarily for the compiler, if we would then we wouldn't be having this discussion because the compiler doesn't mind spaghetti. We write code for the next guy who will try to read, understand and modify the code. As an experienced programmer I can tell you, but you probably know anyway, that in all languages you can write very concise but completely incomprehensible snippets which even you won't understand in 2 weeks.

Re:What Compsci textbooks don't tell you: Real world code sucks by mifrai on Saturday December 22, 2012 @04:52PM (#4957602)
Honestly, I don't think danidiaz was trying to be clever so much as not knowing about the existence of filterM. A good chunk of the code was just an implementation of it:

    filterM' f xs = map fst . filter snd . zip xs <$> mapM f xs
So, given filterM, the code would have looked like

    nexist n xs = take n <$> filterM (fmap not . doesFileExist) xs
Which doesn't look clever at all and it's fairly easy to understand.
Re:What Compsci textbooks don't tell you: Real world code sucks by danidiaz on Sunday December 23, 2012 @04:21AM (#4959054)
Yeah, I had forgotten about filterM.

By the way, I like the (fmap not . doesFileExist) bit. It´s cleaner and less cluttered than (\f -> doesFileExist f >>= return . not)

Re:What Compsci textbooks don't tell you: Real world code sucks by Maro on Sunday December 23, 2012 @03:41AM (#4958993)
Cool, thanks!
Re:What Compsci textbooks don't tell you: Real world code sucks by michaelochurch on Saturday December 22, 2012 @08:58AM (#4956563)
So, in my limited experience, changing code in Haskell is expensive and its effect are not localized.

That's a feature, because it protects against spaghettification. Explained here: http://michaelochurch.wordpress.com/2012/12/06/functional-pr...

When a manager walks in with a new requirement, you'd rather be able to do it in 15 minutes than 2 hours, because managerial injections are annoying and you'd rather get back to more interesting work. Sure. I get that.

However, once enough hands have passed over code, with hackish fixes thrown in to satisfy dipshit flavor-of-the-month requirements, then if it's easy to inject complexity without consideration of the loss of conceptual integrity, people will do so. What you get over time is spaghetti. It doesn't happen overnight. Almost no one sits down and says, "I'm going to write a bunch of shitty spaghetti code."

Spaghetti code is rarely written by one person. It's an "all of us is dumber than any of us" phenomenon. Even Google has some spaghetti code, and I've never met an incompetent engineer at Google. (Project and people management would be a different discussion.)

The nice thing about FP is that changing behavior (except for performance) actually requires altering an interface. In a statically typed language, this requires that you make changes elsewhere in the code to accomodate the new information flow. Yes, it's time-consuming, but it also forces you to think about how your change is going to affect the whole system. That's a good thing.

Writing code shouldn't be easy, because reading average-case code is impossible. If it takes 50% longer to write code, but the result is that people can still comprehend it at scale, I call that a major win.

Re:What Compsci textbooks don't tell you: Real world code sucks by Maro on Saturday December 22, 2012 @11:20AM (#4956828)
> to think about how your change is going to affect the whole system. That's a good thing.

I don't think you've made a coherent argument why that's a good thing. Putting in a printf for debugging is not spaghettification and does/should not affect the whole system.

> Writing code shouldn't be easy

Eh?

Re:What Compsci textbooks don't tell you: Real world code sucks by michaelochurch on Saturday December 22, 2012 @11:53AM (#4956898)
You can printline for debugging using the trace function in Haskell. Or use unsafePerformIO, which is usually outlawed for production code but just fine in a transient state for debugging.

On "writing code shouldn't be easy", you're right that this statement, out of context, seems ridiculous. What I mean is that, when there are trade-offs between making it easy to write vs. read code, generally the reader's interests should be favored, because legible code is so rare. Obviously it's better for it to be easy to write code and to read it, but ease-of-writing is a secondary priority for large, production systems.

Re:What Compsci textbooks don't tell you: Real world code sucks by Maro on Saturday December 22, 2012 @01:35PM (#4957121)
I pretty much agree with everything you say here. Thanks for the trace and unsafePerformIO tips.

> ease-of-writing is a secondary priority

In my experience, once you have enough experience and witnessed the suck of maintaining crappy code, writing good code is second nature and doesn't take conscious effort.

Re:What Compsci textbooks don't tell you: Real world code sucks by michaelochurch on Saturday December 22, 2012 @07:54PM (#4958082)
There's a lot of truth in what you're saying. However, if maintaining shit code turned people into good coders, then there'd be a lot less bad code (because the precursor is so damn common). I think it also requires curiosity, desire to do things right, and access to the right information (which is now trivial if one knows where to look).

What I would actually say is: the differential in difficulty between writing good code and writing bad code should be minimal or, if possible, negative.

Re:What Compsci textbooks don't tell you: Real world code sucks by Maro on Sunday December 23, 2012 @03:38AM (#4958989)
Actually I disagree on your first point. My first job was maintainenance programming, and for some reason I stuck around and did it for 3 years. That has taught me the suck of trying to maintain and work with shitty code.

Now, X years later I still tell stories about that first job, and I always remember how I felt maintaining spaghetti code when I write code.

BUT, I find that most of my fellow programmers either have not worked as m.programmers; or have done so but didn't notice it, because I guess they don't have a built-in desire for arete or just didn't want to get upset about their work and chose to ignore the crappy code.

Re:What Compsci textbooks don't tell you: Real world code sucks by ethanhunt_ on Saturday December 22, 2012 @08:28PM (#4958186)
> it also forces you to think about how your change is going to affect the whole system. That's a good thing. How do you reconcile that with software's pervasive idea of abstractions, which lets us avoid exactly what you say is a good thing?
Re:What Compsci textbooks don't tell you: Real world code sucks by michaelochurch on Saturday December 22, 2012 @11:48PM (#4958612)
Abstraction is a good thing, but what I'm talking about here is object-oriented programming. An "object" is something of which one has incomplete knowledge. OOP is, I would argue, about managing incomplete knowledge, on the (correct) assumption that if systems get large, it will be inevitable that people don't understand all the details of what they're working with.

Good abstractions deliver a lot of value. Files and sockets, for example, should be treated as objects, because the client should (usually) be agnostic of how the things are actually implemented. So OOP isn't all bad.

What's messy is a paradigm that gives an unbounded right to inject complexity. Programmers don't have enough autonomy in most corporations to do things right, so the result in most cases of Brownian-motion requirements is an increasingly gnarly pile of hacks. What Haskell does well (being both functional and statically-typed) is punish that style of development while it occurs, making it so that the kludge isn't always the path of least resistance.

Re:What Compsci textbooks don't tell you: Real world code sucks by tluyben2 on Saturday December 22, 2012 @05:40AM (#4956320)
Yep, it's a) only syntax (if you cannot get past syntax maybe a you need to get another job) b) a bad example in the article c) an example of how to make code unreadable which goes for every programming language. In general Haskell programmers strive to refactor to a point where you have balance between readable, performant and idiomatic Haskell.
Re:What Compsci textbooks don't tell you: Real world code sucks by p4bl0 on Saturday December 22, 2012 @05:52AM (#4956334)
"Syntax doesn't matter… unless you're a programmer." ~ Rob Pike.

I otherwise agree with your b) and c) points.

Re:What Compsci textbooks don't tell you: Real world code sucks by tluyben2 on Saturday December 22, 2012 @05:54AM (#4956339)
There is no need to torture yourself with insane syntax and I should express that probably differently, but I find the obsession over syntax usually a lack of skills or experience. For instance, there are hordes of programmers screaming they 'cannot do' Objective C because of it's syntax. That's silly.