DEV Community

Cover image for A month of clean code

A month of clean code

Jason McCreary on November 10, 2017

Each week over the past month, I have posted before and after code samples calling out ways to clean up code. The response has been amazing, many r...
Collapse
einenlum profile image
Yann Rabiller

Thanks for your article. I agree with most of your refactoring except the first one. I really disagree with the idea of a method User::createFromCourseEnrollmentRequest.

1/ It's just 4 values that are usually used to create a User (first name, last name, email, password). We can assume there is nothing special related to the context of the Course Enrollment.

2/ It binds your model to a specific Request (so I can imagine to the Laravel Framework) which is IMO a very bad idea. Here it's just moving some code to hide it in the Model, it's not refactoring. Actually, in my opinion, it will lower the reusability and maintenance of your code.
What if you want to use something else than Laravel tomorrow?

For these reasons I would prefer the Before version. But I like the idea of a method to return a User (created or fetched) :).

Collapse
gonedark profile image
Jason McCreary Author

To be clear, there is no actual Model/Request coupling. It's just coincidence that we pass the same object. To your point, we could easily use $request->only() to pluck these values out to pass down to the model.

Collapse
eimihar profile image
Rahimie Ahmad

as for me this static method doesn't introduce a new behaviour to the user model. design pattern wise, it basically uses static factory method (i actually like it), although i preferred to have some sort of EnrollmentRegistrationService for this.

Collapse
rafalpienkowski profile image
Rafal Pienkowski

Nice article. Good that you're pasting examples. You clean controllers code but in my opinion for the big picture you should add code snippet for places where the code is moved to. I think that could be very useful otherwise it looks like you're moving code in other places.
Examples with methods refactoring suit me.
Don't give up with refactoring. Fingers crossed 😁

Collapse
datashaman profile image
datashaman

IMO, it's better to mirror the use of the collection method inside Set::contains() instead of using in_array, since items seems to be a Collection object. You get more functionality that way and it's more Laravel-ish.

return $this->items->containsStrict($item);
Collapse
gonedark profile image
Jason McCreary Author

I see your suggestion. However, it's important to remember an array is a collection. No need to introduce an entire framework just to use one method.

Collapse
datashaman profile image
datashaman

Ah, I see that the other calls are to the Set class size and contains. I misread - I thought it was calling those on items. My bad. You are correct.

Collapse
pabuisson profile image
Pierre-Adrien Buisson

Very interesting, I'll keep on reading your posts, thanks! This is actually one of the area I want to improve these days so your posts come in handy. Just one detail: you should make your screenshot bigger, because they're really hard to read like this, even full-size.

Collapse
gonedark profile image
Jason McCreary Author

Thanks!

Unfortunately, as commented, this is a limitation of dev.to and their image upload functionality. View the original tweets for a better resolution.

Collapse
pabuisson profile image
Pierre-Adrien Buisson

Oh I'm new to dev.to and didn't know that, sorrry for the useless comment. I'll follow up on twitter as well, thanks for the heads up!

Collapse
magnetik profile image
Baptiste Lafontaine

Too bad the screenshots are way to small, they seem really well edited.

Collapse
gonedark profile image
Jason McCreary Author

This is a limitation of dev.to. View my original tweets for a higher res image.

Collapse
magnetik profile image
Baptiste Lafontaine

Awesome, thanks.

Collapse
meanin profile image
Paweł Ruciński • Edited on

For me it looks like a great introduction into usage of Boy Scout Rule.

That's not mine, but read more maybe here

Collapse
gonedark profile image
Jason McCreary Author

Indeed, I've written about this many times.

Collapse
fdutey profile image
Florian Dutey • Edited on

1) To do clean code, you must stop using php
2) Models should not implement every possible way of instanciating them
3) Transfer from one account to another without transaction? This should be in a service.
4) The basic minimum for clean code is to have tests for them. I don't see any
5) No comments?

Collapse
gonedark profile image
Jason McCreary Author

I stopped reading your reply after your first point… Clean code is not exclusive to any one language.

Collapse
biros profile image
Boris Jamot ✊ /

Either your first point is a troll, either it discredits you as a good developer.

Collapse
danielgc profile image
Daniel Gutierrez C.

Excellent article, problem with screencaptures is the (automated?) generated cloudinary image of 880px max width; a lightbox would be great (the zoom mouse cursor tricks us to think that the image file will open in a modal window) or a link to original image would be highly appreciated instead of manual editing the link. What I did was to convert res.cloudinary.com/practicaldev/im... into thepracticaldev.s3.amazonaws.com/i... and similarly in every image in order to watch your nice programming standards.

Who can we contact to express these drawbacks of dev.to?

Collapse
gcdcoder profile image
Gustavo Castillo

Awesome article and thanks for sharing it.

Collapse
gonedark profile image
Jason McCreary Author

For sure! Glad you enjoyed it. Follow me on Twitter to see more next week.

Collapse
bgadrian profile image
Adrian B.G.

I was actually looking for Open Source code to write some refactoring articles, but I couldn't found one :/
I will share your work with my cadets.

Collapse
gonedark profile image
Jason McCreary Author

Awesome. Be sure to share back once you write something up!

Collapse
sarmaaditta profile image
Aditta Chakraborty

This is a really awesome blog. I love this blog. I wanna translate it in Bangla & post in my organization website. Would you like to give me permission to translate & repost it?

Collapse
gonedark profile image
Jason McCreary Author

Sure. Send me the link when you're done.

Collapse
sarmaaditta profile image
Aditta Chakraborty

Can you give me your email address for opening an account of you? I will give the post Author you.

Collapse
sarmaaditta profile image
Aditta Chakraborty

Thank you very much. I will create an account for you. Then I will send you everything. Give me your email address.

Collapse
jochemstoel profile image
Jochem Stoel

I hate to sound stupid but these lines with comment blocks, I can only assume you are generating these? Using what? Or did you only manually do them for the sake of example?

Collapse
gonedark profile image
Jason McCreary Author • Edited on

Yes, I added them to a screenshot of the code for the sake of example.

Collapse
jochemstoel profile image
Jochem Stoel

Shit.

Collapse
paveltrufi profile image
Pavel Razgovorov

The screenshots are a little bit hard to see (too small) :(

Will try later from my phone xD

Collapse
biros profile image
Boris Jamot ✊ /

Hi, great article on such a simple concept!

It seems that the $request argument is missing on line 7.

Thanks!

Collapse
gonedark profile image
Jason McCreary Author

You are absolutely correct. Unfortunately it's an old screenshot and I'm too lazy to redo it. :)

Collapse
hamdirizal profile image
Hamdi Rizal • Edited on

Really, the only complaint I have in this post is the screenshots are too small. I barely able to read them.

Collapse
webwizo profile image
Asif Iqbal

Ah, great article.

Nice tips for cleaning your code and it is readable with comment on each line.

Thanks.

Collapse
axetrodome profile image
Axel Mhar

Nice Article

Collapse
saviobosco profile image
Saviobosco

Thanks for the wonderful tips.

Collapse
josephemmanuel profile image
joseph emmanuel kayode (iemarjay)

Great Article
IMO clean code is not just about architectures... a code is also cleaner when it can communicate it's intention without the use of comments. like example one

Collapse
gbkhellohello profile image
GBKSOFT

Thanks for your article, Jason.

Collapse
nicolaecasir profile image
Nicolae Casîr

Awesome!!!

Collapse
manjuanand021_20 profile image
Manjuanand

Thanks Jason. These tips really helped me.

Collapse
gonedark profile image
Jason McCreary Author

Great. Check out BaseCode for even more practices.

Collapse
johhnyalmi91 profile image
Johhnyalmi91

What's the code editor on screenshots? Vim or atom?

Collapse
gonedark profile image
Jason McCreary Author

Atom with the default theme.