Live data from Hacker News

Simple Ways of Reducing the Cognitive Load in Code

chrismm.com

201–203 of 203 posts

Re: Simple Ways of Reducing the Cognitive Load in Code

#201

Earlier quoted context omitted.

State can be stored in instance variables as well, and should be if many small functions share them, it's what objects are for.

> State can be stored in instance variables as well... but then we are back to the beginning of the thread...

No, we aren't, since objects are the proper way in this case to reduce cognitive load. This notion that all the code has to be in one method to be understood is symptomatic of programmers who don't know how to factor correctly thus leading to the necessity of having to read a methods implementation to understand what it does, i.e. they're bad programmers blaming their shortcomings on well factored code rather than learning how to read and write well factored code correctly. Breaking down large methods into numerous small ones makes the code easier to read and understand, not harder, unless you do it entirely wrong.

Re: Simple Ways of Reducing the Cognitive Load in Code

#202
post #65

Earlier quoted context omitted.

It's more like the pipe operator in ocaml ( http://blog.shaynefletcher.org/2013/12/pipelining-with-opera... ). The lisp version has the extra advantage that you don't have to repeat it between all the intermediate functions. ((->> 2 (* 100) str count) vs 2 |> (* 100) |> str |> count).

I understand how a lisp implementation would work here to require only the single operator (I'm assuming a fairly simple macro). Would it not be possible to do something similar in another functional language to take a and apply it sequentially to a list of function calls?

There are no semantic problems with this, but typing will get in the way: you can express it fairly easily if all the functions have the same type (such as Int -> Int): actually it's just 'foldr ($)'. But it is difficult to type a list of functions such as each member's return value has the same type as the next one's parameter (symbolically, [an-1 -> an, ..., a1 -> a2, a0 -> a1]). It's easier to refer to the composition of such functions, which is why you would see it as 'h . g . f'.

Re: Simple Ways of Reducing the Cognitive Load in Code

#203
post #80

Earlier quoted context omitted.

Stream based programming is a paradigm that with some training and proper code indentation is much , much faster to read than a nested for loop. Once you get used to it,you can literally fast-scan code written in this style with the confidence that you are not missing anything. Also, assuming you do not use mutable state, it also has the advantage of being easily parallelizable without any code changes. (As well, as…

"Stream-based code is certainly not something you can read off the cuff." List transactionsIds = transactions.stream() .filter(t -> t.getType() == Transaction.GROCERY) .sorted(comparing(Transaction::getValue).reversed()) .map(Transaction::getId) .collect(toList()); I honestly think most programmers fluent in Java 7 programming, can guess this is finding "grocery" type transactions, sorting by "value" transaction prop…

Haven't touched Java in a decade and that is beautiful. I like Ruby's functional idioms and LINQ though.
Post reply on HN