Ita Matauriro Enguva Dzose Anoedzwa uye Anonzwisisika
TL; DR: Unogona kuputsa yakaoma yekusimbisa regex kuita zvikamu zvidiki kuti uedze chikamu chega chega uye utaure zvikanganiso chaizvo.
Matambudziko Agadziriswa 😔
- Zvakaoma-ku-yedza zvinogara zvichitaurwa
- Kuzivisa kukanganisa kusina kujeka
- Kugadzirisa hope dzinotyisa
- Matambudziko ekuchengetedza
- Mitsetse yakareba uye nzira
- Matauriro asingagarike
- Primitive Obsession
- Kusarudzika kukanganisa
- Knowledge silos
- Maonero akapera
- Zvikanganiso pasina tsitsi kugumisa vashandisi
Related Code Kunhuwa 💨
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxv
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxvii
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xx-we-have-reached-100
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-ix-7rr33ol
Matanho 👣
- Ongorora regex kuti uone zvikamu zvayo zvine musoro.
- Dhonza iyo regex kuita diki, yakatumidzwa sub-patterns yechinhu chimwe nechimwe.
- Nyora bvunzo dzeyuniti yega yega-pattern kuti uone kuti inoshanda nemazvo.
- Sanganisa akaedzwa madiki-patterns mune yakazara yekusimbisa logic.
- Refactor iyo kodhi kuti ipe yakajeka mhosho meseji kune yega yega inotadza chikamu.
Muenzaniso Code 💻
Pamberi 🚨
function validateURL(url) { const urlRegex = /^(https?:\/\/)([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})(\/.*)?$/; // Criptic and untesteable return urlRegex.test(url); }
Mushure 👉
// Step 1: Define individual regex components const protocolPattern = /^(https?:\/\/)/; const domainPattern = /^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; const pathPattern = /^\/.*$/; // Step 2: Write unit tests for each component describe("Protocol Validation", () => { test("should pass for http://", () => { expect(protocolPattern.test("http://")).toBe(true); }); test("should pass for https://", () => { expect(protocolPattern.test("https://")).toBe(true); }); test("should fail for invalid protocols", () => { expect(protocolPattern.test("ftp://")).toBe(false); }); }); describe("Domain Validation", () => { test("should pass for valid domains", () => { expect(domainPattern.test("example.com")).toBe(true); expect(domainPattern.test("sub.domain.org")).toBe(true); }); test("should fail for invalid domains", () => { expect(domainPattern.test("example")).toBe(false); expect(domainPattern.test("domain..com")).toBe(false); }); }); describe("Path Validation", () => { test("should pass for valid paths", () => { expect(pathPattern.test("/path/to/resource")).toBe(true); expect(pathPattern.test("/")).toBe(true); }); test("should fail for invalid paths", () => { expect(pathPattern.test("path/to/resource")).toBe(false); expect(pathPattern.test("")).toBe(false); }); }); // Step 3: Validate each part and report errors function validateURL(url) { if (!protocolPattern.test(url)) { throw new Error("Invalid protocol. Use http:// or https://."); } const domainStartIndex = url.indexOf("://") + 3; const domainEndIndex = url.indexOf("/", domainStartIndex); const domain = domainEndIndex === -1 ? url.slice(domainStartIndex) : url.slice(domainStartIndex, domainEndIndex); if (!domainPattern.test(domain)) { throw new Error("Invalid domain name."); } const path = url.slice(domainEndIndex); if (path && !pathPattern.test(path)) { throw new Error("Invalid path."); } return true; } // Step 4: Add integration tests for the full URL validation describe("Full URL Validation", () => { test("should pass for valid URLs", () => { expect(validateURL("https://lesluthiers.com/tour/")).toBe(true); expect(validateURL("https://bio.lesluthiers.org/")).toBe(true); }); test("should fail for invalid URLs", () => { expect(() => validateURL("ftp://mastropiero.com")). toThrow("Invalid protocol"); expect(() => validateURL("http://estherpsicore..com")). toThrow("Invalid domain name"); expect(() => validateURL("http://book.warren-sanchez")). toThrow("Invalid path"); }); });
Type 📝
- [x] Semi-Otomatiki
Safety 🛡️
Iyi refactoring yakachengeteka kana iwe ukatevera matanho nemazvo.
Kuedza chikamu chimwe nechimwe kunovimbisa kuti iwe unobata zvikanganiso nekukurumidza.
Sei Code iri Nani? ✨
Iyo refactored kodhi iri nani nekuti inovandudza kuverenga, kuchengetedza, uye testability.
Kuputsa regex kuita zvikamu zvidiki kunoita kuti kunzwisisa izvo chikamu chimwe nechimwe chiite nyore.
Iwe unogona zvakare kutaura zvikanganiso zvakati kana kusimbiswa kwatadza, izvo zvinobatsira vashandisi kugadzirisa zvavanoisa.
Uyu zvakare mukana wakanaka wekushandisa iyo Test-Driven Development tekinoroji, zvishoma nezvishoma ichiwedzera kuoma nekuunza zvidimbu zvitsva.
Inovandudza Sei Kukurukurirana? 🗺️
Nekutyora regex kuita zvidiki, zvine musoro zvikamu, iwe unogadzira mepu iri padyo pakati pezvido zveReal-World (semuenzaniso, "URL inofanirwa kuve neprotocol inoshanda") nekodhi.
Izvi zvinoderedza kusanzwisisika uye zvinoita kuti kodhi iratidze dambudziko redunhu nemazvo.
Kuganhurirwa ⚠️
Iyi nzira inogona kuwedzera imwe pamusoro kune yakapusa regex mapatani uko kuapwanya pasi kungave kusingakoshe.
Refactor neAI 🤖
Unogona kushandisa maturusi eAI kubatsira kuona regex zvikamu.
Bvunza iyo AI kuti itsanangure zvinoitwa nechikamu chega chega cheregex, yobva yakutungamira mukuityora kuita zvidimbu zvidiki zvinoyedzwa. Semuenzaniso, unogona kubvunza kuti, "Regex iyi inoitei?" uye wotevera, "Ndingaipatsanura sei kuita zvikamu zvidiki?".
Ndi2025, Hapana programmer anofanirwa kunyora nyowani Regular Expressions zvakare.
Iwe unofanirwa kusiya iyi mechanic basa kuAI.
Kurudziro: 1. Ongorora regex kuti uone zvikamu zvayo zvine musoro.2. Kamura regex kuita diki, dzine mazita madiki-patterns pachikamu chimwe nechimwe.3. Nyora bvunzo dzeyuniti padiki-pattern kuti uone kuti inoshanda nemazvo.4. Sanganisa akaedzwa madiki-patterns mune yakazara yekusimbisa logic.5. Refactor iyo kodhi kuti ipe yakajeka mhosho meseji kune yega yega inotadza chikamu.
Pasina Mirayiridzo Yakakodzera | NeMirayiridzo Yakananga |
---|---|
Tags 🏷️
- Testability
Level 🔋
- [x] Pakati
Zvinoenderana Refactorings 🔄
Onawo 📚
Credits 🙏
Mufananidzo naGerd Altmann paPixabay
Ichi chinyorwa chikamu cheRefactoring Series.