Guide to writing bad Laravel code

Mantas Donelavicius
5 min readMar 25, 2017

--

This guide will help you to transform from good Laravel developer to noob. It is quite easy, you just need to do the easiest thing possible in short-term. This way you will need to write more code, it will have more bugs and will be harder to understand. Things will always break and everybody will be scared to fix, because every fixed bug will introduce two more.

MVC

When you will get your hands on project written in Laravel you know one thing for sure: they are organizing their code using MVC pattern.

How to fix that? Just create simple .php files like show-article-comments.php, store-comment.php and so on… You will need just one file per page. PHP and HTML code should be in one file:

You will get bonus points if at the top of every PHP file you will have MySQL connection code.

CRUD

CRUD stands for create, read, update, delete. Imagine comment box on the website. You can create, view, update and delete comments. What about users? You can also create, view, update and delete them. Seems like some sort of pattern. Some people even say that all operations on web are just CRUD’s.

But where is fun if you will be able to easily find methods in controller. It is better to name controller methods like: saveToDatabase(), getOneUser(), saveNewUserInformation()…

This way if somebody works with you, they won’t be bored because they will need to constantly guess how controller methods are named.

You will also get a lot of bonus points if this UserController will contain methods like: saveUserComment(), deleteUserComment(), showUserComments().

Don’t ever try to create another file named CommentController like this one:

REST

Some brave souls even think that it is possible to organize URL’s this way.

But who are we kidding? Everybody knows that the right way to do things is (random urls):

/show-all-users
/save-user
/save-comment-for-user
/delete-comment-of-user

It is crazy to think that you need to learn 7 URL’s (for REST) and you can use them everywhere. Much nicer is to name things differently every time. Only creative people can do it.

As always bonus points will be awarded to those programmers who will name their views differently than their URL’s:
show-all-users.blade.php
save-user.blade.php
and so on…

If you name your views after your URL structure, it show your lack of creativity. Also it means that your soul is not adventurous, because you don’t like to search for files in jungle of randomly named folders.

ORM

This is the easiest part. If you write code like this in your CONTROLLER’s, you know that you learned everything I know.

When you change how your database table rows are named you go through every controller and change the code.

When you need to validate that the title is unique, you will also need to change code in all controllers.

If your boss tells you that you need to send email when new comment is added, guess what? Yes, change code everywhere.

If you see code like this, close your eyes, hit CTRL + A and DELETE key.

This example can infect your mind with crazy ideas, that it is possible to change code only in one place to send emails and you don’t need to edit every controller that creates comments.

So what we learned from this important lesson? Your controllers should know how your database rows are named! Don’t forget that!!!

Otherwise it will be to easy to change your code and there won’t be any fun battling with your code when you need to change something.

This is one more example what you shouldn't do with your models.

Editor

The best editor to write your code is Notepad++. But I am not sure about that, sometimes I think that Notepad without pluses is even better!

It is much more fun to write every character by hand. When you have big project and need to rename some function names it get’s even better. Who needs automatic function renaming and different ways to navigate your code like jumping into function or finding where it is used?

So don’t even think about editors like PhpStorm or NetBeans.

Bonus

And as always massive amount of bonus points will be given to those who think that they know programming patterns like SOLID: read more.

Do you have some good advice's? How to name variables, how to align code, how to…

P. S.

Sometimes I mix HTML and PHP code.
Sometimes I reference database rows in controllers.
Sometimes I don’t use REST url naming.

Sometimes it just makes more sense. But only sometimes…

--

--