
Principles of Effective Code Design and Best Practices" (66 characters)
"Learn about the principles of straight-line code, grouping related items, and making design decisions in programming. Explore code examples and best practices to improve your design skills." (224 characters)
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Principles of straight-line code Make dependences obvious: (e.g., through passing arguments, return values) firstResult = doThing1(); secondResult = doThingY(firstResult); Vs. doThing1(); doThingY(); 2
Grouping related items (example) Ordering implicit, but emphasizes grouping MarketingData marketingData = new MarketingData(); marketingData.ComputeQuarterly(); marketingData.ComputeAnnual(); marketingData.Print(); SalesData salesData = new SalesData(); salesData.ComputeQuarterly(); salesData.ComputeAnnual(); salesData.Print(); 3
Which is better? This code takes an array of strings, it processes all of the strings before a dash one way and all of the remaining strings another way. Assume there is only one dash in the array of strings. boolean dashFound = false; for (String arg : args) { if (arg.equals("-")) { dashFound = true; } else if (!dashFound) { process1(arg); } else { process2(arg); } } A int i = 0; while(i < args.length && !args[i].equals("-")) { process1(args[i]); i++; } B i++; // skip the dash for( ; i < args.length ; i++) { process2(args[i]); } C D Control flow is fine for both Control flow is problematic for both 4
Which is better? public int[] copyIntArray(int[] input) { int [] copy = new int[input.length]; int i = 0; for (int value: input) { copy[i++] = value; } return copy; } A public int[] copyIntArray(int[] input) { int [] copy = new int[input.length]; for (int i = 0; i < input.length; i++) { copy[i] = input[i]; } return copy; } B C D Control flow is fine for both Control flow is problematic for both 5
Which is best? A if (getAmountOfGasInTank() >= gasNeeded(destination)) { // avoid unnecessary stops; reduce wear on engine } else { fillGasTank(); } if (getAmountOfGasInTank() < gasNeeded(destination)) { fillGasTank(); } else { // avoid unnecessary stops; reduce wear on engine } if (gasNeeded(destination) < getAmountOfGasInTank()) { // avoid unnecessary stops; reduce wear on engine } else { fillGasTank(); } if (gasNeeded(destination) >= getAmountOfGasInTank()) { fillGasTank(); } else { // avoid unnecessary stops; reduce wear on engine } B C D 6
Design is hard Design is an art, not a science Large/infinite design space, not enumerable Requirements, constraints, trade-offs, and priorities You get better with practice / experience / seeing good design / hearing critiques of others designs 7
Virtues of a good design (software) Manages complexity Loose coupling Reusability Ease of maintenance Standard techniques Extensibility High Fan-in Low-to-medium Fan-out 8
Good Design Manages Complexity Seven plus or minus two (Miller s Law) The goal of all software-design techniques Break complicated problems into simple problems Separation of concerns Focus on one at a time 9
Keep Coupling Loose small interfaces (few methods, few arguments/method) obvious (interactions through parameter passing) flexible 10
Abstract Data Types Define a class based around conceptual structures Encapsulation / information-hiding Make interfaces more informative (self-documenting) Easier to reason about correctness Treat even simple items as ADTs Good for extensibility 11
Inheritance can provides 2 things Shared interface: Public methods Ensure methods have consistent meaning in derived class Liskov Substitution Principle Shared implementation Code in shared super class, not replicated in each derived Could be private data/methods 12
hasA vs. isA relationship Which is a candidate for inheritance? A) hasA relationship B) isA relationship C) both hasA and isA relationships D) neither hasA and isA relationships 13
Inheritance vs. Interfaces Inheritance should be a isA relationship Interfaces are for capabilities ( mixin s) 14
Designing Good Interfaces Sufficiently Expressive General Minimal 15