TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Imperative vs. Declarative

66 点作者 philip_roberts大约 12 年前

13 条评论

hackinthebochs大约 12 年前
I'm not sure I agree with some of the examples in the article. The examples used paint declarative programming as basically abstractions over details. The problem is that there is no line where an abstraction crosses the boundary into declarative programming. It's not really about abstractions but about control flow. If your code has a specific order it has to run in, then its imperative as you're still describing the steps needed to perform the action. SQL is declarative because you're describing the output set rather than the steps to generate it. Functional languages are considered declarative because of the fact that pure functions can be rewritten, optimized, lazy evaluated, etc by the runtime. I have a hard time considering map/reduce/etc in isolation as examples of declarative programming, as they're usually used in conjuction with an algorithm that most definitely has a defined execution order.
评论 #5480729 未加载
评论 #5480690 未加载
MattRogish大约 12 年前
I really like SQL. Sure, the language has warts but the ability to concisely represent WHAT you want, not HOW you want it, makes it very readable once you understand the simple constructs and how to properly design tables and indexes (not very hard).<p>For example, consider the problem of finding the second largest value in a set.<p>In SQL, I'd do something like:<p><pre><code> SELECT MAX( col ) FROM table WHERE col &#60; ( SELECT MAX( col ) FROM table ) </code></pre> It's pretty readable, and can almost be read in plain english: "Get the next biggest value from the table where it's smaller than the biggest value."<p>How might you do this in Java? <a href="http://stackoverflow.com/questions/2615712/finding-the-second-highest-number-in-array" rel="nofollow">http://stackoverflow.com/questions/2615712/finding-the-secon...</a><p>But look at all the other ways you can do it in that thread. None of them are very readable. And, they can hide subtle bugs that you won't find just by reviewing the code.<p>Ruby has a pretty concise example if you happen to know the trick, and that the sort performance isn't miserable (kind of a gotcha question): <a href="http://stackoverflow.com/questions/8544429/find-second-largest-number-from-an-array-in-ruby" rel="nofollow">http://stackoverflow.com/questions/8544429/find-second-large...</a><p>This is a very simple example, but as you scale up to more complex problems I almost always find SQL is fewer lines of code, more readable, and far less buggy (SQL tends to either work or not work - I find much more subtle bugs in a 30 line Java algo than a 10 line SQL command).
评论 #5480988 未加载
评论 #5480892 未加载
评论 #5481335 未加载
评论 #5481423 未加载
octo_t大约 12 年前
Prolog is declarative programming take to the maximum (excluding things like Answer Set Programming/clingo etc).<p>In Prolog you <i>ask</i> questions. For example: subset([1,2],[2]).<p>then it goes away and says "yes". Or you want to know if any subsets exist: subset([1,2],B).<p>B = [] B = [1] B = [2]<p>This makes it really really nice for some surprising tasks (Windows NT used to ship with a prolog interpreter for setting up the network)
评论 #5481034 未加载
评论 #5481342 未加载
评论 #5480601 未加载
评论 #5480622 未加载
icebraining大约 12 年前
Map and other functional constructs may be declarative, but I only "feel" like I'm programming declaratively when I'm coding in a language like Prolog.<p>The fact that, with unification and backtracking, you can not only get a result for a query, but also "pass a variable" as an argument and get a possible value makes it seem much more like a mathematical expression and less like a computation.<p>For example, I can define some relations:<p><pre><code> parent_of(john, mary). parent_of(mary, eve). grandparent_of(X, Y) :- parent_child(X, Z), parent_child(Z, Y). </code></pre> And then I can simply run a query:<p><pre><code> ?- grandparent_of(john, eve). Yes </code></pre> But I can also make it fill in the value for me:<p><pre><code> ?- grandparent_of(john, X). X = eve </code></pre> 'grandparent_of' is not some piece of code, it's an actual declaration of a relation between the terms.<p>Of course, you can do unification and backtracking in other languages, but Prolog is designed for it.
PeterisP大约 12 年前
On the flip side, it also drastically changes the typical errors.<p>In imperative style, most of your mistakes or carelessness will usually mean that the machine makes a wrong result or crashes in the process - a bad 'what'.<p>In declarative style, most of your mistakes or carelessness will usually mean that the machine will take a bazillion times less efficient way trying to make that result, possibly taking 'forever' or running out of memory - i.e. a bad 'how'.
评论 #5480566 未加载
Uchikoma大约 12 年前
I don't think the author gets declarative right. It feels like he bolts a cool word onto some things he uses. Call me old fashioned, but I think Prolog is declarative, map() and reduce() are not.
hermannj314大约 12 年前
Lately, when I code in C#, I write the code I wish was possible with the goal of trying to code to the problem as stated in the requirements. This way the code that solves the problem looks almost exactly like the description of the problem. That is step #1.<p>Step #2 is doing whatever is necessary to make that code work. Sometimes this means using the more interesting stuff like reflection, dynamic objects, expression tree visitors, etc. but I find that subsequent issues keep getting easier to deal with. This is because step #1 is naturally building a DSL for your problem domain and you start to find that what you did in step #2 is quite reusable.<p>I've been programming for a while, so I have experience with the imperative, "write the code that solves the problem" approach and it works too, but I am having fun with the "write the code that describes the problem" approach more.<p>Just my two cents.
评论 #5481820 未加载
taeric大约 12 年前
It is not just as programmers. Consider, most cookbooks. Then consider the directions that come with Ikea furniture. Of course, the real beauty of both of those examples, is that they are a mix of declarative and imperative instructions.<p>For some reason, it seems we programmers are adamant that it must be one or the other. Consider all of the examples, either purely imperative or purely declarative. Why not both?
评论 #5481022 未加载
评论 #5480719 未加载
toki5大约 12 年前
Great article, but one thing that's sort of glossed over here, and that I half-disagree with, is this:<p>&#62;But we also get to think and operate at a higher level, up in the clouds of what we want to happen, and not down in the dirty of how it should happen.<p>The author mentions this at the end, but I feel it should be stressed more strongly: The dirty of how is <i>important</i>. The author presents a big "if" here, which is: <i>if</i> the function we've built to abstract away some ugliness performs in the best, most efficient way possible, with no drawbacks, then, yes, abstracting that functionality away and forgetting it is okay.<p>But to me that's a big if. It is just as important to me to understand and recognize that <i>map</i> is fast, efficient, and to understand <i>why</i> it's fast and efficient, so that someday, if you come across a situation where <i>map does not apply,</i> you will know why, and you'll be able to use something better.<p>Being up in the clouds all the time is, to me, a pipe dream -- we must always be cognisant of the ground on which we built this tower of abstraction layers.
评论 #5480854 未加载
jonjaques大约 12 年前
OP could definitely have used more examples, but I think he's on the right track. Where declarative or functional programming comes in really handy is composition. Underscore has a lot of utilities that make it easy.<p><pre><code> var genericFilter = function(type, value) { return function(items) { return _.filter(items, function(i) { return i[type] === value; }); } }; var sizeFilter = genericFilter('size', selectedSize); var brandFilter = genericFilter('brand', selectedBrand); var appliedFilters = _.compose(sizeFilter, brandFilter); var filteredItems = appliedFilters(items); // which ends up doing sizeFilter(brandFilter(items)); </code></pre> // edit for sloppy code;
simonv3大约 12 年前
I work with a bunch of UX designers, and as the only developer here I'm often confronted with their question of "why can't I just describe what I want done?"<p>Their apprehension of tackling code is one I don't immediately understand, but I do get that they don't want to think about the how, rather the what. It's a funny parallel.<p>Here's a great video by Bret Victor who saw this problem, and tried to fix it for animation:<p><a href="https://vimeo.com/36579366#t=1748" rel="nofollow">https://vimeo.com/36579366#t=1748</a>
iambot大约 12 年前
I prefer the imperative style personally, I like things done the way <i>I</i> want ... I kid, great write-up though.
ExpiredLink大约 12 年前
What is the result of procedural programming? Functions that can be used <i>declaratively</i>! The purpose of procedural programming is to encapsulate and consequently eliminate "telling the 'machine' how to do something".<p>PS: What happened to 3GL vs. 4GL?