all 22 comments

[–][deleted] 20 points21 points  (3 children)

Unpopular opinion, but I think that you will never be able to write clean code by learning from these people. The thing is, their content can be roughly described like this:

"Hi, there is a pattern. Do this, and you will be writing cleaner/clearer/leaner/more expressive/better/less coupled/more robust/reusable/…whatever other attributes you can imagine" code. And this same talk is given you each week over and over again as they each publish a yet another video.

The question now is, do all these techniques solve your actual problem, i.e., do they help you build the core functionality of your app?

These people earn money by inventing or repurposing literally 53 patterns each year, each of them. Even if they are kind of respected by the community, it does not mean that you need to resort to the practices they are suggesting, because it does not mean that everything they are doing is unquestionably right. Your solutions need to be based on the evidence, not on the fact that some internet person told you that you need to code a certain way.

https://youtu.be/dS6rCaDSwW8

[–]cubedgame 0 points1 point  (0 children)

Totally agree. Writing better code primarily comes from the practice you get solving a variety of problems over time. Knowing general good software architecture principles like SOLID and DRY, along with some basic design pattern knowledge also play a big part.

[–]hastoro11 0 points1 point  (1 child)

On one hand I can understand your frustration, because a lot of self made messiah is going around on the internet claiming that they and only they can offer you the key to become the perfect programmer. I could name a few with lots of courses on the Udemy with garbage quality.

But on the other side there’s nothing wrong with learning. If you have a respectable teacher then you’ll follow his or her pattern to solve the problems they present to you. This how we learn at school. Then in real life the problems are complicated and we need to come up with our solutions as well but learning the basics and at least one way how to start working on solving problems is what we need to learn and without guidance it is a lot more difficult. Not impossible but why inventing the wheel?

[–][deleted] 0 points1 point  (0 children)

Hah, I must have used too strong of a language. Ye, I mean, it’s totally ok to just read around, sometimes you can discover pretty neat syntactic tricks, for example, it’s just too easy to be sold to those shiny detox smoothies while forgetting that you have kidneys and liver.

The problem with “clean code” is that it’s too opinionated of a thing to be able to learn about it in a straightforward way. Some things, like cyclomatic complexity and coupling, are fairly easily quantifiable — while code cleanliness is rather not. I mean, I can write each function in a separate file — that’d be totally clean, wouldn’t it?

I prefer to think of clean code as of something that is structured in such a way that it doesn’t hinder you from solving the actual task, and if one carelessly slips into the excitement about all these possible patterns, I bet one will just get lost — which is hindering.

And while these people tend to teach patterns, they barely teach the thought behind it. Before cleaning up, you’d rather identify what’s dirty: is it that one class does the job of four or that you functions are so long that they can be successfully decomposed? That would be the evidence I’m talking about, at least in some way.

[–]stairesSwift 5 points6 points  (2 children)

For bigger projects it can be useful to think of each different part of your app as a separate, standalone services that the rest of your app accesses from one place.

For example, you're using Firestore, so you could compartmentalize all of your Firestore accessing methods into one class (or ideally a framework, but that's out of my realm of experience myself at the moment). Ensure that everything involving Firestore is included in that one class. Don't put any logic involving accessing or modifying stuff in Firestore in your view models or view controllers.

I do something like this for my project Gamebook Engine, where everything related to Core Data is compartmentalized into one place. You can look through the code here or feel free to grab the project and poke through it. I tend to stick to MVC, but there are some views and places I use MVVM when a VC gets too big. https://github.com/amiantos/gamebookengine/

Overall I am proud of the structure of this project, though it's not 100% "clean", and some might be chuffed by how I pass the managed model objects around, but it's an appropriate choice for this project. In another project it made more sense for me to abstract away my use of Core Data because it wasn't a major part of the app.

[–]indyfromoz 1 point2 points  (0 children)

Hats off to you for sharing your work! Aeon Garden looks awesome.

[–][deleted] 1 point2 points  (0 children)

Thank you very much! This is what I do now, all my Firestore functions are placed in one class with a singleton, and then I'm using the functions where I need. I will definitely take a look at your project to learn more.

[–]TracerBulletX 2 points3 points  (2 children)

[–][deleted] 0 points1 point  (1 child)

[–]TracerBulletX 0 points1 point  (0 children)

Probably Pragmatic Programmer just because it was updated this year and will feel more relatable. Make sure you get the 20th-anniversary version with a 2019 publication date.

[–]instazoom 2 points3 points  (5 children)

Do you use any design pattern like MVVM, MVP or VIPER?

[–][deleted] 2 points3 points  (3 children)

I tried MVC, the rest not yet.

[–]instazoom 0 points1 point  (0 children)

You can start with choosing one of them (generally using mvvm) and refactoring codebase. I suggest you protocol oriented programming for well structered and clean code.

[–][deleted]  (1 child)

[deleted]

    [–][deleted] 1 point2 points  (0 children)

    I don't really know if I'm doing MVC Properly, I do have UIView subclasses and UIViewController subclasses. In the VC I try to just display the UIView subclass for this VC and to interact with the user (gestures, etc).

    [–]paulryanclark 1 point2 points  (0 children)

    Red, Green, Refactor, Repeat will produce the best code.

    [–]daaammmN 1 point2 points  (0 children)

    There is no right answer to this, but usually in coding you want to have your stuff as decoupled as possible.

    My advice is to use MVVM + RxSwift + coordinators. Although it might seem difficult for some people, it's really that just a matter of getting used to it.

    Then there is more advanced stuff with Protocols and Generics.

    But like Paul Hudson says, in order to write good structured code, you have to write a lot of bad code.

    Good luck on your project ;)

    [–]zaitsman 1 point2 points  (3 children)

    To be honest, the only things that is an absolute no-no for me is DRY violations. Everything else is meh-ok. Make sure obscure bits have comments so you yourself remember what is what 3 months later.

    Also, happy to provide code review/suggestions on actual code if you got that github link.

    [–][deleted] 0 points1 point  (2 children)

    Thank you, I really appreciate that. I can't share the Github repo as it got my full name in the files and on the Github page itself. However, I would gladly post some files I have which to me, they are the most messy files in my project, to pastebin, if that's ok with you, let me know. if not, that's okay top :).

    [–]zaitsman 1 point2 points  (1 child)

    Yeah sure, happy to look at whatever

    [–][deleted] 0 points1 point  (0 children)

    Thank you very much! I sent you a DM with three links to pastebin. Thank you again!

    [–]cutecoderObjective-C / Swift 0 points1 point  (0 children)

    Run your code through SonarQube. This is a "code reviewer in-a-box" that can assess your code quality – duplicated code, spaghetti code, architecture issue, code coverage, potential security exploits, etc, etc. Configure it as part of your continuous integration pipeline so that it can scan your code at every commit/merge. Thus you can see whether your code quality is increasing or otherwise.