We help IT Professionals succeed at work.

How to make this code clean / readable?

DrDamnit asked
Last Modified: 2015-09-16
The code below is part of a long line of if...elseif...else statment block where I am parsing a command as entered via a PHP CLI.

I hate the formatting. It follows the PSR, but it's still no fun to read.

Can anyone offer suggestions on how to clean this up so it is hyper-readable? Code runs perfectly, but it is hard to visually distguish where one set of code for a command ends and the next one starts.

$cmd = an object, which has methods for parsing and cleaning up commands.

Watch Question

Most Valuable Expert 2011
Author of the Year 2014
This one is on us!
(Get your first solution completely free - no credit card required)
Most Valuable Expert 2012


This shows my python side coming through where strings as objects are quite handy.

Do you have a link our example of an appropriate RESTful interface here?
Most Valuable Expert 2011
Author of the Year 2014

Yeah, Ruby on Rails treats everything as an object with its own sets of properties -- very handy.  But in PHP we have to call functions to discern the characteristics like strlen().

See if this helps:

Here is my (years old) take on APIs
Most Valuable Expert 2012


Somehow, I think we're talking past each other.

Based on your answers (and my understanding) an API is generally accessed over HTTP. We use SOAP (*shudder*) or REST as the structure / style.

But my question is related to cleaning up code that can be typed into a CLI, and which uses direct interactions with PHP and the database.

Once such CLI command might be:

users find byemail someone@somewhere.com.

This is processed by the Command class.

The "startsWith" is how I am figuring out what class of command has been given. Because these are all valid, but do different things:

users find byemail someone@somewhere.com
users find byfirst ray
users find bylast paseur

Thus, if it starts with "users find", I know I am going to output the same thing, but the only difference is the WHERE statement in the query that finds these users.

Based on your feedback, and reading about CRAP, I ended up refactoring a lot of the code into their own functions, which makes this read better and got rid of the nested if statements:

But I am just not making the connection on how a RESTful style API would be applicable or relevant. Am I missing something?
Most Valuable Expert 2012


For reference, this is the Command class. (Posted because I think it is causing some confusion. I am well aware that PHP requires strlen() to get a string length).

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.