3 ms·
I love programming in a functional-style in Python. I create one-way functions. Every input will always give the same output. Then I build out the test harne
by blackrock 9y ago
I love programming in a functional-style in Python.
I create one-way functions. Every input will always give the same output.
Then I build out the test harness for that function, to unit test the function, and pathway test it. So I make sure that I have automated tests to capture every single extreme that can go into the function.
I do this for every single function created. Then I string together one function into another, and I put that into its own function. And I unit test that larger function. Then sometimes, I even put those 2nd level functions, into more functions. And I unit test that as well. In the end, I just have a function that calls a function, that calls a function, and so forth. The rabbit hole just goes deeper and deeper.
If I ever need to enhance a function, then I make my changes, add more unit tests, and re-execute it. The impact is always local, and I have full confidence in my unit tests and pathway tests.
I'm always incredibly amazed when I execute the program, and it works flawlessly, every time.
I keep iterating through the development cycle this way. And every version checked in, is fully tested, very accurate, with detailed test logs captured, and most importantly, it does not crash. It's beautiful.
With Object-oriented programming, I have to maintain an external state. This is like modifying a global variable within your object. Your methods have to load and manipulate an external variable. And then another method will modify it later.
However, I do like the organization and hiding ability of OOP. So in the end, I just make all my functions as a functional-style, one-way function. And my methods are just wrappers around it.
My main complaint about Python, is that it's just slow. I wish it had a compile to binary feature built in. Although, I get around that with my heavy unit tests, but some coding mistakes can be easily caught during compile time.
- FridgeSeal 9y agoCheck out Nim, it's got python style syntax, but it's statically typed and compiles down into performant C and then into a binary. I've been converting a lambda function used at work from Python into Nim[0]: I've had very few issues so far and the performance gains are fantastic (Admittedly my lambda function is pretty straightforward, but still). It's also got support for functional programming styles, with a function composition operator/first class functions etc[1] and a pragma to tell the compiler that a function has no side effects[2]. [0] https://nim-lang.org/ https://nim-lang.org/ [1] https://nim-by-example.github.io/procvars/ https://nim-by-example.github.io/procvars/ [2] https://nim-by-example.github.io/procs/ https://nim-by-example.github.io/procs/
- xaedes 9y ago"With Object-oriented programming, I have to maintain an external state. This is like modifying a global variable within your object. Your methods have to load and manipulate an external variable. And then another method will modify it later." I like to interprete state as just another input and/or output variable. This way you can test your object member functions while pretending they are FP functions.