SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2010
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is it best/standard practise to prefix vars with letters indicating datatype?

    My supervisors' code features variable names with letters prefixed to them to indicate the variable type i.e. $iCount (for an integer for use as a counter) or $aDeck (array of results from db query about a deck of cards in a gaming application etc). I was told this is standard universal coding practice. is it?

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    No. Search "Hungarian Notation" if you want to bore yourself to sleep.
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I think Hungarian Notation is a stupid practice in a dynamically typed language. A variable can change its type as easily as it can change its value. And adding a bunch of variables can be a lot more confusing then reusing the same variable for the same subject. Thats my opinion anyways.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    It's not a practice I use, though I have seen it used occasionally in VBScript.

    IMHO, it's a poor practice to use in PHP because PHPdoesn't use strict typing (like logic_earth said), and because mistakes can arise if a variable's type changes and the name no longer properly represents what it is.

    It's an approach to correct the lack of strict typing in PHP...which doesn't usually end up working so well.

    If you name a variable descriptively enough, you can always tell what kind of data is is supposed to store.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,815
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    That notation only makes sense in languages that use strict typing and where the programmer is expected to have a bad memory anyd be unable to remember to look back a half dozen lines to where the variable was defined.

    $iCount = 0;
    if (is_numeric($iCount)) $iCount = 'nonsense'; // completely valid in PHP
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •