Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. bulletproof code best practices

bulletproof code best practices

Scheduled Pinned Locked Moved Visual Basic
databasehelpquestioncareer
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DaveC426913
    wrote on last edited by
    #1

    I'm updating a database and want to write well-formed code. The user attempts to add an item to the db. Items must be unique by name; the item already exists. I check this in my code. Question: Which is better: 1] My AddNewItem routine first checks to see if the proposed item exists. Only if it does not will it call an AddItem routine 2] My AddNewItem routine simply calls an AttemptAddNewItem routine. It is that routine's job to handle the checking. i.e. (pseudo-code only): 1]

    sub AddNewItem
    if !CheckforExisting() then
    AddItem()

    else
    'warn user
    end if
    end sub

    sub AddItem
    'adds new record to db

    end function

    function CheckForExisting
    ' checks for existing item
    end function

    2]

    sub AddNewItem
    AddItem()
    end sub

    sub AttemptAddItem
    if !CheckforExisting() then
    'adds new record to db

    else
    'warn user
    end if
    end function

    function CheckForExisting
    ' checks for existing item
    end function

    ________________________________________________________________________ Dave Y10K bug! Let's not get caught with our pants down **AGAIN**! (DC 02002)

    C 1 Reply Last reply
    0
    • D DaveC426913

      I'm updating a database and want to write well-formed code. The user attempts to add an item to the db. Items must be unique by name; the item already exists. I check this in my code. Question: Which is better: 1] My AddNewItem routine first checks to see if the proposed item exists. Only if it does not will it call an AddItem routine 2] My AddNewItem routine simply calls an AttemptAddNewItem routine. It is that routine's job to handle the checking. i.e. (pseudo-code only): 1]

      sub AddNewItem
      if !CheckforExisting() then
      AddItem()

      else
      'warn user
      end if
      end sub

      sub AddItem
      'adds new record to db

      end function

      function CheckForExisting
      ' checks for existing item
      end function

      2]

      sub AddNewItem
      AddItem()
      end sub

      sub AttemptAddItem
      if !CheckforExisting() then
      'adds new record to db

      else
      'warn user
      end if
      end function

      function CheckForExisting
      ' checks for existing item
      end function

      ________________________________________________________________________ Dave Y10K bug! Let's not get caught with our pants down **AGAIN**! (DC 02002)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      It depends what you want to do - if someone tries to add an item that has the same unique identifier, do you want to treat it as editing the current item, or ignore it ? What sort of load are you expecting this program to have ? That is, how many database calls a second do you expect ? If this were a web app, I'd have a return code from the stored proc I called to insert tell me if the item already existed, so I only made one round trip to the database, then I'd deal with telling the user based on that code. Christian Graus - Microsoft MVP - C++

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups