Is it considered bad practice to access a returned array by a key straight away?

I couldn’t think of a good way to word the title, sorry. But what I mean is, is it considered bad practice to do:

print get_array()[2]

over:

output=get_array()
print output[2]

(where get_array() is a function that returns an array, if that wasn’t clear). Which method is better? Why?

5

Let me take a crack at this,

Think of the risks you’re posing by accessing an array straight away.

  • Do you know for SURE, it’s got data at the aforementioned index?
  • Is that data in the format you want?
  • If there was an Exception, like a null at that index, would your
    program be able to recover?

I cannot tell off the bat what language you’re using… but, in any language it’s a good idea to just double check the input of your functions before using them. This can save massive headaches when a codebase grows.

4

From a design point of view, there seems to be a problem here – you’re accessing a function that returns a set of values, take one specific value from it, and discard the rest.

Doesn’t this feel like a waste, retrieving values you have no use for? Wouldn’t it make more sense to replace get_array()[2] with a specific call, get_item(2)?

This entirely depends upon what you are planning on doing with the array and what you should/can do if the element you expect to be at a certain position isn’t there.

Frequently, when this is done, it is returning multiple values (such as a first, middle and last names) which are actually independent — the array is an ad hoc structure. The problem with this is that it isn’t defined anywhere, and so is fragile. If you add enums to make it less fragile, you’ve spent about as much time, for not much gain – better to just define a structure and be done with it.

That said, sometimes you have to consume a API where this has been done, in which case, directly accessing the value is fine if the contract says it will always be there.

If you don’t do anything with the returned value except access it once, putting it in a temp variable doesn’t gain you anything (except possibly during debugging, where knowing what result is passed to a function can be useful).

It is always good practice to separate your code’s IO from its internal computations. The coding-related StackExchange sites are littered with code that mixes Print (or Println or putStr or whatever is the local dialect) inside complex functions. This is bad practice.

In the case of output (as in your example), mixing output statements with computation tends to make the final output of the program dependent on the internal structure of the code. It then becomes difficult to alter that code without breaking the output. For example, you might want to print out a sorted sequence of lines; you happen to extract them from some structure which automatically sorts them, so you naively add print statements as you compute each line. This works. Then you switch to storing the intermediate calculations in a structure which doesn’t sort them. Broken output which requires you to remove the print statements, add a sorting step and then put the print statements after that.

If you separate computation from presentation, you don’t have this problem.

In your specific example, I don’t think either choice is good, because both might fail. Even if you get a valid array, what if it doesn’t have a second row? This is a problem you should resolve before your output step. Your second example is actually worse, because it doesn’t resolve the problem so adds a pointless extra step.

output=get_array()[2]
print output

Would be better. The retrieval of the data and its output can then be cleanly separated.

2

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