SlideShare uma empresa Scribd logo
1 de 13
Baixar para ler offline
ŠVARUS KODAS #1

REGIMANTAS SNIEŠKA
2014 02 06
PAVADINIMAI
public List<int[]> GetThem()
{
List<int[]> list1 = new List<int[]>();
foreach (int[] x in theList)
{
if (x[0] == 4)
list1.Add(x);
}
return list1;
}
PAVADINIMAI
Geras kodas:
public List<int[]> GetFlaggedCells()
{
List<int[]> flaggedCells = new List<int[]>();
foreach (int[] cell in gameBoard)
{
if (cell[STATUS_VALUE] == FLAGGED)
{
flaggedCells.Add(cell);
}
}
return flaggedCells;
}
PAVADINIMAI
O gal geriau ?

if (cell[STATUS_VALUE] == FLAGGED)
{
flaggedCells.Add(cell);
}
=>
if (cell.IsFlagged)
{
flaggedCells.Add(cell);
}
PAVADINIMAI
Naudokite aiškiai suprantamus pavadinimus – pavadinimai su
skaičiais neduoda jokios informacijos
public static void CopyChars(char[] a1, char[] a2)
{
for (int i = 0; i < a1.Length; i++)
{
a2[i] = a1[i];
}
}
a1 = source
a2 = destination
PAVADINIMAI
o Venkite “triukšmo“, kuris nieko nepasako:
• jokių Info, Data, artikelių the, a
• ObjectList -> Objects
• NameString -> Name
• CustomerObject -> Customer
o Naudokite ištariamus pavadinimus, ne trumpinius
o Pavadinimai turi būti lengvai ieškomi
o Vienos raidės pavadinimai tik lokalūs ir mažam scope
PAVADINIMAI
Klasės pavadinimas turi būti daiktavardis, pvz. Customer, Address
Metodai turi būti veiksmažodžiai, pvz. DeletePage, Save
Accessor‘iai, predicate‘ai turėtų būti su Get/Set/Is prefiksais
Išlaikykite nuoseklumą pavadinimuose – pvz. jei yra fetch, get,
retrieve žodžiai, kurie realiai reiškia tą patį, vertėtų pasirinkti vieną
jų
o Naudokite techninius žodžius, nusakančius objekto paskirtį –
AccountVisitor, JobQueue
o
o
o
o
METODAI
o Turi būti trumpi, geriausia iki ~20 eilučių
o Geriau ilgesnis ir informatyvus pavadinimas, negu trumpesnis ir
neaiškus
o Kodas viduje if/else/while iškeltas į atskirą metodą, su esmę
nusakančiu pavadinimu
o Atlikti vieną ir TIK vieną funkciją
o Jei įmanoma, vengti switch, keisti polimorfizmu
METODAI
public Money CalculatePay(Employee e)
{
switch (e.Type)
{
case EmployeeType.Hourly:
return CalculateHourlyPay(e);
break;
case EmployeeType.Salaried:
return CalculateSalariedPay(e);
break;
...
METODAI
public abstract class Employee
{
public abstract Money CalculatePay();
}
public class EmployeeFactory
{
public Employee GetEmployee(EmployeeType type)
{
switch (type)
{
case EmployeeType.Hourly:
return new HourlyEmployee();
...
public class HourlyEmployee : Employee
{ ... }
METODAI
o Atsiradus naujai funkcijai, pagal objekto tipą, nereikia daryti
daugiau switch
o Visa logika susijusi su tipu, laikoma vienoje vietoje
o Atsiradus naujam tipui, kuriame naują klasę, bet nekeičiame senų
(Open for extension, closed for modification)
METODAI
o Pavadinimai – nusakantys metodo esmę, paskirtį, pabandykite
keletą kartų pakeisti, gal kažkuris tiks labiau
o Perduodamų argumentų kiekiai
• Idealiu atveju 0
• 1 – gerai
• 2 – patenkinamai
• 3 – prastai
• 4+ - reikia pergalvoti sprendimą (gal naudoti container objektą?)
o Daug argumentų daro metodą sunkiai suvokiamu
o Mažai argumentų labai palengvina testavimą
METODAI
Nenaudokite output argumentų – jie sunkiai suprantami
Vietoj bool perdavimo geriau du atskiri metodai
Daryti tik tai, kas žadama, jokių paslėptų funkcijų metodo viduje
Vykdyti arba atsakyti į klausimą, bet ne abu iš karto
Vietoj custom error code, geriau mesti Exception, taip atsiskiria
„happy path“
o Try/catch viduje geriausia iškart kviesti metodus, kurių viduje yra
logika (SRP)
o Venkite besikartojančio kodo (!)
o
o
o
o
o

Mais conteúdo relacionado

Destaque

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Destaque (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

Clean code part 1

  • 1. ŠVARUS KODAS #1 REGIMANTAS SNIEŠKA 2014 02 06
  • 2. PAVADINIMAI public List<int[]> GetThem() { List<int[]> list1 = new List<int[]>(); foreach (int[] x in theList) { if (x[0] == 4) list1.Add(x); } return list1; }
  • 3. PAVADINIMAI Geras kodas: public List<int[]> GetFlaggedCells() { List<int[]> flaggedCells = new List<int[]>(); foreach (int[] cell in gameBoard) { if (cell[STATUS_VALUE] == FLAGGED) { flaggedCells.Add(cell); } } return flaggedCells; }
  • 4. PAVADINIMAI O gal geriau ? if (cell[STATUS_VALUE] == FLAGGED) { flaggedCells.Add(cell); } => if (cell.IsFlagged) { flaggedCells.Add(cell); }
  • 5. PAVADINIMAI Naudokite aiškiai suprantamus pavadinimus – pavadinimai su skaičiais neduoda jokios informacijos public static void CopyChars(char[] a1, char[] a2) { for (int i = 0; i < a1.Length; i++) { a2[i] = a1[i]; } } a1 = source a2 = destination
  • 6. PAVADINIMAI o Venkite “triukšmo“, kuris nieko nepasako: • jokių Info, Data, artikelių the, a • ObjectList -> Objects • NameString -> Name • CustomerObject -> Customer o Naudokite ištariamus pavadinimus, ne trumpinius o Pavadinimai turi būti lengvai ieškomi o Vienos raidės pavadinimai tik lokalūs ir mažam scope
  • 7. PAVADINIMAI Klasės pavadinimas turi būti daiktavardis, pvz. Customer, Address Metodai turi būti veiksmažodžiai, pvz. DeletePage, Save Accessor‘iai, predicate‘ai turėtų būti su Get/Set/Is prefiksais Išlaikykite nuoseklumą pavadinimuose – pvz. jei yra fetch, get, retrieve žodžiai, kurie realiai reiškia tą patį, vertėtų pasirinkti vieną jų o Naudokite techninius žodžius, nusakančius objekto paskirtį – AccountVisitor, JobQueue o o o o
  • 8. METODAI o Turi būti trumpi, geriausia iki ~20 eilučių o Geriau ilgesnis ir informatyvus pavadinimas, negu trumpesnis ir neaiškus o Kodas viduje if/else/while iškeltas į atskirą metodą, su esmę nusakančiu pavadinimu o Atlikti vieną ir TIK vieną funkciją o Jei įmanoma, vengti switch, keisti polimorfizmu
  • 9. METODAI public Money CalculatePay(Employee e) { switch (e.Type) { case EmployeeType.Hourly: return CalculateHourlyPay(e); break; case EmployeeType.Salaried: return CalculateSalariedPay(e); break; ...
  • 10. METODAI public abstract class Employee { public abstract Money CalculatePay(); } public class EmployeeFactory { public Employee GetEmployee(EmployeeType type) { switch (type) { case EmployeeType.Hourly: return new HourlyEmployee(); ... public class HourlyEmployee : Employee { ... }
  • 11. METODAI o Atsiradus naujai funkcijai, pagal objekto tipą, nereikia daryti daugiau switch o Visa logika susijusi su tipu, laikoma vienoje vietoje o Atsiradus naujam tipui, kuriame naują klasę, bet nekeičiame senų (Open for extension, closed for modification)
  • 12. METODAI o Pavadinimai – nusakantys metodo esmę, paskirtį, pabandykite keletą kartų pakeisti, gal kažkuris tiks labiau o Perduodamų argumentų kiekiai • Idealiu atveju 0 • 1 – gerai • 2 – patenkinamai • 3 – prastai • 4+ - reikia pergalvoti sprendimą (gal naudoti container objektą?) o Daug argumentų daro metodą sunkiai suvokiamu o Mažai argumentų labai palengvina testavimą
  • 13. METODAI Nenaudokite output argumentų – jie sunkiai suprantami Vietoj bool perdavimo geriau du atskiri metodai Daryti tik tai, kas žadama, jokių paslėptų funkcijų metodo viduje Vykdyti arba atsakyti į klausimą, bet ne abu iš karto Vietoj custom error code, geriau mesti Exception, taip atsiskiria „happy path“ o Try/catch viduje geriausia iškart kviesti metodus, kurių viduje yra logika (SRP) o Venkite besikartojančio kodo (!) o o o o o