{"id":3016,"date":"2020-06-24T08:53:38","date_gmt":"2020-06-24T08:53:38","guid":{"rendered":"http:\/\/the-codest.localhost\/blog\/a-quick-primer-on-refactoring-for-beginners\/"},"modified":"2026-04-24T11:30:09","modified_gmt":"2026-04-24T11:30:09","slug":"strucny-uvod-do-refaktoringu-pro-zacatecniky","status":"publish","type":"post","link":"https:\/\/thecodest.co\/cs\/blog\/a-quick-primer-on-refactoring-for-beginners\/","title":{"rendered":"Stru\u010dn\u00fd \u00favod do refaktoringu pro za\u010d\u00e1te\u010dn\u00edky"},"content":{"rendered":"<p>Proto je pro n\u011bkter\u00e9 nepochopiteln\u00e9, \u017ee <strong>refaktoring<\/strong> je vlastn\u011b oblast programov\u00e1n\u00ed a je tak\u00e9 velmi d\u016fle\u017eitou sou\u010d\u00e1st\u00ed pr\u00e1ce program\u00e1tora. K\u00f3d se neust\u00e1le vyv\u00edj\u00ed, bude se upravovat, dokud bude existovat mo\u017enost p\u0159id\u00e1v\u00e1n\u00ed nov\u00fdch funkc\u00ed. M\u016f\u017ee v\u0161ak nab\u00fdt takov\u00e9 podoby, \u017ee ji\u017e neumo\u017e\u0148uje efektivn\u00ed p\u0159id\u00e1v\u00e1n\u00ed nov\u00fdch funkc\u00ed a bylo by jednodu\u0161\u0161\u00ed cel\u00fd program p\u0159epsat.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Co je refaktoring?<\/h2>\n\n\n\n<p>Obvykle se setk\u00e1te s odpov\u011bd\u00ed, \u017ee se jedn\u00e1 o zm\u011bnu struktury k\u00f3du pomoc\u00ed \u0159ady refaktoriza\u010dn\u00edch transformac\u00ed, ani\u017e by bylo ovlivn\u011bno pozorovateln\u00e9 chov\u00e1n\u00ed k\u00f3du. To je pravda. Ned\u00e1vno jsem tak\u00e9 narazil na definici podle <b>Martin Fowler<\/b> ve sv\u00e9 knize <b>\"Zlep\u0161en\u00ed n\u00e1vrhu st\u00e1vaj\u00edc\u00edho kodexu\"<\/b> kde popisuje <strong>refaktoring<\/strong> jako \"prov\u00e1d\u011bn\u00ed velk\u00fdch zm\u011bn po mal\u00fdch kroc\u00edch\". Popisuje <strong>refaktoring<\/strong> jako zm\u011bnu k\u00f3du, kter\u00e1 nem\u00e1 vliv na jeho fungov\u00e1n\u00ed, ale zd\u016fraz\u0148uje, \u017ee je t\u0159eba ji prov\u00e1d\u011bt po mal\u00fdch kroc\u00edch.<\/p>\n\n\n\n<p>V knize se tak\u00e9 uv\u00e1d\u00ed, \u017ee <strong>refaktoring<\/strong> neovliv\u0148uje fungov\u00e1n\u00ed k\u00f3du a upozor\u0148uje, \u017ee nem\u00e1 \u017e\u00e1dn\u00fd vliv na \u00fasp\u011b\u0161n\u00e9 absolvov\u00e1n\u00ed test\u016f. Popisuje krok za krokem, jak bezpe\u010dn\u011b prov\u00e9st <strong>refaktoring<\/strong>. Jeho kniha se mi l\u00edbila, proto\u017ee popisuje jednoduch\u00e9 triky, kter\u00e9 lze vyu\u017e\u00edt p\u0159i ka\u017edodenn\u00ed pr\u00e1ci.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pro\u010d pot\u0159ebujeme refaktoring?<\/h2>\n\n\n\n<p>&nbsp;Nej\u010dast\u011bji ji m\u016f\u017eete pot\u0159ebovat, kdy\u017e chcete zav\u00e9st novou funkci a k\u00f3d ve sv\u00e9 sou\u010dasn\u00e9 verzi to neumo\u017e\u0148uje nebo by to bylo obt\u00ed\u017en\u011bj\u0161\u00ed beze zm\u011bn v k\u00f3du. Hod\u00ed se tak\u00e9 v p\u0159\u00edpadech, kdy je p\u0159id\u00e1v\u00e1n\u00ed dal\u0161\u00edch funkc\u00ed \u010dasov\u011b nev\u00fdhodn\u00e9, tj. bylo by rychlej\u0161\u00ed k\u00f3d p\u0159epsat od za\u010d\u00e1tku. Mysl\u00edm, \u017ee se n\u011bkdy zapom\u00edn\u00e1, \u017ee <strong>refaktoring<\/strong> m\u016f\u017ee b\u00fdt k\u00f3d \u010dist\u0161\u00ed a \u010diteln\u011bj\u0161\u00ed. Martin ve sv\u00e9 knize p\u00ed\u0161e, jak prov\u00e1d\u00ed refaktoring, kdy\u017e c\u00edt\u00ed v k\u00f3du nep\u0159\u00edjemn\u00fd z\u00e1pach, a jak to \u0159\u00edk\u00e1, <b>\"v\u017edycky nech\u00e1v\u00e1 prostor pro to lep\u0161\u00ed\"<\/b>. A p\u0159ekvapil m\u011b t\u00edm, \u017ee refaktoring pova\u017euje za sou\u010d\u00e1st ka\u017edodenn\u00ed pr\u00e1ce s k\u00f3dem. Pro m\u011b jsou k\u00f3dy \u010dasto nesrozumiteln\u00e9, \u010d\u00edst je je trochu z\u00e1\u017eitek, proto\u017ee k\u00f3d je \u010dasto neintuitivn\u00ed.<\/p>\n\n\n\n<p>Charakteristick\u00fdm rysem dob\u0159e navr\u017een\u00e9ho programu je jeho <b>modularita<\/b>, d\u00edky \u010demu\u017e sta\u010d\u00ed zn\u00e1t jen malou \u010d\u00e1st k\u00f3du, aby bylo mo\u017en\u00e9 zav\u00e9st v\u011bt\u0161inu \u00faprav. Modularita tak\u00e9 usnad\u0148uje n\u00e1stup nov\u00fdch lid\u00ed a umo\u017e\u0148uje jim efektivn\u011bji za\u010d\u00edt pracovat. Aby bylo mo\u017en\u00e9 t\u00e9to modularity dos\u00e1hnout, mus\u00ed b\u00fdt p\u0159\u00edbuzn\u00e9 programov\u00e9 prvky seskupeny dohromady, p\u0159i\u010dem\u017e souvislosti mus\u00ed b\u00fdt srozumiteln\u00e9 a snadno naleziteln\u00e9. Neexistuje \u017e\u00e1dn\u00e9 jednotn\u00e9 pravidlo, jak toho dos\u00e1hnout. Jakmile pozn\u00e1te a pochop\u00edte v\u00edce a l\u00e9pe, jak m\u00e1 k\u00f3d fungovat, m\u016f\u017eete prvky seskupovat, ale n\u011bkdy je tak\u00e9 mus\u00edte testovat a kontrolovat.<\/p>\n\n\n\n<p>Jedn\u00edm z pravidel refaktoringu v <b>YAGNI<\/b>, co\u017e je zkratka pro \"You Aren't Gonna Need It\" (Nebude\u0161 to pot\u0159ebovat) a poch\u00e1z\u00ed z anglick\u00e9ho n\u00e1zvu <b>eXtreme Programming (XP) <\/b>pou\u017e\u00edv\u00e1 se zejm\u00e9na v <strong><a href=\"https:\/\/thecodest.co\/cs\/blog\/how-to-implement-agile-methodology\/\">Agiln\u00ed<\/a> <a href=\"https:\/\/thecodest.co\/cs\/blog\/8-key-questions-to-ask-your-software-development-outsourcing-partner\/\">v\u00fdvoj softwaru<\/a> t\u00fdmy<\/strong>. Zkr\u00e1tka a dob\u0159e, <b>YAGNI<\/b> \u0159\u00edk\u00e1, \u017ee by se m\u011bly prov\u00e1d\u011bt pouze aktu\u00e1ln\u00ed v\u011bci. To v podstat\u011b znamen\u00e1, \u017ee i kdy\u017e by n\u011bco mohlo b\u00fdt v budoucnu pot\u0159eba, nem\u011blo by se to d\u011blat hned.  Ale tak\u00e9 nem\u016f\u017eeme drtit dal\u0161\u00ed roz\u0161\u00ed\u0159en\u00ed a zde se st\u00e1v\u00e1 d\u016fle\u017eitou modularita.<\/p>\n\n\n\n<p>Kdy\u017e mluv\u00edme o <strong>refaktoring<\/strong>, je t\u0159eba zm\u00ednit jeden z nejpodstatn\u011bj\u0161\u00edch prvk\u016f, tj. testy. Na str\u00e1nk\u00e1ch <strong>refaktoring<\/strong>, pot\u0159ebujeme v\u011bd\u011bt, \u017ee k\u00f3d st\u00e1le funguje, proto\u017ee <strong>refaktoring<\/strong> nem\u011bn\u00ed zp\u016fsob fungov\u00e1n\u00ed, ale jeho strukturu, tak\u017ee v\u0161echny testy mus\u00ed b\u00fdt spln\u011bny. Nejlep\u0161\u00ed je po ka\u017ed\u00e9 mal\u00e9 transformaci spustit testy pro \u010d\u00e1st k\u00f3du, na kter\u00e9 pracujeme. To d\u00e1v\u00e1 <a href=\"https:\/\/thecodest.co\/cs\/blog\/why-us-companies-are-opting-for-polish-developers\/\">n\u00e1s<\/a> potvrzen\u00ed, \u017ee v\u0161e funguje, jak m\u00e1, a zkracuje dobu cel\u00e9 operace. Pr\u00e1v\u011b o tom mluv\u00ed Martin ve sv\u00e9 knize - prov\u00e1d\u011bjte testy co nej\u010dast\u011bji, abyste nemuseli d\u011blat krok zp\u011bt a ztr\u00e1cet \u010das hled\u00e1n\u00edm transformace, kter\u00e1 n\u011bco rozbila.<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/thecodest.co\/cs\/dictionary\/what-is-code-refactoring\/\">Refaktorizace k\u00f3du<\/a><\/strong> bez testov\u00e1n\u00ed je to otrava a je velk\u00e1 \u0161ance, \u017ee se n\u011bco pokaz\u00ed. Pokud je to mo\u017en\u00e9, bylo by nejlep\u0161\u00ed p\u0159idat alespo\u0148 z\u00e1kladn\u00ed testy, kter\u00e9 n\u00e1m daj\u00ed trochu jistoty, \u017ee k\u00f3d funguje.<\/p>\n\n\n\n<p>N\u00ed\u017ee uveden\u00e9 transformace jsou pouze p\u0159\u00edklady, ale jsou opravdu u\u017eite\u010dn\u00e9 p\u0159i ka\u017edodenn\u00edm programov\u00e1n\u00ed:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Extrakce funkc\u00ed a prom\u011bnn\u00fdch - pokud je funkce p\u0159\u00edli\u0161 dlouh\u00e1, zkontrolujte, zda existuj\u00ed n\u011bjak\u00e9 men\u0161\u00ed funkce, kter\u00e9 by bylo mo\u017en\u00e9 extrahovat. Tot\u00e9\u017e plat\u00ed pro dlouh\u00e9 \u0159\u00e1dky. Tyto transformace mohou pomoci p\u0159i hled\u00e1n\u00ed duplicit v k\u00f3du. D\u00edky Mal\u00fdm funkc\u00edm se k\u00f3d st\u00e1v\u00e1 p\u0159ehledn\u011bj\u0161\u00edm a srozumiteln\u011bj\u0161\u00edm,<\/li>\n\n\n\n<li>P\u0159ejmenov\u00e1n\u00ed funkc\u00ed a prom\u011bnn\u00fdch - pro spr\u00e1vn\u00e9 programov\u00e1n\u00ed je d\u016fle\u017eit\u00e9 pou\u017e\u00edvat spr\u00e1vn\u00e9 pojmenov\u00e1n\u00ed. Vhodn\u011b zvolen\u00e9 n\u00e1zvy prom\u011bnn\u00fdch mohou o k\u00f3du hodn\u011b napov\u011bd\u011bt,<\/li>\n\n\n\n<li>Seskupen\u00ed funkc\u00ed do jedn\u00e9 t\u0159\u00eddy - tato zm\u011bna je u\u017eite\u010dn\u00e1, pokud dv\u011b t\u0159\u00eddy prov\u00e1d\u011bj\u00ed podobn\u00e9 operace, proto\u017ee m\u016f\u017ee zkr\u00e1tit d\u00e9lku t\u0159\u00eddy,<\/li>\n\n\n\n<li>P\u0159ekr\u00fdv\u00e1n\u00ed vno\u0159en\u00e9ho p\u0159\u00edkazu - pokud podm\u00ednka vyhovuje pro zvl\u00e1\u0161tn\u00ed p\u0159\u00edpad, vydejte p\u0159\u00edkaz return, kdy\u017e podm\u00ednka nastane. Tyto typy test\u016f se \u010dasto ozna\u010duj\u00ed jako ochrann\u00e1 klauzule. Nahrazen\u00ed vno\u0159en\u00e9ho podm\u00edn\u011bn\u00e9ho p\u0159\u00edkazu p\u0159\u00edkazem ukon\u010den\u00ed m\u011bn\u00ed d\u016fraz v k\u00f3du. Konstrukce if-else p\u0159i\u0159azuje ob\u011bma variant\u00e1m stejnou v\u00e1hu. Pro toho, kdo k\u00f3d \u010dte, je to zpr\u00e1va, \u017ee ka\u017ed\u00e1 z nich je stejn\u011b pravd\u011bpodobn\u00e1 a d\u016fle\u017eit\u00e1,<\/li>\n\n\n\n<li>Zaveden\u00ed zvl\u00e1\u0161tn\u00edho p\u0159\u00edpadu - pokud n\u011bkter\u00e9 podm\u00ednky pou\u017e\u00edv\u00e1te ve sv\u00e9m k\u00f3du mnohokr\u00e1t, mo\u017en\u00e1 by st\u00e1lo za to vytvo\u0159it pro n\u011b samostatnou strukturu. D\u00edky tomu lze v\u011bt\u0161inu kontrol zvl\u00e1\u0161tn\u00edch p\u0159\u00edpad\u016f nahradit jednoduch\u00fdm vol\u00e1n\u00edm funkc\u00ed. \u010castou hodnotou, kter\u00e1 vy\u017eaduje zvl\u00e1\u0161tn\u00ed zpracov\u00e1n\u00ed, je \u010dasto null. Proto se tento vzor \u010dasto naz\u00fdv\u00e1 nulov\u00fd objekt. Tento p\u0159\u00edstup v\u0161ak lze pou\u017e\u00edt v jak\u00e9mkoli zvl\u00e1\u0161tn\u00edm p\u0159\u00edpad\u011b,<\/li>\n\n\n\n<li>Nahrazen\u00ed podm\u00edn\u011bn\u00e9ho polymorfismu instrukc\u00ed.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">P\u0159\u00edklad<\/h2>\n\n\n\n<p>Toto je \u010dl\u00e1nek o <strong>refaktoring<\/strong> a je t\u0159eba uv\u00e9st p\u0159\u00edklad. N\u00ed\u017ee chci uk\u00e1zat jednoduchou uk\u00e1zku refaktorizace s pou\u017eit\u00edm <b>P\u0159eps\u00e1n\u00ed vno\u0159en\u00e9ho p\u0159\u00edkazu<\/b> a <b>Nahrazen\u00ed podm\u00edn\u011bn\u00e9ho polymorfismu instrukc\u00ed<\/b>. \u0158ekn\u011bme, \u017ee m\u00e1me programovou funkci, kter\u00e1 vrac\u00ed a <a href=\"https:\/\/thecodest.co\/cs\/blog\/hash-to-use-or-not-to-use\/\">hash<\/a> s informacemi o tom, jak zal\u00e9vat rostliny v re\u00e1ln\u00e9m \u017eivot\u011b. Takov\u00e9 informace by pravd\u011bpodobn\u011b byly v modelu, ale pro tento p\u0159\u00edklad je m\u00e1me ve funkci.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">def zavla\u017eov\u00e1n\u00ed_info(plant)\n     result = {}\n     if plant.is_a? Suculent || plant.is_a? Cactus\n         result = { water_amount: \"Trochu \" , how_to: \"Ze dna\", zal\u00e9v\u00e1n\u00ed_d\u00e9lka: \"2 t\u00fddny\" }\n     elsif plant.is_a? Alocasia || plant.is_a? Maranta\n         result = { water_amount: \"Velk\u00e9 mno\u017estv\u00ed\", how_to: \"Jak si p\u0159ejete\", watering_duration: \"5 dn\u00ed\" }\n     elsif plant.is_a? Peperomia\n         result = { water_amount: \"Dicent amount\",\n             how_to: \"nemaj\u00ed r\u00e1dy vodu na listech\",\n             doba zal\u00e9v\u00e1n\u00ed: \"1 t\u00fdden\" }\n     else\n         result = { water_amount: \"Dicent amount\",\n             how_to: \"Jak si p\u0159ejete\",\n             doba trv\u00e1n\u00ed zavla\u017eov\u00e1n\u00ed: \"1 t\u00fdden\"\n             }\n     end\n     return result\n konec\n<\/code><\/pre>\n\n\n\n<p>Jde o to, aby se zm\u011bnila, pokud se vr\u00e1t\u00ed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">if plant.isa? Suculent || plant.isa? Cactus\n\n     result = { wateramount: \"A little bit \" , howto: \"Ze dna\",\n<\/code><\/pre>\n\n\n\n<p>Na<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"><code>return { water_amount: \"Trochu \" , how_to: \"Ze dna\",watering_duration: \"2 t\u00fddny\" } if plant.is_a? Suculent || plant.is_a? Cactus<\/code><\/code><\/pre>\n\n\n\n<p>vr\u00e1tit { voda<em>\u010d\u00e1stka: \"Trochu\" , jak<\/em>na: \"Odspodu\", zal\u00e9v\u00e1n\u00ed<em>trv\u00e1n\u00ed: \"if plant.is<\/em>a? Suculent || plant.is_a? Cactus<\/p>\n\n\n\n<p>A tak d\u00e1le se v\u0161\u00edm v\u0161udy, a\u017e dojdeme k funkci, kter\u00e1 vypad\u00e1 takto:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">def zavla\u017eov\u00e1n\u00ed_info(plant)\n\nreturn result = { wateramount: \"a little bit \" , howto: \"Ze dna\", wateringduration: \"2 t\u00fddny\" } if plant.isa? Suculent || plant.is_a? Cactus\n\nreturn result = { wateramount: \"Velk\u00e9 mno\u017estv\u00ed\", howto: \"Jak si p\u0159ejete\", wateringduration: \"5 dn\u00ed\" } if plant.isa? Alocasia || plant.is_a? Maranta\n\nreturn result = { water_amount: \"Dicent amount\",\n\n          howto: \"Odspodu! nemaj\u00ed r\u00e1dy vodu na listech\",\n          wateringduration: \"1 t\u00fdden\" } if plant.is_a? Peperomia\n\nreturn result = { water_amount: \"Dicent amount\",\n\n          how_to: \"Jak si p\u0159ejete\",\n\n          zal\u00e9v\u00e1n\u00ed_duration: \"1 t\u00fdden\"\n\n          }\n\nend<\/code><\/pre>\n\n\n\n<p>\u00a0Na sam\u00e9m konci jsme ji\u017e m\u011bli v\u00fdsledek. A dobr\u00fdm zvykem je postupovat krok za krokem a ka\u017edou zm\u011bnu otestovat. Tento blok if byste mohli nahradit p\u0159\u00edpadem switch a hned by to vypadalo l\u00e9pe a nemuseli byste poka\u017ed\u00e9 kontrolovat v\u0161echna if. Vypadalo by to takto:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">def zavla\u017eov\u00e1n\u00ed_info(plant)\n\nswich plant.class.to_string\n\ncase sukulent, kaktus\n\n     { wateramount: \"A little bit \" , howto: \"Ze dna\", zal\u00e9v\u00e1n\u00ed_duration: \"2 t\u00fddny\" }\n\ncase Alocasia, Maranta\n\n     { wateramount: \"Velk\u00e9 mno\u017estv\u00ed\", howto: D\u00e9lka zal\u00e9v\u00e1n\u00ed: \"Jak si p\u0159ejete\", watering_duration: \"5 dn\u00ed\" }\n\ncase Peperomia\n\n     { water_amount: \"Dicent amount\",\n\n          how_to: \"nemaj\u00ed r\u00e1dy vodu na listech\",\n\n          doba zal\u00e9v\u00e1n\u00ed: \"1 t\u00fdden\" }\n\njinak\n\n     { water_amount: \"Dicent amount\",\n\n            how_to: \"Jak si p\u0159ejete\",\n\n       zal\u00e9v\u00e1n\u00ed_duration: \"1 t\u00fdden\" }\n\nend\n\nend<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>A pak m\u016f\u017eete pou\u017e\u00edt <b>Nahrazen\u00ed podm\u00edn\u011bn\u00e9ho polymorfismu instrukc\u00ed<\/b>. To znamen\u00e1 vytvo\u0159it t\u0159\u00eddu s funkc\u00ed, kter\u00e1 vr\u00e1t\u00ed spr\u00e1vnou hodnotu a p\u0159epne je na spr\u00e1vn\u00e9 m\u00edsto.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">t\u0159\u00edda Suculent\n\n...\n\ndef zal\u00e9v\u00e1n\u00ed_info()\n\n     return { wateramount: \"Trochu \" , howto: \"Ze dna\", watering_duration: \"2 t\u00fddny\" }\n\nend\n\nend\n\nt\u0159\u00edda Cactus\n\n...\n\ndef zal\u00e9v\u00e1n\u00ed_info()\n\n     return { wateramount: \"Trochu \" , howto: \"Ze dna\", watering_duration: \"2 t\u00fddny\" }\n\nend\n\nend\n\nt\u0159\u00edda Alocasia\n\n...\n\ndef zavla\u017eov\u00e1n\u00ed_info\n\n     return { wateramount: \"Velk\u00e9 mno\u017estv\u00ed\", howto: \"Jak si p\u0159ejete\", watering_duration: \"5 dn\u00ed\" }\n\nend\n\nend\n\nt\u0159\u00edda Maranta\n\n...\n\ndef zavla\u017eov\u00e1n\u00ed_info\n\n     return { wateramount: \"Velk\u00e9 mno\u017estv\u00ed\", howto: \"Jak si p\u0159ejete\", watering_duration: \"5 dn\u00ed\" }\n\nend\n\nend\n\nt\u0159\u00edda Peperomia\n\n...\n\ndef zavla\u017eov\u00e1n\u00ed_info\n\n     return { water_amount: \"Dicent amount\",\n\n      how_to: \"nemaj\u00ed r\u00e1di vodu na listech\",\n\n      doba zal\u00e9v\u00e1n\u00ed: \"1 t\u00fdden\" }\n\nend\n\nend\n\nt\u0159\u00edda Plant\n\n...\n\ndef zavla\u017eov\u00e1n\u00ed_info\n\n     return { water_amount: \"Dicent amount\",\n\n              how_to: \"Jak si p\u0159ejete\",\n\n              zal\u00e9v\u00e1n\u00ed_duration: \"1 t\u00fdden\" }\n\nend\n\nend<\/code><\/pre>\n\n\n\n<p>A v hlavn\u00ed funkci zavla\u017eov\u00e1n\u00ed_infofunkce bude k\u00f3d vypadat takto:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">def zavla\u017eov\u00e1n\u00ed_info(plant)\n\n    plant.map(&amp;:watering_info)\n\nkonec<\/code><\/pre>\n\n\n\n<p>Tuto funkci lze samoz\u0159ejm\u011b odstranit a nahradit jej\u00edm obsahem. T\u00edmto p\u0159\u00edkladem jsem cht\u011bl p\u0159edstavit obecn\u00fd princip. <strong>vzor refaktoringu<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Souhrn<\/h2>\n\n\n\n<p><strong>Refaktoring<\/strong> je velk\u00fdm t\u00e9matem. Douf\u00e1m, \u017ee tento \u010dl\u00e1nek byl podn\u011btem k dal\u0161\u00edmu \u010dten\u00ed. Tyto str\u00e1nky <strong>dovednosti refaktoringu<\/strong> v\u00e1m pom\u016f\u017ee zachytit chyby a zlep\u0161it va\u0161i d\u00edlnu \u010dist\u00e9ho k\u00f3du. Doporu\u010duji p\u0159e\u010d\u00edst si Martinovu knihu (Improving the Design of Existing Code), kter\u00e1 je docela z\u00e1kladn\u00edm a u\u017eite\u010dn\u00fdm souborem pravidel <strong>refaktoring<\/strong>. Autor ukazuje r\u016fzn\u00e9 transformace krok za krokem s \u00fapln\u00fdm vysv\u011btlen\u00edm a motivac\u00ed a radami, jak se vyhnout chyb\u00e1m p\u0159i transformaci. <strong>refaktoring<\/strong>. D\u00edky sv\u00e9 v\u0161estrannosti je to \u00fa\u017easn\u00e1 kniha pro frontend a <strong>v\u00fdvoj\u00e1\u0159i backendu<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/thecodest.co\/careers\"><img decoding=\"async\" src=\"\/app\/uploads\/2024\/05\/ruby_developer_hiring.jpeg\" alt=\"Sta\u0148te se juniorn\u00edm v\u00fdvoj\u00e1\u0159em Ruby\"\/><\/a><\/figure>\n\n\n\n<p><strong>P\u0159e\u010dt\u011bte si v\u00edce<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/thecodest.co\/blog\/graphql-ruby-what-about-performance\">GraphQL Ruby. Jak je to s v\u00fdkonem?<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/thecodest.co\/blog\/rails-and-other-means-of-transport\">Kolejnice a dal\u0161\u00ed dopravn\u00ed prost\u0159edky<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/thecodest.co\/blog\/rails-development-with-tmux-vim-fzf-ripgrep\">V\u00fdvoj Rails pomoc\u00ed TMUX, Vim, Fzf + Ripgrep<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Mo\u017en\u00e1 p\u00ed\u0161u o n\u011b\u010dem, co je pro mnoh\u00e9 z\u0159ejm\u00e9, ale mo\u017en\u00e1 ne pro v\u0161echny. Refaktoring je podle m\u011b slo\u017eit\u00e9 t\u00e9ma, proto\u017ee zahrnuje zm\u011bnu k\u00f3du bez vlivu na jeho fungov\u00e1n\u00ed.<\/p>","protected":false},"author":2,"featured_media":3017,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-3016","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-development"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>A Quick Primer on Refactoring for Beginners - The Codest<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thecodest.co\/cs\/blog\/strucny-uvod-do-refaktoringu-pro-zacatecniky\/\" \/>\n<meta property=\"og:locale\" content=\"cs_CZ\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Quick Primer on Refactoring for Beginners\" \/>\n<meta property=\"og:description\" content=\"Perhaps I am writing about something obvious to many, but maybe not to everyone. Refactoring is, I think, a complicated topic because it involves changing the code without affecting its operation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thecodest.co\/cs\/blog\/strucny-uvod-do-refaktoringu-pro-zacatecniky\/\" \/>\n<meta property=\"og:site_name\" content=\"The Codest\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-24T08:53:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-24T11:30:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/regactor.png\" \/>\n\t<meta property=\"og:image:width\" content=\"960\" \/>\n\t<meta property=\"og:image:height\" content=\"540\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"thecodest\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"thecodest\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minut\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/\"},\"author\":{\"name\":\"thecodest\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/person\\\/7e3fe41dfa4f4e41a7baad4c6e0d4f76\"},\"headline\":\"A Quick Primer on Refactoring for Beginners\",\"datePublished\":\"2020-06-24T08:53:38+00:00\",\"dateModified\":\"2026-04-24T11:30:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/\"},\"wordCount\":1338,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/regactor.png\",\"articleSection\":[\"Software Development\"],\"inLanguage\":\"cs\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/\",\"name\":\"A Quick Primer on Refactoring for Beginners - The Codest\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/regactor.png\",\"datePublished\":\"2020-06-24T08:53:38+00:00\",\"dateModified\":\"2026-04-24T11:30:09+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/#breadcrumb\"},\"inLanguage\":\"cs\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"cs\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/#primaryimage\",\"url\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/regactor.png\",\"contentUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/regactor.png\",\"width\":960,\"height\":540},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/a-quick-primer-on-refactoring-for-beginners\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thecodest.co\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A Quick Primer on Refactoring for Beginners\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"name\":\"The Codest\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thecodest.co\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"cs\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"cs\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/03\\\/thecodest-logo.svg\",\"contentUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/03\\\/thecodest-logo.svg\",\"width\":144,\"height\":36,\"caption\":\"The Codest\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/pl.linkedin.com\\\/company\\\/codest\",\"https:\\\/\\\/clutch.co\\\/profile\\\/codest\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/person\\\/7e3fe41dfa4f4e41a7baad4c6e0d4f76\",\"name\":\"thecodest\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"cs\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"caption\":\"thecodest\"},\"url\":\"https:\\\/\\\/thecodest.co\\\/cs\\\/author\\\/thecodest\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Rychl\u00fd z\u00e1klad refaktoringu pro za\u010d\u00e1te\u010dn\u00edky - The Codest","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/thecodest.co\/cs\/blog\/strucny-uvod-do-refaktoringu-pro-zacatecniky\/","og_locale":"cs_CZ","og_type":"article","og_title":"A Quick Primer on Refactoring for Beginners","og_description":"Perhaps I am writing about something obvious to many, but maybe not to everyone. Refactoring is, I think, a complicated topic because it involves changing the code without affecting its operation.","og_url":"https:\/\/thecodest.co\/cs\/blog\/strucny-uvod-do-refaktoringu-pro-zacatecniky\/","og_site_name":"The Codest","article_published_time":"2020-06-24T08:53:38+00:00","article_modified_time":"2026-04-24T11:30:09+00:00","og_image":[{"width":960,"height":540,"url":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/regactor.png","type":"image\/png"}],"author":"thecodest","twitter_card":"summary_large_image","twitter_misc":{"Written by":"thecodest","Est. reading time":"7 minut"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/#article","isPartOf":{"@id":"https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/"},"author":{"name":"thecodest","@id":"https:\/\/thecodest.co\/#\/schema\/person\/7e3fe41dfa4f4e41a7baad4c6e0d4f76"},"headline":"A Quick Primer on Refactoring for Beginners","datePublished":"2020-06-24T08:53:38+00:00","dateModified":"2026-04-24T11:30:09+00:00","mainEntityOfPage":{"@id":"https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/"},"wordCount":1338,"commentCount":0,"publisher":{"@id":"https:\/\/thecodest.co\/#organization"},"image":{"@id":"https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/#primaryimage"},"thumbnailUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/regactor.png","articleSection":["Software Development"],"inLanguage":"cs","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/","url":"https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/","name":"Rychl\u00fd z\u00e1klad refaktoringu pro za\u010d\u00e1te\u010dn\u00edky - The Codest","isPartOf":{"@id":"https:\/\/thecodest.co\/#website"},"primaryImageOfPage":{"@id":"https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/#primaryimage"},"image":{"@id":"https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/#primaryimage"},"thumbnailUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/regactor.png","datePublished":"2020-06-24T08:53:38+00:00","dateModified":"2026-04-24T11:30:09+00:00","breadcrumb":{"@id":"https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/#breadcrumb"},"inLanguage":"cs","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/"]}]},{"@type":"ImageObject","inLanguage":"cs","@id":"https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/#primaryimage","url":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/regactor.png","contentUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/regactor.png","width":960,"height":540},{"@type":"BreadcrumbList","@id":"https:\/\/thecodest.co\/blog\/a-quick-primer-on-refactoring-for-beginners\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thecodest.co\/"},{"@type":"ListItem","position":2,"name":"A Quick Primer on Refactoring for Beginners"}]},{"@type":"WebSite","@id":"https:\/\/thecodest.co\/#website","url":"https:\/\/thecodest.co\/","name":"The Codest","description":"","publisher":{"@id":"https:\/\/thecodest.co\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thecodest.co\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"cs"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"cs","@id":"https:\/\/thecodest.co\/#\/schema\/logo\/image\/","url":"https:\/\/thecodest.co\/app\/uploads\/2024\/03\/thecodest-logo.svg","contentUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/03\/thecodest-logo.svg","width":144,"height":36,"caption":"The Codest"},"image":{"@id":"https:\/\/thecodest.co\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/pl.linkedin.com\/company\/codest","https:\/\/clutch.co\/profile\/codest"]},{"@type":"Person","@id":"https:\/\/thecodest.co\/#\/schema\/person\/7e3fe41dfa4f4e41a7baad4c6e0d4f76","name":"thecodest","image":{"@type":"ImageObject","inLanguage":"cs","@id":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","caption":"thecodest"},"url":"https:\/\/thecodest.co\/cs\/author\/thecodest\/"}]}},"_links":{"self":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts\/3016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/comments?post=3016"}],"version-history":[{"count":12,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts\/3016\/revisions"}],"predecessor-version":[{"id":7707,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts\/3016\/revisions\/7707"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/media\/3017"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/media?parent=3016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/categories?post=3016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/tags?post=3016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}