7 ms·
Ineffective coding habits many F# programmers don’t have
- 201709User 9y agoTimes whitespace misused as meaningful: C# - 0, F# - 38255. Times had to re-order files to build a project: C# - 0, F# - 19. Experienced non-arrogant developers found for hire: C# - 10000000, F# - 12.
- deleted 9y ago[deleted]
- NHern031 9y agoI'm glad those hiring numbers aren't accurate. Otherwise we I wouldn't be employable.
- Roboprog 9y agoHow many frustrated developers are identified as "arrogant"? I for one am tired of OOP (a la Simula 67) presented as the be-all and end-all :-( The functional programming guys were right, and C++ set back the industry several decades. (better to have both FP and OOP, but OOP-only stinks)
- nickpeterson 9y agoNah, we like you even despite this. We want you to like F# as much as we do. Come on over, we have cookies!
- icc97 9y agoI do wish more people had this attitude. There's enough other things to fight about than who uses what code.
- mathw 9y agoWhitespace IS meaningful, to the programmer. Having the compiler understand that is great, because it means I no longer have to express my block structure twice. It's one of the few things I regret about Rust's decisions, that they went for a C-like curly braces syntax.
- hardlianotion 9y ago'Many' is the weasel word here.
- stephengillie 9y agoThere's an anti-pattern where a function will be declared entirely in a callback, instead of declaring the function in another location and referencing it locally. It's difficult to read and logically follow. This seems especially common in Javascript. Is it just a sign of novice?
- yen223 9y agoDeclaring the function in another location is the antipattern. Code locality is important for being able to maintain contextual information when reading code. Declaring a callback in a different location from where it's used means one extra level of indirection for no good reason. Also, not sure how this is relevant to anything.
- stephengillie 9y agoDeclaring functions in another location is the purpose of functions, yes? Otherwise we would code in a purely linear fashion. Some code locality should be sacrificed for readability, reuse, and clarity. Not to make a "forest of functions", but to define separate ideas more clearly than stuffing one thought into a parameter of another thought. It's like if I was telling you about something funny that happened when I got to work, but first I have to describe every detail of my drive to work... It's a thread on ineffective coding habits, so I thought I would piggyback with a general question. I regrettably did not pursue an undergrad CS degree, and so sometimes wonder about the gaps in my education such a degree would have filled. Thank you for your time.
- Roboprog 9y agoIf the function is large, sure, make it into it's own name thing (with as restricted a scope as you can get away with). However, many languages support callbacks/lambdas/closures/blocks as parameters to generic library code, and these dangly bits are often one or two lines. E.g. (pseudo-code) - * list.for-each : print * list.filter : is-wanted * db-connection.bracket-transaction : compound-update This allows you to use, or create your own, library code as extensions of the language to reduce boiler-plate code for visiting, filtering, resource management, etc. Have a look at something like the Ruby language to see how this is exploited to reduce A LOT of code noise.
- maxxxxx 9y agoI hope F# people can do better than this. Not very convincing.
- melling 9y agoWell, if the code does the same thing and it’s 1/10 the size, that would be convincing. Don’t recall seeing this sort of comparison for ML languages in the past, however.
- jackmott 9y agonot usually 1/10 almost always at least 1/2, sometimes approaching 1/10 if a class hierarchy is a large % of the project and it can turn into a discriminated union.
- sidlls 9y agoThere are plenty of such comparisons (a famous one comparing Haskell to C++ comes up every so often). Most of the time there is a generous positive bias toward the favored language and excessive abuse of the worst features of the one that is the target of ridicule. It's not just FP versus Other, either; it's sort of an industry meme in some sense.
- stephengillie 9y agoReadability is why I use "closing flags" so I don't get lost in a sea of closing curly brackets. Example: if ($TreeHasLeaves) { Write-Host -f green "The variable TreeHasLeaves is $($TreeHasLeaves)" }; #end if TreeHasLeaves My "GilLang" auto-adds these closing flag comments after each closing bracket - currently it only supports Powershell, but the goal is to streamline basic coding (if/then, for/foreach, do/while, try/catch) in many languages. To create the above function statement, curly brackets included, just type use "New-FunctionStatement if TreeHasLeaves -Powershell".
- na85 9y agorainbow-delimiters-mode is great for this, if you're an emacs user.
- stephengillie 9y agoIs this a good example?[0] That would be helpful, and I hope Notepad++ gains the feature. Notepad++ does have a bracket helper that highlights the other bracket, but flagging the closing bracket helps when there are several brackets. [0] https://raw.githubusercontent.com/thblt/eziam-theme-emacs/master/screenshots/rainbow-delimiters.png https://raw.githubusercontent.com/thblt/eziam-theme-emacs/ma...
- leephillips 9y agoVim has that, too (as a plugin). Great for lisps.
- vorotato 9y agoVscode has both rainbow delimeters and rainbow tabs plugins, highly recommend both.
- duedl0r 9y agoI think that's readable just for you, if you're used to it. For me it's just noise. Why would you need it anyway? My take is, to minimize nesting and scope, so I can see right away where the scope starts and ends.
- weberc2 9y agoI like F#, but I think the "F# has objectively better syntax" claim needs a lot more support than the "signal-to-noise-ratio" argument. I don't know of anything that suggests that humans are better at parsing whitespace-delineated arguments than say, comma-delineated. My experience (probably biased similarly to F#'s target audience generally, possibly with above average exposure to functional languages) suggests that C-like syntaxes are easier to read.
- aloisdg 9y agoAsk Python dev. Most people found Python code easy to read thanks to whitespace.
- weberc2 9y agoI am a Python dev (by day, anyway), and whitespace doesn't make Python easier to read. But Python is still much easier to read in many cases than F# because Python retains parens and commas and the single-expression lambda syntax hampers gratuitous nesting. Go and Rust are even easier to read than Python, largely thanks to curly braces. I still prefer F# to Python, but not because of the syntax. NOTE: I'm not saying C-like syntax is objectively easier to read; I'm merely (very) skeptical of the OP's claim that F#'s syntax is somehow objectively better.
- aloisdg 9y agoI am a C#/JS dev by day. In my own time, I write in F#, Python, TypeScript, C (used to), C++ (used to). I have trouble with brackets nowadays. Each time I wrote some, it feels rubbish. I should try go one day :) Note that F# is the only _truly_ (not purely) functional language I am used to.
- weberc2 9y agoI think you should learn Go, but I suspect you won't like it very much.
- goatlover 9y ago
- sremani 9y agoI know for sure I am "stealing" some one else's analogy here (apologies in advance). In .net world, C# is the lego blocks and F# is the clay.
- ahelwer 9y agoI have no idea what this analogy could possibly mean.
- Ensorceled 9y agoMy kids stopped playing with clay and play dough and graduated to lego at around the age of four.
- vorotato 9y agoToo bad, they could be designing cars :P. (car designers still build their model in clay)
- 201709User 9y agoClay is impossible to refactor once finished.
- vorotato 9y agoWhereas F# is very easy to refactor
- mannykannot 9y agoThe twice-implemented requirement mentioned near the start of this article seems to have the potential for being the basis of an informative case study, but this was not it - instead, it is just another article pontificating over the minutiae of syntax (the linked article may have a more informative analysis, but the link seems to be broken.) At one point, we read "your abstractions should afford the right behaviours whilst make it impossible to do the wrong thing." Well, that would undoubtedly be helpful, but Turing may want to have a word with you about the feasibility of guaranteeing it. What does F# do in this regard? We are told it does not have nulls, which is nice, but what does it do to avoid someone introducing data that are semantically, rather than formally, null values (NaN, for example?) The section on testing does not seem to contain anything that is F#-specific. it mentions QuickCheck, but that has apparently been implemented for many imperative languages, including C#. Is this sort of thing more effective for functional languages? There is no discussion of the possibility here. I am actually persuaded that there are benefits to functional programming, but articles like this paradoxically give the impression that there is not much substance behind the large claims. The first thing I would like to know about the case study was whether the implementers of one version benefitted from the problems encountered by those of the other, especially with regard to getting the requirements nailed down.
- brians 9y agoTuring hardly prevents it; indeed, by the Church-Turing thesis, shows us how it’s done: write an abstract model of your problem space. This is your types and type functions (like Option/Some/Maybe). Write a type that is the theorem claiming you get your output from your input. Now you guarantee you cannot do the wrong thing. Affordances for the right behaviors? Those are library functions you build bottom up, or assemble from a library of structures (functor, monad, etc.) and your types. As all affordances, there are art and craft here. On semantic nulls, the compiler typically enforces completeness on case analysis.
- mannykannot 9y agoYes, I should have scrubbed that paragraph and started over, to say something like eliminating nulls is only part of the problem, and you still have to do everything right, starting with your abstract model of the problem space. With regard to semantic nulls, I offered NaN as an example, but I was thinking of the case where the programmer introduces what is, in practice, a semantic null value for one of his user-defined types - something that should be distinguished as a special case, but isn't.
- mamcx 9y agoIs ironic how most failed to see the main points, and focus in the syntax (syntax is easy to attack because the cargo cult notion of "syntax not matter much"). I have learned and used FOR WORK almost a dozen languages now, F# being the last of the bunch. I totally remove many ineffective coding habits, mostly: - Inmmutability - The type checker making me aware of problems - Not need to over-check for nulls because now I don't need to worry - Pattern matching + AGDT are awesome. This cut the need to make a lot of unecessary classes. - Make me to think outside over OO. I do this also in Pascal(Delphi) and python, and hate how C# is OO/Class for all. --- About syntax: F# totally cut millons and millons of lines of code! (exagerating!) but more important: Far less files than similar C# project (and yes, I have made the rewrites. I do rewrites for life! I work upgrading, fixing, moving, old codebases most of time). However, some things that I hate of F#: - Whitespaces must be as python. Is better. - Not like to rely on type inference. Is a good idea on paper but have the stupid habit of only work AFTER you write. Put the types manually is better, IMHO. You get more clarity, ease readability, not need to rely on a IDE, the types are less obtuse, etc. Ironically, VS Code show the types as you write but Visual Studio Mac no. I have both opened some times because this! So, type inference is net negative for me. Maybe is my pascal love talking here. I don't like how everything is "let". I wish it was "let, function, etc". I don't like the overuse of spaces for everything. And how comas are for tuples but ; for separate items. But overall? F# is better than C# most of time.
- mannykannot 9y agoYou say that "syntax not matter much" is a cargo cult notion. I am not sure you understand the cargo cult analogy, but I take it that you disagree with the sentiment, which is itself ironic, given that the reasons you give for favoring F# are all much more than syntax issues. They could have been the basis for a more persuasive article.
- mamcx 9y agoOk, is not well articulated: > F# are all much more than syntax issues But some of the things that make F# unique are surfaced with the syntax. Is not just there for the sake of it. Pattern matching is the most obvious thing. The way the syntax present this aspect is close to perfect... ie: I'm for fun building a language. I have tried a lot of ways to present some things, like Pattern Matching. I can't figure a better way without resorting to weird characters, unicode or text editors that not exist. (I replace "|" as separator but the basic structure is just the same). Other ways the syntax is significant is in how it encourage curried functions, chaining and pipelines (with |>). The pipeline way is something that I truly adore. Wish F# was more into this, IMHO. Not fan in how let have different meanings, neither the use of "," for tuples. Also is confunsing why functions with "let sum a b" are truly different to "let sum(a,b)". Hate this (because suddenly pipelines and curried application not work anymore). The making of "inmutable first" and marking some vars as "mutable" is something truly nice. Could be easier if it was "let" and "var" instead of "let mutable" but anyway. Is good. Anyway, as a amateur language designer it make me more aware in how the syntax make a language better or more ergonomic and lead the way in how use it.
- vkou 9y agoThe visual honesty example is dishonest. It uses two indents for indenting the parameter list of the function, and two indents for indenting the body of the function. Most indentation conventions suggest four space indentation for breaking up a list of parameters, and two space indentation for the body of a {}. In order to avoid the problem that the example criticizes.
- Ensorceled 9y agoMy reaction was similar - "Who formats code like that?" It's pretty easy to be better when starting from such a low bar.
- knieveltech 9y agoI take issue with the notion that comments are "noise".
- eradicatethots 9y agoI don’t
- nomel 9y agoI've never once come back to my own code, say, three months later, and wished I had less comments.
- kruczek 9y agoI did, although I admit it was only for awful projects, where people used to comment out code instead of deleting it... hundreds of lines of commented out code and occasionally one or two lines uncommented in the middle of that. Not fun to maintain code in such state.
- icc97 9y agoCommented out code, is not a comment about the code. It's just someone not clearing their trash.
- knieveltech 9y agoI'm glad we don't work together, I'd surely get you fired. ;)
- xelxebar 9y agoI take a philosophy that "executable comments" are better than "static comments". So instead of writing a comment to explain some gnarly bit of logic, I'll try to do the reverse of inlining and break pieces into little, suitably-named functions. The vast majority of my comments are either javadoc-style function protocols or high-level discussion on subsystem purpose and architecture. Leaning on this commenting style, I find that looking at past code of mine is much easier to re-grok than having lots of explanatory comments everywhere.
- Roboprog 9y agoForgive me if I read this article as "Scala vs Java", since I'm on the JVM instead of .NET :-)
- Roboprog 9y agoNot that I'm that enamored with Scala, just that Java sets the bar so very low.
- sidlls 9y agoThe link to the article with the comparison doesn't work to me. Often what I find in these articles is that the second implementation takes much less time and has less code and fewer bugs primarily because of the lessons learned the first time round. It has almost nothing to do with the language of implementation.
- johnnyb9 9y agoI wonder what version of C#? C# 7 and 6 are less verbose.
- mathw 9y agoOnly in a few areas.
- dgudkov 9y agoThe idea behind the article is good, but the implementation leaves to desire. I can confirm that F# develops good habits in a C# programmer. My favorite acquired habits are: 1. Intolerance to nulls and absence of object intializers in constructors. Use of zero objects a-la Array.empty. 2. Semantic typing even for simple types (e.g. type Name = string).
- xelxebar 9y agoCompletely off topic, but this site along with a few others like StackOverflow seem to hijack my browser history and break the back button. In order to back out of their domain, I have to quickly bang the back key twice. I have no idea what causes this. I even run with Javascript completely disabled.
- Grustaf 9y ago"controlFactoryFactory" seems to me a pretty apt name for a factory that creates controlFactories, if that's whatbitbis. What else could you name it really? "controlMetafactory" perhaps.
- vorotato 9y ago“The C# project took five years and peaked at ~8 devs. It never fully implemented all of the contracts. The F# project took less than a year and peaked at three devs (only one had prior experience with F#). All of the contracts were fully implemented.” wowzers