How do you program differently in dynamic languages?

前端 未结 15 1525
南笙
南笙 2021-02-01 03:37

How would someone who really knows how to take advantage of dynamic programming languages approach programming differently than someone working in a static language?

I

相关标签:
15条回答
  • 2021-02-01 03:51

    It comes down to one of my favorite ratios: How much time I spend thinking about solving a problem, vs. how much time I spend thinking about the tool I'm using to solve the problem. Think of it as equivalent to S/N ratios.

    With duck-typing languages (which I consider to be the factor that helps me the most with productivity), I simply am able to spend more time thinking about my problem and its solution (and write code that addresses those specifically), and I spend less time keeping the language artifacts straight.

    Then there's a lot of code I just don't write, involving declarations and especially type-casts.

    But it's mainly keeping my focus in the sweet spot.

    0 讨论(0)
  • 2021-02-01 03:53

    Dynamic Languages can change the object at run time, you can add methods, properties...

    One good example of Dynamic Languages magic is this Groovy code snippet which call a method on a webservice in just two lines of code:

    def proxy = new SoapClient("http://localhost:6980/MathServiceInterface?wsdl");
    def result = proxy.add(1.0, 2.0);
    

    This is another Groovy snippet that extract data from XML:

    def contacts = new XmlParser().parseText("<contacts><name>Bahaa Zaid</name></contacts>");
    def myName = contacts.name[0].text();
    

    You cannot do this in Static Languages. Dynamic Language can change the objects to reflect the actual runtime condition.

    0 讨论(0)
  • 2021-02-01 03:53

    I do not have a specific answer, just a suggestion: have a look at the book "Design patterns in Ruby" : it goes over most of the classic design patterns (à la Gamma et al., and more) and express them, quite succinctly, in Ruby :)

    0 讨论(0)
  • 2021-02-01 03:58

    More libraries and more important more useable libraries.

    My guess is that the "Duck Typing" usually associated with dynamic languages helps simplify the code significantly and makes writing generic code much easier. You are not constrained by a strict class hierarchy and thus are able to more easily compose components from different libraries together.

    0 讨论(0)
  • 2021-02-01 03:58

    Read "Higher Order Perl" by Mark Jason Dominus. It only discusses Perl but it does give techniques that are natural to Perl that would be less natural in most static languages.

    All languages obviously have their strengths and weaknesses and dymanic vs static 
    

    is only one of many ways to classify a language. I would not make the argument that dynamic languages as a whole are better or worse then static languages. But I do think this book is very good at showing different ways of approaching problems using Perl that would be more difficult or impossible in most Static languages.

    0 讨论(0)
  • 2021-02-01 04:01

    For me it's turnaround speed. The dynamic languages I use (Python and a bit of JavaScript at the moment) are interpreted. This means I can try things out on the fly. If I want to see how a certain bit of the API behaves I can just hack away at the interpreter for a couple of minutes.

    If I wanted to do the same in a language like C# I'd have to fire up VS, make a project, then compile it. If I want to test a part of a bigger piece of software I'm working on I probably have to compile that, which can take ages. Fortunately in .Net I can load up assemblies from the big project in IronPython and get some of the same benefits (i.e. quickly testing out different parts of the API) of interpreted languages.

    0 讨论(0)
提交回复
热议问题