Ruby – when to use instance variables vs parameters between methods?

I’m writing several methods that call other methods.

To pass the information I have a couple of choices:

  1. Pass the information as parameters

  2. Set instance variables so that other methods can access them

When should I choose one option over the other?

It seems that the first option is good as it is very specific about what is being passed. the downside seems to be that a lot of values are being passed around.

The second method doesn’t require passing all the values around but seems to lead to a lot of magic where methods set instance variables ‘somewhere’

Should I always be very explicit about gets passed to other methods in the class? Are there exceptions so this?

1

It depends on the role of those variables with respect to the entire class.

If your class is small, and the information that these variables transport is relevant to all or almost all of it, then it makes sense to have them as instance variables. A bank account object probably needs to know its holder for most actions anyway, so it makes sense to hold on to it indefinitely.

If the data are very specific to this method and its helpers, then they should travel as parameters. The date of a transaction is not so much a feature of the account as of a single action taken on that account. (If you have more than one of these parameters, and they tend to be passed around as unit (e.g. date, time of day and time zone), then it is also wise to bundle them into a helper object to keep the number of parameters down.)

Let me misquote you to make my point clear:

I’m writing several functions that call other functions. To pass the information I have two choices:

  1. Pass the info as arguments.
  2. Use global variables.

When should I choose one option over the other?

It is generally acknowledged that non-local state is a bad idea. You should therefore generally pass the information using parameters, not via variables in a common scope. This becomes more manageable if you group related information together into objects or structs.

Passing too many parameters is a code smell, and it can probably be refactored to use a few more classes. Assume a pathological case where you are writing an address book and have a method add_contact(surname, firstname, fullname, ...). It would be better to group that into a Name class, likewise for parts of an address: add_contact(name, address).

If however the necessary parameters to your function are completely unrelated, it may be that your method is trying to do too much, and should be refactored into smaller parts.

I do not think that using instance variables is a good solution: it is needless mutation of state, and brings with it all the problems of global variables, on a smaller scale. This way lies un-debuggable spaghetti code.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Ruby – when to use instance variables vs parameters between methods?

I’m writing several methods that call other methods.

To pass the information I have a couple of choices:

  1. Pass the information as parameters

  2. Set instance variables so that other methods can access them

When should I choose one option over the other?

It seems that the first option is good as it is very specific about what is being passed. the downside seems to be that a lot of values are being passed around.

The second method doesn’t require passing all the values around but seems to lead to a lot of magic where methods set instance variables ‘somewhere’

Should I always be very explicit about gets passed to other methods in the class? Are there exceptions so this?

1

It depends on the role of those variables with respect to the entire class.

If your class is small, and the information that these variables transport is relevant to all or almost all of it, then it makes sense to have them as instance variables. A bank account object probably needs to know its holder for most actions anyway, so it makes sense to hold on to it indefinitely.

If the data are very specific to this method and its helpers, then they should travel as parameters. The date of a transaction is not so much a feature of the account as of a single action taken on that account. (If you have more than one of these parameters, and they tend to be passed around as unit (e.g. date, time of day and time zone), then it is also wise to bundle them into a helper object to keep the number of parameters down.)

Let me misquote you to make my point clear:

I’m writing several functions that call other functions. To pass the information I have two choices:

  1. Pass the info as arguments.
  2. Use global variables.

When should I choose one option over the other?

It is generally acknowledged that non-local state is a bad idea. You should therefore generally pass the information using parameters, not via variables in a common scope. This becomes more manageable if you group related information together into objects or structs.

Passing too many parameters is a code smell, and it can probably be refactored to use a few more classes. Assume a pathological case where you are writing an address book and have a method add_contact(surname, firstname, fullname, ...). It would be better to group that into a Name class, likewise for parts of an address: add_contact(name, address).

If however the necessary parameters to your function are completely unrelated, it may be that your method is trying to do too much, and should be refactored into smaller parts.

I do not think that using instance variables is a good solution: it is needless mutation of state, and brings with it all the problems of global variables, on a smaller scale. This way lies un-debuggable spaghetti code.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Ruby – when to use instance variables vs parameters between methods?

I’m writing several methods that call other methods.

To pass the information I have a couple of choices:

  1. Pass the information as parameters

  2. Set instance variables so that other methods can access them

When should I choose one option over the other?

It seems that the first option is good as it is very specific about what is being passed. the downside seems to be that a lot of values are being passed around.

The second method doesn’t require passing all the values around but seems to lead to a lot of magic where methods set instance variables ‘somewhere’

Should I always be very explicit about gets passed to other methods in the class? Are there exceptions so this?

1

It depends on the role of those variables with respect to the entire class.

If your class is small, and the information that these variables transport is relevant to all or almost all of it, then it makes sense to have them as instance variables. A bank account object probably needs to know its holder for most actions anyway, so it makes sense to hold on to it indefinitely.

If the data are very specific to this method and its helpers, then they should travel as parameters. The date of a transaction is not so much a feature of the account as of a single action taken on that account. (If you have more than one of these parameters, and they tend to be passed around as unit (e.g. date, time of day and time zone), then it is also wise to bundle them into a helper object to keep the number of parameters down.)

Let me misquote you to make my point clear:

I’m writing several functions that call other functions. To pass the information I have two choices:

  1. Pass the info as arguments.
  2. Use global variables.

When should I choose one option over the other?

It is generally acknowledged that non-local state is a bad idea. You should therefore generally pass the information using parameters, not via variables in a common scope. This becomes more manageable if you group related information together into objects or structs.

Passing too many parameters is a code smell, and it can probably be refactored to use a few more classes. Assume a pathological case where you are writing an address book and have a method add_contact(surname, firstname, fullname, ...). It would be better to group that into a Name class, likewise for parts of an address: add_contact(name, address).

If however the necessary parameters to your function are completely unrelated, it may be that your method is trying to do too much, and should be refactored into smaller parts.

I do not think that using instance variables is a good solution: it is needless mutation of state, and brings with it all the problems of global variables, on a smaller scale. This way lies un-debuggable spaghetti code.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Ruby – when to use instance variables vs parameters between methods?

I’m writing several methods that call other methods.

To pass the information I have a couple of choices:

  1. Pass the information as parameters

  2. Set instance variables so that other methods can access them

When should I choose one option over the other?

It seems that the first option is good as it is very specific about what is being passed. the downside seems to be that a lot of values are being passed around.

The second method doesn’t require passing all the values around but seems to lead to a lot of magic where methods set instance variables ‘somewhere’

Should I always be very explicit about gets passed to other methods in the class? Are there exceptions so this?

1

It depends on the role of those variables with respect to the entire class.

If your class is small, and the information that these variables transport is relevant to all or almost all of it, then it makes sense to have them as instance variables. A bank account object probably needs to know its holder for most actions anyway, so it makes sense to hold on to it indefinitely.

If the data are very specific to this method and its helpers, then they should travel as parameters. The date of a transaction is not so much a feature of the account as of a single action taken on that account. (If you have more than one of these parameters, and they tend to be passed around as unit (e.g. date, time of day and time zone), then it is also wise to bundle them into a helper object to keep the number of parameters down.)

Let me misquote you to make my point clear:

I’m writing several functions that call other functions. To pass the information I have two choices:

  1. Pass the info as arguments.
  2. Use global variables.

When should I choose one option over the other?

It is generally acknowledged that non-local state is a bad idea. You should therefore generally pass the information using parameters, not via variables in a common scope. This becomes more manageable if you group related information together into objects or structs.

Passing too many parameters is a code smell, and it can probably be refactored to use a few more classes. Assume a pathological case where you are writing an address book and have a method add_contact(surname, firstname, fullname, ...). It would be better to group that into a Name class, likewise for parts of an address: add_contact(name, address).

If however the necessary parameters to your function are completely unrelated, it may be that your method is trying to do too much, and should be refactored into smaller parts.

I do not think that using instance variables is a good solution: it is needless mutation of state, and brings with it all the problems of global variables, on a smaller scale. This way lies un-debuggable spaghetti code.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật