• (nodebb)

    But the code shown is clearly just a fragment of a method. There could easily be more tests after the one shown.

  • (nodebb)

    This isn't C#, but Java as C# uses PascalCase, not camelCase.

  • LZ79LRU (unregistered) in reply to colejohnson66

    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:

    • This is not in fact C#. The Length property is the dead giveaway. In C# it's uppercase L.
    • It's also not Java because in Java Length is a method and not a property.
    • The language uses curly braces.
    • The language supports exceptions.

    And from this analysis Java does indeed look to be the most likely candidate.

  • (nodebb)

    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.

  • Rob (unregistered)

    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

  • (nodebb)

    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?

  • Steve (unregistered)

    v.a.li.d

  • (nodebb)

    any 32 bit integer is potentially an IP address

    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.)

  • (nodebb) in reply to Steve

    a.l.s.o.v.a.l.i.d

  • Richard Brantley (unregistered)

    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.

  • Argle (unregistered)

    "One... two... five!" "Three, Sir." "Three!"

  • ZZartin (unregistered)

    Andy writes: "IPv6 will never be a thing!"

    So yes you would need to use a different algorithm to validate an IPv4 compared to an IPv6, why is that surprising?

  • the cow (not the robot) (unregistered) in reply to colejohnson66

    Almost all the programming languages use the mixture of cases and symbols that the programmer writes...

  • (nodebb) in reply to Richard Brantley

    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.

    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.

  • Fizzlecist (unregistered)

    "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

  • Stuart (unregistered) in reply to Gurth

    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.

  • richarson (unregistered) in reply to Gurth

    "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.

  • TheCPUWizard (unregistered)

    What is 'Weird" about RFC 1819 [I remember 1995 pretty well......]

  • LZ79LRU (unregistered) in reply to Gurth

    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.

  • (nodebb)

    A perfect bridge between IPv4 and IPv6.

    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 :.

  • (nodebb) in reply to jeremypnet

    Interpunct to the rescue! Obviously the symbol between . (binary 01) and : (binary 11) must be · (binary 10).

Leave a comment on “How to Validate an IP Address”

Log In or post as a guest

Replying to comment #:

« Return to Article