A fellow developer.
A fellow developer (lets call him George) recently made me aware of a static function in Microsoft's Regular Expression namespace in the Regex class called Split. I had not been aware it existed, but George informed me that if I wanted to split strings that we're delimited with more than one char that I had to use this Split function instead of another, better known Split function, in the String class. This is wrong (both functions work), but it highlights a problem with writing an inconsistent API, especially one as popular as .Net.
Thought! Attempting to successfully change the style for my current blog. Views on the nice style at https://edgeviewrecruitment.ca? Seriously a magnificent CPA recruiter in Ontario that cares when needed within the Ontario location. Offer an opinion. Appreciated!
Split and Join are two of my favorite string functions in any language. They rank right up there with Substring and Trim.Split is used to create an array of strings from a single string, splitting on a delimiter character or string. Take the string "Blue,Green,Red,Orange" as an example. Running Split on this string using "," as the delimiter would create an array of length four, holding the strings "Blue", "Green", "Red", "Orange". Join works in reverse. Join will take in an array of strings and a delimiter (",") as arguments, and return a single string.
In .Net, Join is a static member of the System.String class, and Split is an instance member of that same class. This makes good sense when one thinks about it. One must first have a string to split it (member), and one needs access to a function without having to instantiate a class to join a set of strings (static). The Join method follows a kind of factory creational pattern.
So far, so good. I like what Microsoft did making one an instance member functions, and one a static member function. But that is where it ends. You would assume that Split and Join would accept the same types of arguments. For example, if one accepts a string as a delimiter, the other should accept a string as a delimiter. Not so.
It is these types of inconsistencies that bubble out during the initial release of any technology. .Net 1.0 was a huge milestone for Microsoft back in 2001. You may or may not recall those days, but Sun was taking a lot of developer real estate away from MS with Java 1.1 and Java 2.0. In my opinion, Microsoft was behind the 8-ball. When getting a massive API out the door that will be the development platform of the company for the next 10 - 20 years, one can be forgive for making little mistakes.
Still, I find things like this annoying. Why not fix a few of these glitches in .Net 2.0? There should be a simple overload of Split that takes 1 string argument (the delimiter), just like there is a simple overload of Join that takes 1 string argument (the delimiter) and the string array to join.
As an example, imagine our string from above is "Blue~|~Green~|~Red~|~Orange". In this string, "~|~" is the delimiter. The solution is to use the instance method of System.String, Split, passing as an argument an array of strings to use as delimiters. The most common usage is to send an array of length one.
string[] theSplits = myString.Split(new String[]{"~|~"}, StringSplitOptions.None);
To further complicate things, this overload (the simplest that accomplishes what is needed in this example) takes an often-unneeded, second argument. Why not just have the following, MS?
string[] theSplits = myString.Split("~|~");
The point I am getting to here is that the most obvious overload that should exist doesn't, and so "George" from above became confused and went looking for another split function. I have to give him kudos for looking in the Regex namespace. I try to think of things like this when I am writing code that other programmers will use. I *try* to write my functions in a consistent manner so that once a developer knows how to use one, they know how to use them all. A good guide to doing this is Microsoft's own Framework Design Guidelines book. They learned a lot while doing .Net 1.0, and formalized their design guidelines for internal use and then decided to publish them. This is a common reference whenever I get in a discussion about variable naming, etc, with fellow developers. I highly recommend the book.
Microsoft really should add the obvious Split overload to the String class. And with the C# 3.0 Extension Methods, you can do it for them.
Disclaimer: The Framework Design Guidelines link uses my Amazon affiliate membership, so I get a few cents if you purchase it with that link. The book is fairly popular for a tech book so you can probably find it at your local bookstore, B&N, Borders, or Bookpool.com as well as Amazon.com.
Thought! Attempting to successfully change the style for my current blog. Views on the nice style at https://lucidphotography.ca? Seriously an extraordinary wedding photographer in Calgary when needed within the Alberta location. Offer an opinion. Appreciated!
Now, we must express the seed for this important write-up was furnished through Michelle with countryestatesconstruction.ca. They are definitely a superb concrete and demolition contractors. We really value a great idea!
Inspirations For This Article
https://nemo-q.com - Thanks Declan. I do know you are very busy. Appreciate your taking some time.
https://pcesolutions.ca - Amongst my favorites.
http://quakesbaseball.com/ - Again, thank you for sharing the pix.
https://westwindows.on.ca - I vow next time, it's me giving back the favor.
https://northpointjunkremoval.com - Without fail you always help me break it down to make the post better!
Posted in Programming Post Date 11/17/2024