I never really comprehend why the author of Genesis included the infamous story of Judah and Tamar. Aside from reinforcing my perception of the Scriptures — that it is a really honest book — the story, read retrospectively, serves to tell us the genesis of Judah, since Judah would later be the major, and royal, tribe of Israel.
Nevertheless, another question popped up: why was it located within the story of Joseph and not before it? The answer perhaps lies in what we read after the story of Judah and Tamar. That is, the story of Joseph being seduced by Potiphar’s wife. So the contrast is established between these two half-brothers. Judah fell into prey easily, while Joseph withstood the challenge and maintained his innocence.