Jump to content

Taking the leap to MVC


SaranacLake

Recommended Posts

After many, many years of struggling, I would like to finally learn how to separate my website's PRESENTATION from its LOGIC.

I have been working on a startup business for over a decade now.

And one of the reasons it is taking me so long to build my website from scratch is because I am a horribly INEFFICIENT PROGRAMMER!!

On one hand, I think I write solid business logic.  But considering that each of my scripts averages between 1,000-2,000 lines of code, that is a monster to manage.

(My website is over 50,000 lines of code at this point.  All home-grown and hand-coded.)

While it is hard to manage things while I am actively coding, what kills me even more, is when I get pulled away for months (or recently for 2 years), and then later on when I come back, it is a nightmare to load all of that code into my brain?!

I mean if you read any of my scripts starting at line 1 and going through line 1,500 I think it reads pretty well.  But who in the hell can keep track of all of that in one reading?  It's certainly a struggle for me!!

I definitely wantto strive for more of a "component archtecture" approach.

(On a side note, I have heard "good" Object-Oriented programmers claim that if a class is more than 100 lines of code then it is too long?!)

So anyways....

My #1 priority for v2.0 is to learn and master separating my website's PRESENTATION from its LOGIC using something like MVC.

(And after that, my goal is to finally learn and master Object-Oriented Programming, but one thing at a time!!)

I want to finally learn how to program like a *professional* coder.  (A very successful and RICH professional programmer!!!)  ?  

So, can all of you gurus out there, please help me get started with a solid Model-View-Controller introduction and/or resources and/or advice??

I know the Internet is littered with this topic, but like anything done well, you need to find good learning materials and at least one or two good mentors.

After all, if MVC and OOP and whatever were so easy, then everyone would be doing it, and doing it well.  (And of course you realize that very few people actually do things like MVC and OOP well!!)

So my goal is to become a "jedi", but I will need some good teachers to get there...

 

 

Link to comment
Share on other sites

That's great you want to learn new things. I would encourage anyone to always keep learning.

What I would suggest, instead of jumping into something completely new, especially based on your story, is to learn refactoring. There is probably quite a bit you can do with your current code base to refactor yourself towards an MVC/OOP application. I would recommend you put your entire project on GitHub so we can see what your working with and make recommendations. If you don't want the world to see the code you can make the repo private and PM access to those of us that would look it over.

I very highly recommend the book "PHP & MySQL: Novice to Ninja, 6th Edition" by Tom Butler & Kevin Yank. I would also recommend the book "Refactoring - Improving the Design of Existing Code" by Martin Fowler. By the time you learn what is in those two books you will indeed be a master "Jedi".

Link to comment
Share on other sites

18 hours ago, SaranacLake said:

(On a side note, I have heard "good" Object-Oriented programmers claim that if a class is more than 100 lines of code then it is too long?!)

That's stupid. Line limits are arbitrary. I mean, a basic PHP class without any code takes about 10 lines already.

A good class takes as many lines of code as it needs and no more.

18 hours ago, SaranacLake said:

My #1 priority for v2.0 is to learn and master separating my website's PRESENTATION from its LOGIC using something like MVC.

There's MVC the principle and MVC the architecture.

For the moment you want MVC the principle. It's about separating models (database and the code that handles data on its own right) from view (presentation and output) from controllers (logic primarily to connect models with views). It helps you keep code clean and prepares you for the next step.

MVC the architecture is about how your application executes. When your first script runs for a request it will execute the controller, which may interact with models or do form processing logic or some other actions, and from there execute the view. The controller is typically a class because that's what it is in basically every other language and MVC implementation out there, but because PHP is not strictly OOP your "controller" could be code at the beginning of a file and the "view" the code that comes after.

18 hours ago, SaranacLake said:

I want to finally learn how to program like a *professional* coder.  (A very successful and RICH professional programmer!!!)  ?  

Professional means you get paid. It says nothing about your abilities or the quality of your code. There are far, far more bad PHP developers out there than good ones.

Link to comment
Share on other sites

@requinix,

So where is a good place to start learning MVC principles?

I'm sure if I Google "MVC", "MVC principles" or "MVC architecture" I will get a million hits.  (And if they are anything like info on OOP, then 90% of them are garbage.)

So since I don't know the "right" way to do things, then how do I know which sources are teaching me the "right" way, and which ones are taking me down a path of failure?

 

Link to comment
Share on other sites

53 minutes ago, SaranacLake said:

So where is a good place to start learning MVC principles?

I really don't know. phptherightway.com is the most reliable place for good PHP advice, but they don't have anything for MVC.

53 minutes ago, SaranacLake said:

So since I don't know the "right" way to do things, then how do I know which sources are teaching me the "right" way, and which ones are taking me down a path of failure?

Typically one discovers that after the fact...

Perhaps the right thing to do would be to pick up a framework like Laravel. Or Symfony. They do all the heavy lifting. If you spend some time working with them, not only do you get to learn about them (a good idea) you'll understand more of how stuff like MVC and a variety of other things work.

Link to comment
Share on other sites

12 hours ago, requinix said:

Perhaps the right thing to do would be to pick up a framework like Laravel. Or Symfony.

Jumping into a framework without even being able to do the basics of procedural code separation, de-duplication, logic, etc.. is not a good move IMO. I liken it to trying to fly a Jet Fighter when you haven't mastered the basics of flying a Cessna. Of course it wont hurt anything to snoop around and try out frameworks (I did as a noob) but the OP has some specific timing goals. If he just wanted to "get the job done" that may be one thing, but the OP has stated he wants to learn. It doesn't take MVC or a framework to "separate my website's PRESENTATION from its LOGIC" as the OP posted. Based on OP's posts across numerous forums, he is not ready for Controllers, Models and the like. A framework like Laravel has a whole ton of "magic" which is just going to confuse the OP.

I go back to my previous post as a good approach for the OP to move forward swiftly. I for one would take some time to review OP's project as a whole if he should make it available on a repo.

So OP,  it's your move. You have at least one expert that will review your project and is willing to guide you towards higher level programming. Who better to teach you how to become a "Jedi" than other "Jedi's"? As you correctly stated, there is a lot of junk out there and you don't know enough to know what is a good tutorial.

Link to comment
Share on other sites

15 minutes ago, benanamen said:

Jumping into a framework without even being able to do the basics of procedural code separation, de-duplication, logic, etc.. is not a good move IMO. I liken it to trying to fly a Jet Fighter when you haven't mastered the basics of flying a Cessna. Of course it wont hurt anything to snoop around and try out frameworks (I did as a noob) but the OP has some specific timing goals. If he just wanted to "get the job done" that may be one thing, but the OP has stated he wants to learn. It doesn't take MVC or a framework to "separate my website's PRESENTATION from its LOGIC" as the OP posted. Based on OP's posts across numerous forums, he is not ready for Controllers, Models and the like. A framework like Laravel has a whole ton of "magic" which is just going to confuse the OP.

I'm certainly not going to be the first person in line to say that Laravel is a good framework, I have to work with it every day and I hate it, but as far as MVC is concerned I think it does a fairly reasonable job of it.

Sticking with the aircraft analogy, I see what I'm suggesting more like sitting in the copilot's seat: as long as one has the mentality of learning how stuff works rather than trying to shoe-horn their experience into it, seeing how a professional system does the job is a good thing. Yes, Laravel has a lot of crappy magic in it, debugging the internals is hell, and the few parts it does well are beyond the scope of learning MVC, but the point is not to copy and paste but to understand the principles. Like for MVC, routing goes through some configuration layer (routing), to a controller, to a view. If I had more experience with other larger frameworks then I'm sure I would rather recommend them.

Have I mentioned I don't like Laravel?

The real basics, like cutting down on procedural coding or improving code quality and reuse, can be learned anywhere, and I think doing that in a framework which already tries to urge you into it is as good a place as any. In fact using a framework is like the middle seat: you don't have the window to look out unless you're sly and you don't have the aisle to stretch out into, so you have to learn to stay within the boundaries established for you. Yes, that analogy was rather turbulent, but I've reached a cruising altitude with this aviation metaphor and I don't want to land while I still have fuel in the tank.

Link to comment
Share on other sites

LOL! I get it. Op would be in the copilots seat but there's no pilot! (Well, okay, the Laracasts are actually pretty good)

Learning Laravel is just that, learning how to USE Laravel when what you really want to learn is how to write your own "Laravel". So sorry to hear you have to work with Laravel every day. I didn't realize things were that rough for you. I laugh when I see ads looking for "Laravel Ninja's". Some fun facts. A current install of Laravel before you even think about writing a lick of code is 7,445 Files and 1,445 Folders. Symphony is a whopping 9,855 Files and 1,843 Folders.

I am coming in for a landing and waiting for the OP in the First Class Pilots Lounge.

Link to comment
Share on other sites

27 minutes ago, benanamen said:

Some fun facts. A current install of Laravel before you even think about writing a lick of code is 7,445 Files and 1,445 Folders. Symphony is a whopping 9,855 Files and 1,843 Folders.

Try picking up React(js). Installing it, its requirements, and the typical packages necessary to work with it, totals something like 16k files and a few hundred MBs.

Link to comment
Share on other sites

2 hours ago, benanamen said:

Jumping into a framework without even being able to do the basics of procedural code separation, de-duplication, logic, etc.. is not a good move IMO. I liken it to trying to fly a Jet Fighter when you haven't mastered the basics of flying a Cessna. Of course it wont hurt anything to snoop around and try out frameworks (I did as a noob) but the OP has some specific timing goals. If he just wanted to "get the job done" that may be one thing, but the OP has stated he wants to learn. It doesn't take MVC or a framework to "separate my website's PRESENTATION from its LOGIC" as the OP posted. Based on OP's posts across numerous forums, he is not ready for Controllers, Models and the like. A framework like Laravel has a whole ton of "magic" which is just going to confuse the OP.

I go back to my previous post as a good approach for the OP to move forward swiftly. I for one would take some time to review OP's project as a whole if he should make it available on a repo.

So OP,  it's your move. You have at least one expert that will review your project and is willing to guide you towards higher level programming. Who better to teach you how to become a "Jedi" than other "Jedi's"? As you correctly stated, there is a lot of junk out there and you don't know enough to know what is a good tutorial.

 

I appreciate the offer, but just like any sane business person wouldn't post their business plans online, there is no way hell I am publishing the codebase for my business on the Internet - not even a "private" Git repository.

My code *does* work, and I have a viable product, so it's not like I can't code.

My objective moving forward is to reduce the time it takes to ship my code.  (There is LOTS of room for improvement there!!)

It's one thing to post sample code, and clearly I am willing to do that, but to "turn over the keys to the kingdom", even to Bill Gates or Steve Jobs ain't gonna happen!

But I am still eager to learn and accept help from others, just not quite on those terms.

I'm sure you can appreciate my stance?!

 

Link to comment
Share on other sites

It's your code, your call.

There is one other option, you could email a zip to me. There is nothing you have written that I could not do myself so you don't have to worry about me stealing it. I was just offering  solutions that will get you on the fast track to where you want to go. So, if emailing to one person doesn't work for you either, then I would say take a break from your project and read a book. Actually, two books, the ones I mentioned previously. If you read and learn what is in them, you wont need us for much of anything, at least for a long time.

Link to comment
Share on other sites

1 hour ago, benanamen said:

It's your code, your call.

There is one other option, you could email a zip to me. There is nothing you have written that I could not do myself so you don't have to worry about me stealing it. I was just offering  solutions that will get you on the fast track to where you want to go. So, if emailing to one person doesn't work for you either, then I would say take a break from your project and read a book. Actually, two books, the ones I mentioned previously. If you read and learn what is in them, you wont need us for much of anything, at least for a long time.

 

Is the Fowler book agnostic?

Will it work for Python and Java as well as PHP?

Do I already need to understand MVC or OOP prior to reading it?

 

Also, can you recommend some exceptional books (or online resources) for learning *proper* MVC?

Based on what I know, it seems that learning MVC before OOP would be the best way to go...

 

Link to comment
Share on other sites

Novice to Ninja covers MVC and OOP. It's just like the name, you start out a Novice and end up a Ninja. I know the title can imply that the book is FOR novices to Ninja expertise level rather than starting as a novice and ending up a Ninja. I have also brought that up to the author, as my first impression was the latter.

Link to comment
Share on other sites

Just now, benanamen said:

Novice to Ninja covers MVC and OOP. It's just like the name, you start out a Novice and end up a Ninja. I know the title can imply that the book is FOR novices to Ninja expertise level rather than starting as a novice and ending up a Ninja. I have also brought that up to the author, as my first impression was the latter.

And your answer to my other questions?

Link to comment
Share on other sites

Is the Fowler book agnostic? The concepts are.

Will it work for Python and Java as well as PHP? The concepts will

Do I already need to understand MVC or OOP prior to reading it?  Not at all. When it says novice, it means it.

Also, can you recommend some exceptional books (or online resources) for learning *proper* MVC? I already did.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.