- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
But the code shown is clearly just a fragment of a method. There could easily be more tests after the one shown.
Admin
This isn't C#, but Java as C# uses PascalCase, not camelCase.
Admin
What you cited are just best practice conventions and not rules. Nobody is preventing you from writing your code in any case you want including but not ALL CAPS, all lowercase and even cyrilic. And yes, I have seen all of them. Yes, cyrilic.
Not to say that your guess is entirely wrong. Just for the wrong reason. If we want to see what language this actually is we need to look not at the code the developer wrote but what precious little code we have that is not entirely custom.
And for that we only have the following hints:
And from this analysis Java does indeed look to be the most likely candidate.
Admin
If that's the entirety of the checks then there's also 1.2.3.4.5.6.7 (however many dot-separated parts you want), not to mention not even caring if what's between the dots is numeric, let alone in range.
Admin
The lowercase "string" is for me a clear indication this is not Java. It's of course possible to write your own class called "string", but why would you?
My guess is that this is indeed C#, but "length" should probably be "Length". A copy-paste error seems likely. I thought it might be an extension property (because someone kept mistyping "length" and instead if fixing their code they added a workaround), but those don't exist yet: https://github.com/dotnet/csharplang/discussions/5811
Admin
What about StringTools.splitStr? Both Java and C# have a split method for the standard string class. A hint of another WTF, or just really old code?
Admin
v.a.li.d
Admin
Technically correct (the best kind of correct), in that a raw 32-bit integer is accepted and parsed by libc's
inet_aton()
and related functions. But no sane program ever uses that format for human output purposes. (It might be stored that way internally in memory, database, binary file, or network protocol, but it will never be displayed that way to a human.)Admin
a.l.s.o.v.a.l.i.d
Admin
What amazes me whenever I see this kind of thing is that languages like C# come with vast and useful libraries. A minute of digging will turn up the already existing methods that will solve this problem. Yea, it may slow you down by a matter of seconds, but it's worth it to learn something new you can leverage in the future. The code you don't have to write is code you don't have to debug.
Admin
"One... two... five!" "Three, Sir." "Three!"
Admin
So yes you would need to use a different algorithm to validate an IPv4 compared to an IPv6, why is that surprising?
Admin
Almost all the programming languages use the mixture of cases and symbols that the programmer writes...
Admin
You have to first think to yourself, “I wonder if there’s a method for this in a library?” though. If you already know there is, then you would use that — perhaps first having to search for what it is and how to use it — but if you don’t, chances are you’re not going to go looking for one, either. Because if your way of doing things is to start by searching for existing ways to solve every problem you encounter, you’ll probably on average spend more time searching than it would take to solve typical problems yourself.
Granted, the solution here is not exactly complete and it will happily pass things that aren’t valid IPv4 addresses, but that to me says it was written by someone who didn’t know (or wonder whether) there possibly are existing methods that could be used here, and so didn’t go looking for any, either.
Admin
"1.2.3." is also likely to be valid as the code looks at the length of the array, not the # of "." characters in the string. Any string with at least 3 dots in it would be valid - including the article & this comment
Admin
There are heuristics you can use, though. "Is this something that others are likely to want to use in their codebases?" If the answer is yes, then it's likely - not certain, but likely - that there's a library function for it. Parsing, or checking validity of, IP addresses is one thing for which the answer is most definitely yes; I'd definitely go looking for a useful method in the standard libraries before rolling my own. (And if I didn't find such a method, I'd be questioning the thoroughness of my search, and/or the sanity of whoever wrote those standard libraries.) Sorting is another. Converting strings to upper or lower case. The more fundamental a thing it is that you're trying to do, the more likely that there's a library function for the job, and the more likely I'm going to go looking for that library in preference to writing my own code to solve the problem.
There is NO excuse today for writing an IP address parser for any reason other than a learning exercise.
Admin
"Granted, the solution here is not exactly complete and it will happily pass things that aren’t valid IPv4 addresses, but that to me says it was written by someone who didn’t know (or wonder whether) there possibly are existing methods that could be used here, and so didn’t go looking for any, either."
So, that's two reasons to fire them.
Admin
What is 'Weird" about RFC 1819 [I remember 1995 pretty well......]
Admin
It's a way of thinking though. Once you are used to using these languages, or even ones with far less built in like C++ your first thought when facing a commonly occurring problem like validation or XML parsing or similar is that there is probably something built in or at least a commonly used and acknowledged library out there. And your first move absolutely becomes to google first. Because 6 minutes with google can save you 6 hours of planning, 6 days of coding and 6 years of debugging.
Admin
No, I think to be in the spirit of the thing, you need to use a different punctuation mark. IPv6 uses
:
and IPv4 uses.
. I was going to suggest;
but that's not "in between.
and:
.Admin
Interpunct to the rescue! Obviously the symbol between
.
(binary 01) and:
(binary 11) must be·
(binary 10).