Is it a good idea to make statements to depend on

2019-06-08 14:01发布

After a my previous question, an answer (@Andy H) said that the related problem was the order of statements. So, in general, is it "correct" / "right" to have "before-after statement dependencies"? That is, for example, if I have the following working code

class Article < ActiveRecord::Base
  acts_as_list ...

  include MyModule
end

and the following code does not work

class Article < ActiveRecord::Base      
  include MyModule

  acts_as_list ...
end

Is there some "bad" reason to do not continue the implementation of the class? That is, in general, is that a good idea to make statements to depend on the order of other statements?

2条回答
可以哭但决不认输i
2楼-- · 2019-06-08 14:03
  1. In your case: yes, it's not acceptable. Conceptually the order is not important there, it's the implementation which is forcing a fixed order to work. Not nice.

  2. In general: Of course in a non-lazy language like Ruby the order of statements matter. But it's not the same the order "dependency" in x = 1; y = 2 * x that one that involves state and side-effects (silly example: x = []; x << 1 if condition1?; x << 2 if condition2?; x).

查看更多
你好瞎i
3楼-- · 2019-06-08 14:15

I think this is not a question of "good" vs "bad".

It just happens to be the case that code gets executed from top down.

So if you have some code in statement_a that relies on behaviour added by statement_b then putting statement_b before statement_a is the only solution...

查看更多
登录 后发表回答