Thread: Psuedocode

  1. #31
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by cstryx View Post
    In my opinion, regex is not the best solution anyways and it's also going to be slower than regular string manipulation.
    IMO...

    This si a good time to use a regex.
    The input is from a 'trusted' source (not user input), is short and relatively constant.

    A regex will produce clear and maintainable code.

    A regex can be a configuation item, allowing easy change without a rebuild.


    BTW if performance is an issue, 'for' is faster than 'foreach' and implicit comparisions (>='0' and <= '9') are faster than string functions (IsDigit)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  2. #32
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by novacain View Post
    IMO...

    This si a good time to use a regex.
    The input is from a 'trusted' source (not user input), is short and relatively constant.

    A regex will produce clear and maintainable code.

    A regex can be a configuation item, allowing easy change without a rebuild.


    BTW if performance is an issue, 'for' is faster than 'foreach' and implicit comparisions (>='0' and <= '9') are faster than string functions (IsDigit)
    The compiler typically will optimize a simple foreach loop to a for loop anyways if you've ever debugged through .NET binaries for such analysis. Therefore, in theory yes, but not really because of the way it *really* works. Because of the compiler optimizations I doubt it would make a difference in this case, but you still maintain the verbosity of such simple syntax without risking performance. I was not going for fully optimized code regardless, but rather just to show how it would be possible without regex, because regex for such a small string will produce overhead.

    This is the regex I would use if I chose to go that way though:
    Code:
    ^[A-Z]{2}\d{4}$
    Why do you mention "'trusted' source" though? :/ If it was *truly* a trusted source validation would be unnecessary and a method to validate wouldn't be requested by OP; there must have been a reason he wanted validation for it.
    Last edited by cstryx; 09-13-2015 at 01:07 AM.

  3. #33
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Bottom line is that all this is off topic and irrelevant to the HOMEWORK problem the OP asked about.

    You wanted to show off but you just picked a silly thing to show off about, but picked on someone with enough experience to know you are wrong / irrelevant (check my join date, it is before C# was invented....).

    Quote Originally Posted by cstryx View Post
    The compiler typically will optimize a simple foreach loop to a for loop anyways
    Just not true.

    It depends on the type of object your are looping through, what compiler switches you use and what type of build.

    Quote Originally Posted by cstryx View Post
    because regex for such a small string will produce overhead.
    Yes.

    But who cares in a HOMEWORK problem?

    Do you think the OP would get more marks for saving a nono-seccond?

    Quote Originally Posted by cstryx View Post
    Why do you mention "'trusted' source" though? :/ If it was *truly* a trusted source validation would be unnecessary and a method to validate wouldn't be requested by OP; there must have been a reason he wanted validation for it.
    Files are more trustworthy than user input (as I stated).

    But files can become corrupt or contain errors.

    Good coders do not allow those types of issues to cause errors in their code, they write code that allows these types of data quality issues to be discovered and corrected.

    Quote Originally Posted by cstryx View Post
    Code:
    ^[A-Z]{2}\d{4}$
    You do know that '/d' will match to all UNICODE digits and that there are a considerable amount of them?

    That will cost you a nanosecond or 2.....

    ie /d matches to;

    0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹߀߁߂߃߄߅߆߇߈߉०१२३४५६७८९ ০১২৩৪৫৬৭৮৯੦੧੨੩੪੫੬੭੮੯૦૧૨૩૪૫૬૭૮૯୦୧୨୩୪୫୬୭୮୯௦௧௨௩௪௫௬௭௮௯ ౦౧౨౩౪౫౬౭౮౯೦೧೨೩೪೫೬೭೮೯൦൧൨൩൪൫൬൭൮൯๐๑๒๓๔๕๖๗๘๙໐໑໒໓໔໕໖໗໘໙ ༠༡༢༣༤༥༦༧༨༩၀၁၂၃၄၅၆၇၈၉႐႑႒႓႔႕႖႗႘႙០១២៣៤៥៦៧៨៩᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙ ᥆᥇᥈᥉᥊᥋᥌᥍᥎᥏᧐᧑᧒᧓᧔᧕᧖᧗᧘᧙᭐᭑᭒᭓᭔᭕᭖᭗᭘᭙᮰᮱᮲᮳᮴᮵᮶᮷᮸᮹᱀᱁᱂᱃᱄᱅᱆᱇᱈᱉ ᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙꘠꘡꘢꘣꘤꘥꘦꘧꘨꘩꣐꣑꣒꣓꣔꣕꣖꣗꣘꣙꤀꤁꤂꤃꤄꤅꤆꤇꤈꤉꩐꩑꩒꩓꩔꩕꩖꩗꩘꩙ 0123456789
    Last edited by novacain; 09-14-2015 at 01:20 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #34
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by novacain View Post
    Bottom line is that all this is off topic and irrelevant to the HOMEWORK problem the OP asked about.

    You wanted to show off but you just picked a silly thing to show off about, but picked on someone with enough experience to know you are wrong / irrelevant (check my join date, it is before C# was invented....).



    Just not true.

    It depends on the type of object your are looping through, what compiler switches you use and what type of build.



    Yes.

    But who cares in a HOMEWORK problem?

    Do you think the OP would get more marks for saving a nono-seccond?



    Files are more trustworthy than user input (as I stated).

    But files can become corrupt or contain errors.

    Good coders do not allow those types of issues to cause errors in their code, they write code that allows these types of data quality issues to be discovered and corrected.



    You do know that '/d' will match to all UNICODE digits and that there are a considerable amount of them?

    That will cost you a nanosecond or 2.....

    ie /d matches to;

    0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹߀߁߂߃߄߅߆߇߈߉०१२३४५६७८९ ০১২৩৪৫৬৭৮৯੦੧੨੩੪੫੬੭੮੯૦૧૨૩૪૫૬૭૮૯୦୧୨୩୪୫୬୭୮୯௦௧௨௩௪௫௬௭௮௯ ౦౧౨౩౪౫౬౭౮౯೦೧೨೩೪೫೬೭೮೯൦൧൨൩൪൫൬൭൮൯๐๑๒๓๔๕๖๗๘๙໐໑໒໓໔໕໖໗໘໙ ༠༡༢༣༤༥༦༧༨༩၀၁၂၃၄၅၆၇၈၉႐႑႒႓႔႕႖႗႘႙០១២៣៤៥៦៧៨៩᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙ ᥆᥇᥈᥉᥊᥋᥌᥍᥎᥏᧐᧑᧒᧓᧔᧕᧖᧗᧘᧙᭐᭑᭒᭓᭔᭕᭖᭗᭘᭙᮰᮱᮲᮳᮴᮵᮶᮷᮸᮹᱀᱁᱂᱃᱄᱅᱆᱇᱈᱉ ᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙꘠꘡꘢꘣꘤꘥꘦꘧꘨꘩꣐꣑꣒꣓꣔꣕꣖꣗꣘꣙꤀꤁꤂꤃꤄꤅꤆꤇꤈꤉꩐꩑꩒꩓꩔꩕꩖꩗꩘꩙ 0123456789
    Okay, I'm sorry but are you trying to imply that you have more (or lots of) experience with C# just based on your join date to this forum? :S Your join date doesn't say anything about your experience with C# because it isn't proof as to how much or how long you've worked with C#. The only thing that says is when you joined this forum...

    "Just not true" - I said typically, and in this case the requirements would hold true.

    "Files are more trustworthy than user input" - Huh? You don't think that a configuration file is meant to be edited by a user in some cases? It's still subject to user input, being a file doesn't make that foolproof.

    "Good coders do not allow those types of issues to cause errors in their code, they write code that allows these types of data quality issues to be discovered and corrected." - This is an unnecessary statement. This is the whole reason why this thread exists, you're stating the obvious here.

    I'm not a regex expert, I work with C# mainly, but what you just said about a regex pattern is just one more reason NOT to use it. It opens up the potential for a can of unwanted worms that may be transparent to the program and user. Same reason why char* literals (and especially non-const ones) are avoided by C/C++ programmers.

    My intention was not to show off, but the fact that you have to make a bold statement about your experience doesn't say otherwise in terms of what you are attempting to do here.
    Last edited by cstryx; 09-14-2015 at 07:43 AM.

Popular pages Recent additions subscribe to a feed