“F # is no harder to master than Entity Framework or WPF”: Interview with Scott Vlashin





Who should I ask about F #, if not a person who has dedicated a detailed website to this language? Scott Vlashin created the resource "F # for Fun and Profit" , familiar to many Habra residents: from Habré they translated both the series of articles "Functional Thinking" and the article "Railway-Oriented Programming" from there.



And in November, he will speak at our DotNext conference in Moscow with the report “The power of composition”. And in anticipation of this presentation, we asked him about F # and generally functional programming.



- Let's go from the very beginning: what did you do before functional programming, how did you come to F # and how did you create the site?



- I am a man of venerable age, and when I was at university, there were no separate computer science programs yet. I received a mathematical education, but did not want to do math, so after university I worked for about 10 years on a variety of jobs, including a carpenter.



One fine day, in the late 1980s, my father bought a computer, CP / M Kaypro, with a tiny amount of memory and 5.25-inch floppy disks for his work. This was before Windows appeared, so DOS stood on it. It was on it that I started programming. I was engaged in databases, at first for my father, he needed it for his work. And then I began to do it professionally.



My first language was Turbo Pascal, and in 1989 or 1990 I met Smalltalk, and I really liked it, it is still one of my favorite languages. One job replaced another, and in the end I, like most programmers, got a job at a large company to write boring business applications (I call them “BLOBs”: Boring Line-of-Business applications). And for a very long time he was doing just that.



For some time I wrote in Python, about 10 years - in C #. And in 2011, that is, not so long ago, I decided that I was tired of my work and it would be nice to try something new; so I wanted to do functional programming. It turned out that my Visual Studio already had a functional language, so I tried to understand F #. And at first it seemed very strange, I couldn’t understand anything, it was so different from everything that I had worked with before.



There were several good blogs on F #, but there were very few of them, and there was not much documentation either. As a result, my friends gave me excellent advice: if you want to study something properly, try to start teaching others about it, because it makes you very well understand the topic. In addition, I was advised to start a blog, this allows me to stand out among other programmers.



In general, in 2012 I started the blog “F # for fun and profit” and began to publish articles in it every time I learned something new about F #. Now there are several hundred pages, and it has gained great popularity. At first it was just a hobby, I worked on it in my free time. And about 3 or 4 years ago, I decided to quit my job and become a freelance consultant. The year before last, I wrote a book, which also turned out to be quite popular. And so my acquaintance with F # took place.



- How do you work as a freelancer with F #, or not only?



- Basically, F #, although generally speaking, what they will pay for - that's why I advise * laughs * . If I need money, but someone has work on C #, and it looks interesting, I take it. And last year I worked with Python for three months. What matters to me is not the language, but what kind of problem it is necessary to solve. I like to study, and when you are hired to solve a problem, you have to become, if not an expert, then at least sort out a new area.



In this way, I had to study real estate economics and insurance risks. I believe that good code can only be written when you have a good understanding of the subject you are doing, and not just write what others are telling you. For me, this is the most interesting - not a language, but a problem.



- In Russia, although some developers have an interest in F #, business with this language is difficult: it is more difficult to find or replace a developer than with C #. And those companies with which you work, how are they solved in F #?



- There are two most common situations. The first option is when the company already uses F #, they usually have some kind of pilot project. They call me and ask me to help them launch this project and teach them the language. Usually they are ready to spend about six months on such a project to find out if they want to do this further.



In addition, I am engaged in teaching people domain-driven design, and here F # is not in the spotlight, but I use it as a language. I show programmers accustomed to C # how much shorter the same code in F # can be than in C #. That is, I quietly promote language. It helps that you don’t have to switch to F # entirely, you can write a domain model in F #, and everything else in C #.



- You say that C # and F # can be used together. But in C #, the Entity Framework, NHibernate, or something similar is most often used. And among developers on F # this is much less popular. How to mix these languages ​​taking into account the difference in approaches?



- One of the companies I worked with uses the Entity Framework. They tried to switch to the Ports and Adapters architecture, that is, to remove all input / output operations from the core of the architecture. For this, the Entity Framework is pretty bad. In such situations, it is much more convenient to use something like Dapper, which allows you not to deal with SQL in the middle of your code. Among other things, this simplifies testing.



Let them not use functional programming, but the situation still pushes them to have a clean core of the program, and keep the database somewhere on the periphery. If thinking has switched to this format, then this is an important step towards abandoning Entity. In such a company, I, in fact, would not have changed anything. You cannot force people to change; they must themselves want change. I’m not trying to sell myself and impose on someone the way that I think is the best. Usually people already want to change, and I just help them achieve this. Do you understand what I'm talking about?



- That is, your customers are companies that themselves are already moving to a more functional approach.



- Even if they work with C #, they switch to a more functional C #, start using LINQ, immutable data structures, that is, in general, go in this direction. Therefore, for them, switching to F # is no longer a big leap.



Are the professions of a developer and a carpenter similar



- You have an interesting thread on Twitter comparing the work of a programmer and a carpenter. I would like to ask about “functionalism”, starting from this thread. But can you tell the essence of it for our readers?



- Developers like to compare themselves with engineers, and software development - with the construction of buildings or bridges. And there is much debate about whether programming is really close to these activities, or whether they are fundamentally different. Like, we have requirements for the project changing every day - when you build a bridge, probably everything is completely wrong? Or is it really so there too?



I believe that in this dispute there is no single correct answer. I have never been an engineer, but I was a carpenter. And I can say that carpenters have a lot of different work, very different in format, and each needs its own approach.



For example, in one of the works I did kitchen cabinets. In America, they are all very standardized, all the same size, tailored to each other, and work is being done with power tools. It is necessary to provide some quality, but in America, the old kitchen is usually thrown away when the house changes its owner, that is, it will not serve for a very long time. So in this work, everything is tied to speed and cost savings.



Then I had another task, where I needed to replace a large, 6-inch oak beam in the middle of a room in a building that was 400-500 years old. Here, everything was the other way round: everything was curved, no right angles, and to replace it, it was necessary to manually fit a new piece of wood so that it had exactly the same shape as the old one. This required a lot of accuracy.



Finally, there was the third work in which I did the scenery for the stage. They were made of plywood and very thin wood for props.



My idea is that each work requires its own approach. In the case of kitchen cabinets, precision, the use of power tools and reproducible results are needed. In an old wooden house you work with a legacy system, it is important to be careful, not to rush, you need not speed, but the correctness of the result. Finally, in the case of decorations, you deliberately create a fragile structure that does not have to be strong, you often have to cut it and assemble it again in a few minutes, such structures do not last forever.



When they say that programming is similar to engineering, this is true only for certain types of programming. For example, if you are writing software that controls an airplane, you need to be very careful and achieve very high accuracy. A completely different thing is a one-line script to search for files, this is more like creating scenery. There is no point in spending 20 hours proving that this script works and writing 1000 unit tests for it. All work should take no more than 5 minutes. And when you work with an inherited system, you need to maximally fit your code to the existing one; large refactoring is undesirable here.



That is, in each case, context is important. Sometimes you need to plan a lot, think a lot about a project, write a lot of tests. In other cases, it’s enough to whip up something. Many people lack flexibility in this regard, they believe that if you do not use unit tests or use some programming language, then you are not a professional. In fact, the idea that it all depends on the context is pretty obvious. What is surprising, rather, is how stubbornly some programmers insist on their ideas, and if you at least somehow deviate from their ideals, you are immediately sent to the black list. In my opinion, this is stupid.



- You say that for an outside observer the activity looks uniform, but when you see it from the inside, completely different cases are revealed. And I want to ask: is it the same with functional programming? Those who look from the outside have a common stereotype, but in fact there are gigantic differences?



- That's right. From the outside it may seem that all “functionaries” think alike, but there are many different groups that argue with each other: supporters of Haskell, F #, Clojure, Elm. Even inside F #, there is strong disagreement as to which direction this language should evolve - should you try to imitate Haskell or should ease of use be a priority. So you are right, inside this field is much more diverse than it is usually represented by outsiders.



- For differences in the work of a carpenter, you have given very illustrative examples. Can you illustrate the differences in functional programming with specific examples too?



- There is a school of functional programming, which believes that you need to try to prove everything, and so that everything is mathematically perfect. This school uses a lot of mathematical jargon, for example, “monoids” or “monads”. These are mainly Haskell users, and the academic environment is very influential.



And there are people who are more important to achieve results. They are interested not so much in mathematics as in immutability and I / O removal to the periphery. The best example of this approach is the Elm community. Here they are primarily involved in the creation of web applications. In contrast to the first group, here they do not consciously use mathematical jargon, and they consciously refuse the part of the functionality that is in Haskell and which Haskell users consider vital.



In addition, there is a dispute between proponents of strong typing and dynamic typing. In the layman's view, functional programming is something like Haskell or F #, but besides them, there are languages ​​like Clojure that have dynamic typing and a completely different approach to solving problems. If you collect all this motley company in one room, they can fight. But I think that all approaches have their own reason, and when I work for someone, I don’t tell them that their approach is wrong.



- Many are frightened by the mentioned “academic nature” (“F # is rooted in ML, which is for rigorous scientific evidence, but I solve real problems here”). But it turns out that people are afraid in vain?



- In general, it seems strange to me that so many people are accustomed to considering academicity as something negative. Well, that is, how, some consider it negative, others - positive.



The fact is that many of the technologies that we now use in programming have arisen in the academic environment, for example, garbage collection or types. So there is nothing wrong with academic methods per se. Another question is that an excessive emphasis on them can be harmful, because scientists and programmers have different goals.



Although functional languages ​​have academic roots, it seems to me the right conscious decision to hide this logic in languages ​​such as F # and Elm. Therefore, F # is not used to prove theorems, but to solve real problems, it is a very pragmatic language. And universities have now switched to even more complex languages, such as Coq, F * and the like. They are much more academic, and are used to prove theorems.



As I said, scientists and programmers do different things. Programmers spend most of their time reading and writing files, working with databases, displaying data on the screen, checking entered data, converting them, etc. But scientists are not interested in such things. But the fact is that things that were purely academic 40 years ago may not be so today.



- As you yourself said in connection with the work of a carpenter, different approaches are good in different contexts, there are no universal approaches. And specifically, F # is also best suited to certain tasks. What are these tasks?



- Yes, this is definitely not a universal language, I definitely would not recommend using it at all for everyone, it would be stupid. But it seems to me that F # is an excellent replacement for C # - with the exception of tasks requiring very high performance. Programming in F # is based on a completely different approach: immunity, structural equality, explicit dependencies, F # does not have null values, and so on. And it seems to me that this approach is much more useful in solving everyday programming problems.



Therefore, if a person uses C #, he should definitely ask about F #, this language will help make the code better. As for other applications, it seems to me that F # would be well suited for many tasks for which Python is now used. F # and Python are very similar, and it seems to me that F # has great potential for data processing. So far, there is still a lot of work to be done in this area, but perhaps in a few years people will use F # for various things related to big data and data science, for which Python is now used.



Finally, F # is very convenient to work with JavaScript. In general, nobody wants to work directly with JavaScript, so there are many languages ​​that compile in JS: for example, ReasonML (which runs on OCaml) and Fable (which runs on F #). Personally, I prefer to work with any of these options than to deal with JavaScript, so when working on the frontend, I would choose something like Fable. So these are the three main areas in which F # shows its best side.



- As you noted in your report “F # for C # developers”, the main thing in the language is not syntax, but philosophy. But here lies the difficulty for those who want to quickly understand "whether this language suits me." You can already understand if you like the syntax by a cursory acquaintance. But how long does it take to understand the philosophy of language?



- A person who writes in C # can quickly figure out a language like Java or Go, because most of these standard languages ​​have about one imperative model. Going from them to F # definitely requires a lot of effort, and that stops some people. In my experience, F # is much easier to learn if for a while you forget everything you know about OOP. Otherwise, you start to transfer all sorts of things from C # to F #.



As for the time, somewhere in two weeks of training it is already possible to start writing working code, and it will take several more months to more or less get used to the language. Finally, for a good level of ownership you need more time, 6 months, maybe more - this is if we are talking about sorting out all the libraries, idioms and the like.



But honestly, switching to F # is no more difficult than switching to Entity or WPF. They also require a lot of time. Do not underestimate the necessary efforts, but sometimes they say that this transition takes years. I repeat: in order to start writing code, it takes several weeks to get comfortable - several months. I say this both from my own experience and from the experience of other people with whom I spoke.



Do I need to know C # before F #



- It is clear that among those using F # the majority came to it from C #. Are there many who come to F # without C # experience?



- There are not very many such people, and it’s quite difficult for them, because all libraries have documentation for C #, so people everywhere come across examples from C #. But still there are such people, and in addition, F # is taught at several universities.



There are those who are trying to learn F # after Python. The problem is that F # is very dependent on .NET, and .NET is tied to C #. The same situation with Visual Basic, there are also all the examples in C #. Let's hope that in the next few years this situation will be able to change and make the language easier to learn, now this is one of the important problems.



- If students are given both C # and F #, then usually they do it in that order, and then F # can lead students into a stupor. What do you think about the idea to change the order? If you first introduce people to things like LINQ and subsets of monads, is this likely to make it easier to understand the language?



- I heard that people who first get acquainted with functional programming, then get completely confused when they start to study OOP, and start asking: how can I make immunity? Where are the monads? This is a matter of habit. For a person who has learned functional programming, OOP seems strange and frightening, and vice versa. This does not mean that you cannot start with functional programming, there is a tutorial in which F # is used as an introductory language, Programming Theory Concepts or something like that.



- If it weren’t for the mentioned problems with C # in the documentation, could you safely recommend F # to everyone as the first language?



- Great question. I think it depends on the situation. I would definitely recommend it for students at the university, because it covers all the things that are taught in the university programming course. But if a teenager or a child is learning to program, then it makes sense to start with something simpler, like Python or JavaScript, because there you can get a working program much faster.



An adult programmer is already used to the fact that the return is not necessarily instant. In JavaScript, you can run an animation on the screen in five minutes, with F # this will not work. But if a person is ready to spend time and energy, then why not. F # is a rather small language, it does not have a bunch of different oddities, like in C #, where there are many different ways to solve problems. So for beginners, F # is a fairly simple language.



- To the question about the F # audience - and what audience is your F # for fun and profit website focused on? Does the reader have knowledge of C #?



- At first, he dealt almost exclusively with F #, and then he largely followed my development as a programmer. Now it is devoted not so much to F #, but to functional programming as a whole. For example, there are my articles on Railway Oriented Programming, Property Based Testing and much more. And I heard many times from readers that they used these ideas in TypeScript or Ruby, so this is material that is far from being targeted only to F # users. I hope these articles will be useful to everyone, including C # users.



Fun and profit



- The name "F # for fun and profit" (F # for fun and profit ") may seem strange to someone, because functional programming is usually not associated with" entertainment ". But do you think there is a lot of fun there too?



- Yes. And this name I gave the site is not accidental. I wanted to emphasize that F # can be both useful and interesting, that is, this language is not only for the academic environment. As I said, by the time I decided to try F #, I was very tired of my work and of the language in which I wrote. And with F #, I became interested again.



I don’t know why some languages ​​are interesting, while others are not really. But I have never heard anyone call Java an interesting language. Maybe the whole point is that in some languages ​​you can achieve significantly greater results in less time, you do not need to spend time on typing and the like. Be that as it may, but many people told me that with the transition to F # after C #, programming has become much more fun.



- To the question of “benefit”: you can hear that functional programming is not only interesting, but also well paid, the salaries of developers in functional languages ​​are higher.



- Yes, that's right. Companies that decide that they need functional programming are willing to pay more money to programmers. So I advise people to learn functional programming also and therefore.



A few years ago at StackOverflow there was a survey where it turned out that in F # salaries are higher than in any other language. The situation, of course, can change, but in general, programmers in functional languages ​​definitely get more than programmers in C #. True, this is due to the fact that functional programmers are now much less common, in 10 years this difference may disappear. But now there is definitely an advantage.



In addition, I noticed that often employers are looking for programmers with F # experience, even if the work itself is not directly related to F #. That is, if a person learned F #, then he most likely grew up as a developer, or at least he is ready to try new things. After all, there are those programmers who do not want to learn anything. In general, studying F # in your career is clearly not harmful.



- So, F # is beneficial for developers. What about employers? Indeed, if the owner of the company reads our interview, he will conclude for himself that F # programmers are more difficult to find and they need to pay more. What are the advantages of F # from the point of view of the company?



- From the point of view of the company, F # reduces the amount of work performed. Immunity, explicit dependencies, etc. significantly reduce the number of bugs and simplify code support. When you need to make some changes, it is much more likely that the new code will work the first time. It is customary to say about such code that if it compiles, then it works. There are exceptions, but this is usually the case. With F # there is no need to check for null values; if types are spelled correctly, they are not confused; many unwanted situations in the code can be made impossible. Thanks to all these tricks, F # code is written much faster.



According to my feelings, when you write in F #, the code has a significantly higher percentage of logic and a smaller percentage of templates, fewer unnecessary formalities that have nothing to do with the program itself. In C # there are many patterns - Visitor, Factory, Singleton, Bridge, but in F # there is nothing of this, just there is code that does what you need.



So in order to solve a problem, it turns out that you need significantly less code. , , . , , , , , . Google Amazon — .



— , F# , — , , . ?



- Yes it's true. , , C# , , C#. , C#, null, , - . F# . , , , .



- - , , , . C# F#, , , . , , . , , F#, .



— , Microsoft F# ( C# ). What do you think about this?



— , Microsoft C#. , . — , Microsoft, , , Entity Framework Visual Studio. , Microsoft, Microsoft - — . , , Python Ruby. - , - , .



, F#, , , — F# , . Microsoft, . , Ionide, VS. , F# , Microsoft. , , , , , Microsoft . Microsoft , , .



— Haskell. F# — .NET-, ?



— , - , , Smalltalk, - . F# - , .NET. Java, Scala . , , C#, Java, F# , Scala, .



Haskell, . Haskell , , F#. F# , Haskell . , , , API Java, .NET JavaScript. API .NET , , API .



— . F#, , : , , ?



— , F# . , , , . , . F# , , C#. , Haskell, - , .



, , , .



F# , , . , -, .



, - , , — , ? , , , . F#, C#, , . . - , F#.



, F# , , .



DotNext «The Power of Composition». , F#: , , , . , .




All Articles