We help IT Professionals succeed at work.

How to make this code clean / readable?

DrDamnit
DrDamnit asked
on
57 Views
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.

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

code
Comment
Watch Question

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

Author

Commented:
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

Commented:
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:
http://www.programmableweb.com/news/how-to-build-restful-apis-using-php-and-laravel/how-to/2014/08/13

Here is my (years old) take on APIs
https://www.experts-exchange.com/articles/12239/Introduction-to-Application-Programming-Interfaces.html
CERTIFIED EXPERT
Most Valuable Expert 2012

Author

Commented:
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:

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

Author

Commented:
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).
command.class.php.txt

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.

OR

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.