String splits to create arrays

Creating an array of strings for configuration or verification purposes can get long and ugly in code. For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>arrayOfStrings = ['one','two','three','four']
</code>
<code>arrayOfStrings = ['one','two','three','four'] </code>
arrayOfStrings = ['one','two','three','four']

Often to reduce the characters and improve readability I will use string operations to contruct these from a raw string:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>arrayOfStrings = 'one two three four'.split(' ')
</code>
<code>arrayOfStrings = 'one two three four'.split(' ') </code>
arrayOfStrings = 'one two three four'.split(' ')

My rationale is that it makes the items easier to read for a person, as they would see the gaps (one two) easier than the string and array delimiters ('one','two').

If its known that the strings will not contain spaces, would this go against good programming practise?

It would be a bad practice, because it introduces a dependency on a subtle assumption that is hard to document and hard to remember accurately: namely, that the values maintained in this place are any strings without spaces in them. This is almost the same as “any strings”, and the former definition will inevitably be mistaken for the latter one day.

Configuration values like these will almost certainly have to change as a system develops. Storing them this way will work fine until one day, new data violate the unspoken assumption and introduce a defect. Note that the defect will probably be subtle: values before the string with the space in it will still be processed correctly, and the ones afterwards will appear in slightly the wrong position, only the value itself will be missing and two or more others will take its place.

In a way, subtle preconditions and subtle defects like this are much worse than obvious ones. Glaring errors are noticed quickly and eradicated quickly. A misbehaviour like this is liable to go unnoticed for quite some time and do damage that won’t immediately be known, until it can’t be easily repaired and the cause can’t be easily found because the programmer who constructed the storage method has moved on.

(What, then, should one do to store large numbers of string literals? Unless your language has a quote-less construct like Lisp lists or Perl’s qw() operator, you are almost certainly better off holding string values in a properties file or a database than spelling them out in code. There are other reasons why this is a good idea, but avoiding methods that are either hard to read or subtly problematic is reason enough.)

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