The document discusses principles and best practices for clean code and defensive programming. For clean code, it recommends using intention-revealing names, organizing methods logically, refactoring complex conditional expressions, avoiding Booleans and using enums instead, and writing comments that explain why rather than what. For defensive programming, it advises validating all input data, protecting against invalid states, choosing consistent error handling, adding logging, and using exceptions judiciously. Examples show avoiding null values and returning exceptions instead of null.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
97 views
Software Engineering Principles & Best Practices
The document discusses principles and best practices for clean code and defensive programming. For clean code, it recommends using intention-revealing names, organizing methods logically, refactoring complex conditional expressions, avoiding Booleans and using enums instead, and writing comments that explain why rather than what. For defensive programming, it advises validating all input data, protecting against invalid states, choosing consistent error handling, adding logging, and using exceptions judiciously. Examples show avoiding null values and returning exceptions instead of null.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8
Software Engineering
Principles & Best Practices
Clean Code • Use intention revealing variable and method names. • Organize your class methods such that if A calls B, B is directly below A. • Change complicated if expressions into function call so that it’s easier to read. • If (( employee.flags & HOURLY_FAG) && (employee.age > 50) • If ( employee.isEligableForRetirement() ) • Avoid passing Booleans to functions. Create meaningful two value enums instead. • FileStream f = File.Open (“foo.txt”, true, false); • FileStream f = File.Open (“foo.txt”, CasingOptions.CaseSenstative, FileMode.Open); • Create “is” methods for your enums. • FileMode fileMode. • If ( fileMode.isOpen() ) • Write comments about why you are doing something over writing comments about what you are doing. Defensive Programming • Protect your code from invalid data • Validate all input data • Validate return values • Validate limits and bounds • Protected your code from invalid state conditions • Choose appropriate and consistent error handling strategy • Add diagnostic code – logging • Use exception handling only when you need to • Is NOT about swallowing errors or hiding bugs AVOID - Ignoring null or invalid arguments
public void addFriendToList( List<Friend> friends,
• We will never know when friends or newFriend is null!
PRACTICE - Rejecting invalid arguments
public void addFriendToList( List<Friend>
friends, Friend newFriend ) { NullPointerException is Objects.requireNonNull( friends ); thrown if friends is null Objects.requireNonNull( newFriend ); friends.add( newFriend ); } AVOID – Return values indicating success of failure Rule: A runway must be either departure or arrival public Runway getRunway( String runwayName ) { Runway runway = arrivalRunways.getRunway( runwayName ); if ( runway == null ) { runway = departureRunways.getRunway( runwayName ); if ( runway == null ) { Return value must be evaluated to determine return null; success/failure } } } return runway; } PRACTICE – Throwing exceptions for invalid states public Runway getRunway( String runwayName ) { Objects.requireNonNull( arrivalRunways ); Objects.requireNonNull( departureRunways ); Runway runway = arrivalRunways.getRunway( runwayName ); if ( runway == null ) { runway = departureRunways.getRunway( runwayName ); if ( runway == null ) { throw new IllegalArgumentException( "Runway " + runwayName + " does not exist" ); } } return runway; } PRACTICE – Throwing exceptions for invalidate states
public void start()
throws JMSException { log.debug( "Starting" ); Invalid state condition to call start() without first if ( isInitialized ) initializing { consumerLoop(); } else { throw new IllegalStateException("The consumer must be initialized before starting."); } }