Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I think the method for building a string array in C# and PHP is a little to verbose.

C#

string[] my_string_array = new string[]{ "This", "is", "ugly" };

PHP

$my_string_array = array( 'PHP', 'is', 'a', 'little', 'better' );

While I know there is insignificantly more memory usage this way is there anything really wrong with creating string arrays like this:

C#

string[] my_string_array = "This,is,ugly".Split(',');

PHP

$my_string_array = explode( ',', 'PHP,is,a,little,better' );

Any chance in the future these languages will use a simpler syntax like JavaScript?

share|improve this question
1  
C# is statically typed and requires verbose type declarations. This is a way of life. I don't see any place for elaborate type inference (as in the ML family) in that language. Some languages have a very concise syntax for that case, like Perl: @string_array = qw(Perl really rules) (splits at whitespace, at compile time). – amon Mar 8 at 21:18
1  
Something like this: var advice = new []{ "Take", "it", "or", "leave", "it" };? – Leonid Mar 8 at 21:38
1  
BTW in C#, local variables usually use camelCase, not underscores. – svick Mar 8 at 22:36
1  
Also, if you're asking about future (which is off topic here, we don't have crystal balls) it would help you knew about the present state of the languages. – svick Mar 8 at 22:39
@amon Static typing doesn't have to mean verbose types. That's what type inference is for. And C#'s type inference isn't as good as that of functional languages, but it can still help a lot. – svick Mar 8 at 22:40
show 1 more comment

closed as not a real question by Corbin, cHao, Glenn Rogers, svick, Jesse C. Slicer Mar 8 at 22:52

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

C# also supports less verbose versions of array creation.

You can leave out the type:

string[] my_string_array = new[] { "This", "is", "ugly" };

You can also use var:

var my_string_array = new[] { "This", "is", "ugly" };

And you can even leave out the new part entirely:

string[] my_string_array = { "This", "is", "ugly" };
share|improve this answer
Nice. I have never seen that last one. Anytime I try to use var I get an error. – iambriansreed Mar 9 at 1:44
@iambriansreed Then you're doing something wrong. Try to figure out what exactly are you doing wrong and if you can't, consider asking question on SO. And if you meant that you tried to use var with the last sample in my answer, then there is a reason why I didn't include that options in my answer: it doesn't work. – svick Mar 9 at 11:46
It could have worked though if the C# team bothered to make it work. F# is very good at deriving the types, and it could handle it. – Leonid Mar 10 at 5:28

I don't know any C#, but as for PHP, it has bracketed arrays since PHP 5.4:

$arr = ['this', 'is', 'an', 'array', 'of', 'strings'];

Unless you're absolutely certain that your code will always be run on >= 5.4, I wouldn't use this. Destroying backward compatibility for 7 characters just doesn't seem worth it (though I do share your distaste of array()).


As for splitting, I wouldn't do that. It looks pretty nasty (to me anyway). Also, maintaining and searching for it could be a bit more of a pain (what happens if you want , in a string? then what if you want your new delimiter in there? then the next new one, etc?).


tl;dr: It's just the way PHP is; working around that is just going to bring you pain. You can use [...] syntax now, but unless you are 100% sure it will only be on PHP 5.4+, it's not worth it.

share|improve this answer
Nice, wasn't aware 5.4 was that old. – iambriansreed Mar 8 at 21:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.