10

I have recently been more concerned about the way I write my code. After reading a few books on design patterns (and overzealous implementation of them, I'm sure) I have shifted my thinking greatly toward encapsulating that which change. I tend to notice that I write less interfaces and more method-oriented code, where I love to spruce life into old classes with predicates, actions and other delegate tasks. I tend to think that it's often the actions that change, so I encapsulate those. I even often, although not always, break down interfaces to a single method, and then I prefer to use a delegate for the task instead of forcing client code to create a new class.

So I guess it then hit me. Should I be doing functional programming instead?
Edit: I may have a misconception about functional programming.

Currently my language of choice is C#, and I come from a C++ background. I work as a game developer but I am currently unemployed.

I have a great passion for architecture. My virtues are clean, flexible, reusable and maintainable code. I don't know if I have been poisoned by these ways or if it is for the better. Am I having a refactoring fever or should I move on? I understand this might be a question about "use the right tool for the job", but I'd like to hear your thoughts.

Should I pick up a functional language? One of my fear factors is to leave the comfort of Visual Studio.

6
  • You can try F# if leaving Visual Studio is a deal-breaker.
    – Adam Lear
    Commented Mar 12, 2011 at 22:13
  • It's not a deal-breaker. I just haven't tried many other alternatives. I guess I got some fear against working without intellisense, refactoring or similar tools and have this conception that you end up writing code in a plain text editor again.
    – Statement
    Commented Mar 12, 2011 at 22:15
  • I am currently unemployed ... should not this be the first thing to fix?
    – Job
    Commented Mar 13, 2011 at 3:03
  • I would try to work without VS for a little while. You might find that working in a very minimalist way might be fun.
    – Zachary K
    Commented Mar 13, 2011 at 10:52
  • @Job, yes, but I have been struck with a series of quite bad events recently :) I damaged my foot (well I don't code with my foot, but, still...) just recently after leaving my old job in search for a new one.
    – Statement
    Commented Mar 15, 2011 at 17:37

6 Answers 6

5

YES

Learning new stuff should be fun. And learning a new way to program should make you a better programmer. Check one of Haskell or Erlang, both are functional but they are very different. If you pick Haskell go find a few talks by "Simon Payton Jones" on youtube or podcasts. The guy is a great speaker.

2
  • 1
    I chose this answer of many reasons. @Zachary K says it might be fun to step out of the box of a well known place and have another perspective. I shouldn't be afraid to try something new. It's not the end of days. Haskell is a language that has caught my attention in various discussions a couple of times before. I don't know if I need a functional language but as @Toby Allen says it's a great way of seeing how other ways work and I agree on this after trying out some UnrealScript. @ammoQ missed the point a bit but I hadn't a very good definition of my question. I still get his concerns.
    – Statement
    Commented Mar 15, 2011 at 17:45
  • Glad to help. Have fun
    – Zachary K
    Commented Mar 16, 2011 at 7:24
14

Holy * * * *. There is no silver bullet. Step down. Relax, take a deep breath.

Just because you have a tool in your toolbox, it doesn't mean you are bound to use it. Concentrate on the task at hand, i.e. writing a program that works, and refactor when code smells bad. Don't encapsulate and abstract every little detail just because it might change some day in the future. IMO it's really much better to start straight forward, KISS & YAGNI, and react to changing requirements when they actually change.

I don't think anyone here can tell you whether or not a functional language is better suited for your needs. Because you didn't tell us what it is that you are working on.

5
  • Well, I ain't working on anything in particular right now. I'm between jobs. I am/was a game developer.
    – Statement
    Commented Mar 12, 2011 at 22:19
  • 3
    This is a dangerous situation regarding overengineering. No deadline, no backlog, just too much time to get obsessed with details.
    – user281377
    Commented Mar 12, 2011 at 22:28
  • (A lot of guesswork ahead since I have no clear picture in my mind) I guess my "needs" were more reflected in the way I prefer to write code. I don't know a great deal about functional programming, and I guess the name might tricked me a bit. I am just feeling that my preference is not as much object oriented as I thought. So maybe I should enjoy working in another paradigm?
    – Statement
    Commented Mar 12, 2011 at 22:33
  • Yes, I guess it's the case. I am in a phase of learning right now so that is why I explore the possibilities of writing in particular ways. At work, one had often no time to write such code, and you have to respect the code of conduct and maintain a consistent style among developers.
    – Statement
    Commented Mar 12, 2011 at 22:35
  • 2
    It's absolutely ok when you try every tool at hand now that you are between jobs, but I'm afraid we can't tell you whether or not the shoe fits.
    – user281377
    Commented Mar 12, 2011 at 22:38
12

You can pick up F#. It is .NET functional language, which will allow you to use Visual Studio and reuse a lot of your .NET knowledge. It's assemblies of course integrate with C# assemblies seamlessly (allowing you to easily combine C#, C++/CLI and F# in one project). F# support comes with VS Pro or better, I'm not sure whether it is enabled by default though.

2
  • I think there's an addon for F# in VS2008, but it is default in VS2010.
    – apoorv020
    Commented Mar 13, 2011 at 6:08
  • F# works even with a free VS Shell.
    – SK-logic
    Commented Mar 13, 2011 at 11:17
6

IMO, it's well worth trying out a functional language. Even if you never use it directly in a released product, learning it will almost certainly help give a somewhat different outlook/viewpoint on programming. Even if you end up continuing to do all your "real" coding in C#, chances are pretty fair that it'll help you improve the code you do write in C#.

As others have mentioned, the obvious choice for somebody accustomed to Visual Studio is F#, which is basically an Ocaml/Caml/ML derivative. IMO, that was a good choice on the part of Microsoft for introducing people to functional programming -- it's a full-fledged functional language, but doesn't have nearly as foreign a syntax as some do (e.g., while Lisp-based languages do have real advantages, beginners almost always find it difficult to read).

2

Yes, you should.

If you choose a pure one, like Haskell, you'll learn not only a new language, but a new way of thinking. This can help you later in your proceural/OO work, too.

F#, on the other hand, is not pure, and so you can easily miss what is important in functional programming. Yóu can, so to speak, continue writing procedural programs that mutate a variable here, perform some side effects there, just in F# syntax.

1
  • I actually got started at tryhaskell.org :) It's exciting to learn new things.
    – Statement
    Commented Mar 31, 2011 at 17:59
1

If you are already using c# you don't need a new language - with the combination of extension methods, lambdas and the generic Func/Action types it is fairly easy to write functional code in C# - that way you can use a functional style in the algorithms where it makes most sense without needing to change the entire application.

4
  • 1
    You always need another language. Even if you never use it, it is a great learning experience to learn a new language. No one language has everything.
    – Toby Allen
    Commented Mar 13, 2011 at 16:23
  • You always need to learn, but you don't always need to learn another language. The benefit comes from learning the concepts of functional programming rather than the language itself, and you can do that without leaving the familiar environment and changing the entire project. C# is good enough, and in practical terms there is a huge advantage to only having the functional code in the algorithms that actually benefit from it. Commented Mar 13, 2011 at 22:43
  • Yeah I guess my concern was more biased toward if there are languages better suited (syntactically) for working with methods/functions in the manner I find myself often working in while toying out this. C# offer lambda expressions and that is nice in itself but for example having a function return a value without any parameters yield code such as () => x, and maybe there are cleaner ways to go about.
    – Statement
    Commented Mar 15, 2011 at 17:34
  • +1 @Toby. I just recently skimmed through UnrealScript reference and saw some interesting language solutions for working with states et.c. It carries over to C# in terms of thinking about the problem and they have a quite nice solution. So yes, I do believe you learn a great deal when you step out of your sandbox and pillage your neighbors for all the sand and toy you find :)
    – Statement
    Commented Mar 15, 2011 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.