Never start coding unless you understand the task - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Never start coding unless you understand the task

Description:

Analyze requirements to make sure that they are consistent and complete. ... Do you make an assumption of max string length and allocate fixed-size string ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 31
Provided by: maxifom
Learn more at: https://www.cse.psu.edu
Category:

less

Transcript and Presenter's Notes

Title: Never start coding unless you understand the task


1
Problem Solving
  • Never start coding unless you understand the
    task!
  • Gather requirements first. This means identify
    the problem and ask questions about it. Now you
    kind of know what to do.
  • Analyze requirements to make sure that they are
    consistent and complete. Now you know that
    whatever you are going to do is doable.
  • Produce technical specification (i.e. express the
    requirements in technical terms). Now you know
    exactly what to do.
  • Think of what steps you need to take solve the
    problem and express them verbally to obtain a
    high-level process description. Now you have a
    process that you can code!
  • Start coding.

2
Process Analysis
  • Split complex task in a series of simple steps.
  • Identify entities the problem deals with. These
    will be your classes.
  • Identify what data characterizes your entities.
    These will be your class properties.
  • Identify what actions your entities can
    perform. These will be your class methods.
  • Find repeated actions and generalize them.
    These will be your utility functions or utility
    classes. You will reuse them.
  • Dont make any assumptions check user input
    and make provisions for worst cases.

3
Email Validation Task
  • Problem Determine if the email address is valid.
  • Requirement analysis What rules can we define to
    check email address against? What is email
    address comprised of?
  • max.fomitchev_at_cse.psu.edu
  • Local Part _at_ domain
  • subdomain1.subdomain2.tld

4
Email Validation Rules
  • Core Rules
  • Single _at_ character
  • Non-empty Local Part
  • Non-empty domain
  • Non-empty subdomain(s)
  • Non-empty TLD
  • Local Part must not end with .
  • Additional Rules
  • 7) 1 lt TLD length lt 7
  • 8) Local Part length lt 65
  • 9) Domain Length lt 256
  • 11) Subdomain(s) must not begin with - or end
    with -
  • 12) Check for invalid characters \\, , lt,
    gt, ), (, , , , ,, ,
  • 13) Domain cannot contain two - in a row
  • 14) Check TLD against the list of ICANN domains

5
Optimized Rules / Technical Spec
  • General
  • There has to be LocalPart and Domain separated by
    _at_
  • Must not contain general illegal characters
    (\\, , lt, gt, ), (, , , ,
    ,, , )
  • LocalPart
  • 0 lt Length lt 65
  • Must not end with .
  • Domain
  • 0 lt Length lt 256
  • There has to be at least one subdomain and a TLD
    separated by .
  • Must not contain _at_
  • All subdomains must be valid
  • TLD must be valid
  • Subdomain
  • 0 lt Length lt 64
  • Must not begin or end with -

6
Design Choice Buffer Allocation
  • Since you do not know sizes of strings you deal
    with
  • Do you make an assumption of max string length
    and allocate fixed-size string buffers on
    stack?simpler faster robust -limited
  • Or do you allocate memory on heap (e.g. using the
    new operator)?-harder -slower -err.prone
    universal

7
Choice A Fixed Buffers
  • // Our Assumptions
  • define MAX_LOCAL_PART 65
  • define MAX_DOMAIN 256
  • define MAX_SUBDOMAIN 64
  • define MAX_TLD 7
  • define MAX_SUBDOMAINS 10
  • // Buffers
  • char LocalPartMAX_LOCAL_PART
  • char DomainMAX_DOMAIN
  • char TLDMAX_TLD
  • char SubdomainMAX_SUBDOMAINSMAX_SUBDOMAIN

8
Top-Level Class Email
  • Email this is the one we validate
  • Contains Address string (property)
  • Contains LocalPart (property)
  • Contains Domain (property)
  • Can be parsed into LocalPart and Domain pieces
    (method)
  • Can be validated (method)

9
Email Class
  • class Email
  • public
  • Email(char address)
  • public
  • String Address
  • EmailLocalPart LocalPart
  • EmailDomain Domain
  • public
  • bool Parse()
  • bool IsValid()

10
Other Classes
  • LocalPart
  • Contains UserName string (property)
  • Can be validated (method)
  • Domain
  • Contains Name string (property)
  • Contains an array of Subdomains (property)
  • Contains TLD (property)
  • Can be parsed (method)
  • Can be validated (method)
  • Subdomain
  • Contains Name string (property)
  • Can be validated (method)
  • TLD (closely similar to Subdomain but somewhat
    different)
  • Contains Name string (property)
  • Can be validated (method)

11
EmailLocalPart Class
  • class EmailLocalPart
  • public
  • EmailLocalPart()
  • public
  • String UserName
  • public
  • bool IsValid()
  • private
  • char UserNameBufferMAX_LOCAL_PART

12
EmailDomain Class
  • class EmailDomain
  • public
  • EmailDomain()
  • SubdomainCount 0
  • public
  • String Name
  • EmailSubdomain SubdomainMAX_SUBDOMAINS
  • int SubdomainCount
  • EmailTld Tld
  • public
  • bool Parse()
  • bool IsValid()
  • private

13
EmailSubdomain Class
  • class EmailSubdomain
  • public
  • EmailSubdomain()
  • public
  • String Name
  • public
  • virtual bool IsValid() // Virtual means
    that the method
  • // can be
    replaced in a derived
  • // class
  • private
  • char NameBufferMAX_DOMAIN

14
EmailTld Class
  • // Subdomain and Tld are identical, except for
  • // slight differences in validation rules, so to
    avoid repetition derive EmailTld class from
    EmailSubdomain base class
  • class EmailTld public EmailSubdomain // EmailTld
    class is derived from

  • // EmailSubdomain class
  • public
  • EmailTld()
  • / public
  • String Name / // Name is inherited from
    Subdomain
  • // therefore no
    need to declare again
  • public
  • virtual bool IsValid() // Inherited, but
    we are going to
  • //
    replace it since TLD validation
  • // is
    different

15
Email Checker Solution Process
  • Separate email into LocalPart and Domain
  • Separate Domain into Subdomain array and TLD
  • Apply part validation rules to see if any of them
    are violated.

16
Separate Local Part Domain
  • Determine email Length
  • Determine AtPosition the position of _at_
    character in email
  • Copy characters 0AtPosition to LocalPart
  • Copy characters AtPosition1Length to Domain

17
Proceed from Abstract to Concrete
  • Pretend that you have already solved concrete
    tasks (such as copying characters, finding
    character index, etc.) with the help of String
    utility class and proceed to construct your
    abstract solution by referring to String and
    other concrete classes that you have not yet
    implemented.

18
Implementation
  • (!) Find repeated tasks that can be generalized
    and streamlined, e.g.
  • Determining string length (i.e. return the
    length of this string)
  • Copying a range of characters from one string
    into another (i.e. copy length characters from
    this string starting at startPosition into
    destination
  • Finding character index (find aChar in this
    string starting at startPosition)
  • The above (a,b,c) deal with character strings. So
    lets pretend that we have a String class that
    can do all of the above.

19
String Class
  • // String utility class
  • class String
  • public
  • String() // Default constructor (builds
    uninitialized String)
  • Buffer NULL
  • String(char buffer) // String encapsulates
    char-array pointer
  • Buffer buffer
  • public
  • int GetLength()
  • int IndexOf(char aChar, int startPosition)
    // !! Returns -1 when aChar is not found
  • void CopyTo(int startPosition, int length,
    String destination)
  • bool IsValid() // Sanity check

20
String Class Encapsulates char
  • String class encapsulates char pointer.
    Therefore a String must be initialized with a
    valid char pointer!
  • char nameBuffer100
  • String name(nameBuffer)

21
Where Do We Start?
  • We always start with top-level class.
  • So lets pretend that Email class is done and the
    email validation works and we can use it, e.g.
  • Email email(max_at_adelphia.net)
  • if ( email.IsValid() )
  • cout ltlt Email is valid
  • else
  • cout ltlt Email is not valid

22
Where Do We Go Next?
  • We start filling in blanks from the constructor
    of the top-level class.
  • In the constructor we can refer to other classes
    we invented even though they are not yet
    implemented.

23
Constructor EmailEmail()
  • Just store the pointer to email address in
    internal property
  • EmailEmail(char address)
  • // Here we initialize Emails internal
  • // Address property of type String
  • Address String(address)

24
bool EmailIsValid()
  • Now, since we already know what amounts to valid
    email we can express EmailIsValid() concisely
    as follows
  • bool EmailIsValid()
  • // Separate email into LocalPart and Domain
  • return
  • Parse() // Was parsing OK?
  • LocalPart.IsValid() // LocalPart OK?
  • Domain.IsValid() // Domain OK?

25
Void EmailParse()
  • And we know how to parse EmailAddress into
    LocalPart and Domain
  • bool EmailParse()
  • // Find _at_ character
  • int atPosition Address.IndexOf(_at_, 0)
  • if ( atPosition -1 )
  • return false // Shute, _at_ not found!
    Therefore cannot parse.
  • // Determine email length
  • int length Address.GetLength()
  • // Copy email parts in appropriate buffers
  • Address.CopyTo(0, atPosition,
    LocalPart.UserName)
  • Address.CopyTo(atPosition 1, Length -
    atPosition - 1, Domain.Name)
  • return true

26
bool EmailLocalPartIsValid()
  • And we know what rules we need to check to make
    sure that LocalPart is valid
  • bool EmailLocalPartIsValid()
  • // We do not assume anything, so check
    UserName string
  • if ( !UserName.IsValid() )
  • return false
  • int length UserName.GetLength()
  • // Rule Check length between 1 and 64
  • if ( length lt 0 length gt 64 )
  • return false
  • else
  • return true

27
bool EmailDomainIsValid()
  • And we know what rules we need to check to make
    sure that Domain is valid
  • bool EmailDomainIsValid()
  • // Extract subdomains and TLD
  • if ( !Parse() )
  • return false
  • // Check subdomains
  • for ( int i 0 i lt SubdomainsCount i )
  • if ( !Subdomaini.IsValid() )
  • return false
  • // Check TLD
  • if ( TLD.IsValid() )
  • return true
  • else
  • return false

28
bool EmailDomainParse()
  • And we know what rules we need to check to make
    sure that Domain is valid
  • bool EmailDomainParse()
  • // We do not assume anything, so check
    UserName string
  • if ( !Name.IsValid() )
  • return false
  • // Contains at least one .
  • if ( Name.IndexOf(., 0) -1 )
  • return false
  • // Reset SubdomainCount
  • SubdomainCount 0
  • // Extract subdomains
  • int startPosition 0
  • do
  • int nextPosition Name.IndexOf(.,
    StartPosition)

29
bool EmailTldIsValid()
  • TLD is a domain, since all domain rule checking
    applies with an addition of one more TLD-specific
    rule TLD cannot contain a -
  • // Constructor does nothing special, so we simply
    call
  • // the base class constructor
  • EmailTldEmailTld() EmailSubdomain()
  • bool EmailTldIsValid()
  • return EmailSubdomainIsValid()
  • Name.IndexOf(-, 0) -1

30
And So On and So Forth
  • Just keep filling in the blanks and your program
    will be done!
  • In the process you will likely modify your class
    definitions to accommodate your programming needs
    as you discover new things.
  • Class design / programming is an iterative
    approach. Thus changing and going back and forth
    is inevitable and normal.
Write a Comment
User Comments (0)
About PowerShow.com