DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Code Aesthetics

Code Aesthetics

Jose Asuncion user avatar by
Jose Asuncion
·
May. 24, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
2.81K Views

Join the DZone community and get the full member experience.

Join For Free
  • This is my first post on Code Aesthetics, a new topic I am exploring. My objective is to come up with a catalog of coding styles that are easy to read. My belief is that there is a direct correlation between maintainability of code and the way it is written.

    This style is simple: if hard coding some values in an array, simply align them according to =>

    • Align by arrows – If initializing an array with one level, align the arrows according to =>.
      $result = array(
         'include_path' => array(),
         'ini'          => array(),
         'const'        => array(),
         'var'          => array(),
         'env'          => array(),
         'post'         => array(),
         'get'          => array(),
         'cookie'       => array(),
         'server'       => array(),
         'files'        => array(),
         'request'      => array()
      );
      
    Update: only do this if the way the array is instantiated is all the same like the example above. However if they are not such as the one below, I think it’s better to go easy on the spacey.

     

    if (in_array( $rCategory, array_keys($categories) ) ) {
       $category = $categories[$rCategory];
    else {
       $category = array(
          'name' => $rCategory,
          'articles' => array()		  
       );
    }	
    

     

    Seen on PHPUnit1, 2

  • Easy on the spacey – For nested arrays, I think it’s better to go easy on the indention spaces. This means not using the tab k
  • ey and instead just using the space bar. I find that the editors I use (vim and eclipse) seem to adapt just fine.

    Here’s a simple example from one of my projects:

     

    $this->assertEquals(array(
       'Sentosa' => array(
          'Beaches' => 1,
          'Rides'   => 2
        ),
        'Asia Malls' => array(
          'Tampines' => 3,
          'Hougang'  => 4
        )	
    ),  $walls );	
    

    A more complicated one:

    $this->assertEquals(array(
       'application_id' => 1,
       'articles' => array(
          array(
            'name' => 'Beaches',
    	'articles' => array(
    	   array(
              'content' => $this->_getContent(1)
              .
              .
              .
    

    Seen on PHPUnit 1

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Application Scalability — How To Do Efficient Scaling
  • What SREs Can Learn From the Atlassian Nightmare Outage of 2022
  • Maven Tutorial: Nice and Easy [Video]
  • Querying Kafka Topics Using Presto

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo