# Mastering GUIDs with Occam's Razor

**URL:** <https://discourse.codinghorror.com/t/mastering-guids-with-occams-razor/1004>\
**Category:** blog\
**Created:** [September 27, 2005, 12:00am UTC](https://discourse.codinghorror.com/t/mastering-guids-with-occams-razor/1004 "2005-09-27T00:00:00Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![Mo122](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/m/f1d935/32.png) [@Mo122](https://discourse.codinghorror.com/u/Mo122)\
**Post date:** [February 1, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/mastering-guids-with-occams-razor/1004/21 "2009-02-01T00:00:00Z")

</div>

Interesting that you’re only checking that your string LOOKS like a GUID, not that it actually IS one, but I’m just being pedantic (hint: UUIDs/GUIDs are versioned, with the latter being a subset of the former–not all UUIDs are GUIDs [as defined by Microsoft])

---

<div class="post-metadata">

**Author:** ![Andy](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/a/aeb1de/32.png) [@Andy](https://discourse.codinghorror.com/u/Andy)\
**Post date:** [February 1, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/mastering-guids-with-occams-razor/1004/22 "2009-02-01T00:00:00Z")

</div>

When push comes to shove, using exceptions to control program flow is quite evil. It’s basically a posh GOTO. The regex is effectively a hard coded literal, but it can be wrapped up in a constant somewhere.

---

<div class="post-metadata">

**Author:** ![Mark](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/m/e9c0ed/32.png) [@Mark](https://discourse.codinghorror.com/u/Mark)\
**Post date:** [February 17, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/mastering-guids-with-occams-razor/1004/23 "2009-02-17T00:00:00Z")

</div>

> using exceptions to control program flow is evil

So is not providing an equivalent TryParse method for every Parse method in the BCL.

---

<div class="post-metadata">

**Author:** ![Ron](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/r/ecae2f/32.png) [@Ron](https://discourse.codinghorror.com/u/Ron)\
**Post date:** [February 6, 2010, 12:00am UTC](https://discourse.codinghorror.com/t/mastering-guids-with-occams-razor/1004/24 "2010-02-06T00:00:00Z")

</div>

I think most sane people would prefer the non-exception-catching dependent way. However, I would step into the guid constructor source to see what they are doing. Using a regex to match a guid pattern seems like enormous overkill.

---

<div class="post-metadata">

**Author:** ![Simon](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/s/f07891/32.png) [@Simon](https://discourse.codinghorror.com/u/Simon)\
**Post date:** [February 6, 2010, 12:00am UTC](https://discourse.codinghorror.com/t/mastering-guids-with-occams-razor/1004/25 "2010-02-06T00:00:00Z")

</div>

Interestingly everyone has fallen into the trap of discussing this non-topic. Yes, the regex version is faster, and if you need to validate 100,000 guids in your normal program operation (as opposed to just a one off) then use the regex.

For every other scenario i’ll stick with the one which is easier to type and remember. Of course, if they stick a regex version into the TryParse function of the GUID object in .net 3.5 then i’ll happily use it every time.

---

<div class="post-metadata">

**Author:** ![codinghorror](https://discourse.codinghorror.com/user_avatar/discourse.codinghorror.com/codinghorror/32/36371_2.png) [@codinghorror](https://discourse.codinghorror.com/u/codinghorror)\
**Post date:** [January 7, 2025, 6:39am UTC](https://discourse.codinghorror.com/t/mastering-guids-with-occams-razor/1004/27 "2025-01-07T06:39:52Z")

</div>

Closed on January 7, 2025, 6:39am UTC



[Previous page](https://discourse.codinghorror.com/t/mastering-guids-with-occams-razor/1004.md?page=1)
