Jump to content

Recommended Posts

I am wondering if my scripts are too long?

 

A lot of Object-Oriented Programmers - which doesn't describe me - would say that if you have more than 20-30 lines of code in a class then it is too much.

 

Whether using OOP or traditional Procedural Code, do you agree or disagree with this?

 

Currently my Log-In script is like 300 lines, including several queries and nested IF-THEN-ELSE's...

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/256399-is-my-code-too-big/
Share on other sites

A lot of Object-Oriented Programmers - which doesn't describe me - would say that if you have more than 20-30 lines of code in a class then it is too much.

 

I've heard some people say something along the lines of if you have more lines than fit on your screen in a function, it is too big.  For me this would be around 60 lines.  I do not necessarily agree with this, but there are advantages to attempting to keep the line count down.  It's easier to debug things if you can see the whole body of a function at once rather than having to constantly scroll.  Some times there is just nothing you can do though.  I have a few functions where the SQL query itself consists of 100+ lines, then there is the code to process it on top of that.

 

A whole class or script though will likely have many lines.  If you feel like you have too much stuff in your script or function, break it out into other functions or include files at logical places.  If you feel it is ok, then leave it as is.  PHP doesn't care one way or the other, it's all about what you find to be the easiest to read, understand, and maintain.

 

For example I had a function that registered users for courses in one script.  The process of doing that involved creating an invoice, processing payment, and adding the course record to the db.  All three steps together ended up being a lot of code so I broke it out into three separate functions.  It makes the code more readable, and also cuts down on the indentation level so the code is closer to left of the screen.

 

Currently my Log-In script is like 300 lines, including several queries and nested IF-THEN-ELSE's...

I have some scripts that have several thousand lines of code in them, and that's not even counting all the support files that have been include()'ed into it.  Sometimes it just takes a lot of code to do what you need to do.

 

The main thing if you want to try and keep the line count down is to just watch out to make sure your not repeating the same code over and over.  If you are, turn it into a function or a loop so it can be easily reused.  There are also ways you can restructure things sometimes to use less lines, such as using the ternary operator for short if/else conditions.

 

Learning these things mainly just comes down to time and experience.  Look at other peoples code as well as yours and get ideas from that.  When I was just starting out I spent a lot of time browsing help forums such as this one.  In the early days I did not participate much but I learned a lot just from reading other peoples problems and the solutions they were given.  Even still I occasionally learn something new from reading other responses here.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/256399-is-my-code-too-big/#findComment-1314474
Share on other sites

A lot of Object-Oriented Programmers - which doesn't describe me - would say that if you have more than 20-30 lines of code in a class then it is too much.

 

No they wouldn't. In a method maybe, but not a class.

 

Well, I have heard them say that.

 

So what number do you think is too big in a traditional Procedural Script?

 

So what number do you think is too big in a Class/Object?

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/256399-is-my-code-too-big/#findComment-1314645
Share on other sites

A lot of Object-Oriented Programmers - which doesn't describe me - would say that if you have more than 20-30 lines of code in a class then it is too much.

 

No they wouldn't. In a method maybe, but not a class.

 

Well, I have heard them say that.

 

So what number do you think is too big in a traditional Procedural Script?

 

So what number do you think is too big in a Class/Object?

 

 

Debbie

 

 

There is no magical number.

 

If your script is long because it is inefficient or poorly written, then it should be cleaned up. If your script is long because it has to be, then so be it. If your classes or methods are taking on too much responsibility (doing too many different things) then break them down and make them only responsible for a specific task.

 

For comparison, my user auth class is 411 lines and the login method is 43 lines.

Link to comment
https://forums.phpfreaks.com/topic/256399-is-my-code-too-big/#findComment-1314660
Share on other sites

A lot of Object-Oriented Programmers - which doesn't describe me - would say that if you have more than 20-30 lines of code in a class then it is too much.

 

No they wouldn't. In a method maybe, but not a class.

 

Well, I have heard them say that.

 

If that's true, they're idiots.

 

So what number do you think is too big in a traditional Procedural Script?

 

So what number do you think is too big in a Class/Object?

 

There's no set "X lines or above means you're doing it wrong" guideline.  It all depends on what you're trying to do.

 

A better gauge is to keep track of repeated code.  Good code is clear, concise, and doesn't repeat itself.  If you find yourself repeating chunks of code, that's a clear sign that you need to refactor what you have.

Link to comment
https://forums.phpfreaks.com/topic/256399-is-my-code-too-big/#findComment-1314662
Share on other sites

If that's true, they're idiots.

 

They were pretty expert.

 

I think it just means they liked breaking things into really atomic components.

 

Think Rim, Spokes, Hub, Bearing, Value Stem, Tube, Tire versus Bicycle Wheel...

 

 

So what number do you think is too big in a traditional Procedural Script?

 

So what number do you think is too big in a Class/Object?

 

There's no set "X lines or above means you're doing it wrong" guideline.  It all depends on what you're trying to do.

 

A better gauge is to keep track of repeated code.  Good code is clear, concise, and doesn't repeat itself.  If you find yourself repeating chunks of code, that's a clear sign that you need to refactor what you have.

 

Is it possible to build a fairl robust website staying with old-school Procedural Code?

 

Would I be wise to start breaking things up into tons of Functions for re-useability, or is that better saved for OOP?

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/256399-is-my-code-too-big/#findComment-1314679
Share on other sites

Is it possible to build a fairl robust website staying with old-school Procedural Code?

 

Sure. Wordpress for example is procedural.  If the code is well organized and design a procedural approach can be just as effective as an OOP approach.

 

Link to comment
https://forums.phpfreaks.com/topic/256399-is-my-code-too-big/#findComment-1314698
Share on other sites

If that's true, they're idiots.

 

They were pretty expert.

 

I think it just means they liked breaking things into really atomic components.

 

Think Rim, Spokes, Hub, Bearing, Value Stem, Tube, Tire versus Bicycle Wheel...

 

Which is only useful if you need to be able to swap those incredibly granular parts at runtime.  If you don't need to touch them in an individual manner, you're just wasting time.

Link to comment
https://forums.phpfreaks.com/topic/256399-is-my-code-too-big/#findComment-1314776
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.