DEV Community

Discussion on: How has your relationship with complexity changed over time?

Collapse
 
youpiwaza profile image
max

When I was younger, I tended to avoid complexity, or always tried to go on it with some support (either colleague or internet).

But now with some more experience, I'll always deal it the same way with ~3steps :

  1. Always have a clear vision of what your are doing : always prepare, don't write a single line of code before having complete comments describing what you are doing.
  2. A complex thing is always the sum of multiples simple things. Just cut the big log in small pieces and it will be fine
  3. Refactoring. Again, before coding, try to spot any design pattern that could improve and simplify your algorithm.

I recommand having a look at "refactoring guru", the website with the badger mascot, as it explains a lot of it, with detailed examples both theoric and with code.


Some more advices :

  • If you find yourself in trouble, go try the MVC design pattern, and try to define wich part of your code goes where
  • If you are in trouble with the algorithm, make a flowchart, or several, until all cases are noted. It really helps making patterns pop to the eye
  • Try to regroup stuff by entities. All the client stuff goes in "Client", all the navigation stuff goes in "Navigation", and so on. It can help to create Classes, if you yo the Object way. Also if it's an info, it will be a property (you just store something). If it does something, or transform something, it's a method. As simple as that.
  • Naming is reaaaally important.

Maybe the best advice I can give is that it will never be flawless the first time, and don't try to make 10+ classes for a start.

Most of the time I do a big old "index.php" or "script.js", code on the fly, and when things are finally starting running flawlessly, and your file is 500+ lines : it's a hint that it is time to split your code into multiple folders/files.

Got client stuff ? Create a "Client" classe.

Need to create a new client ? Create "Client->create( )" method

Client needs name and email ? Update your class : name & email properties. "Client->create(name, email)"

And so on.

Once you feel good and confortable about this, to get to the next level go check Unit Testing, and after that TDD > Test Driven Development.

It will help you thinking about your code as input/outputs and stuff, and can really improve code reliability, maintenance, avoid regression, and also make your code really faster.


Sorry for the big block and the approximate english.
Feel free to ask questions if you have some ;)

Have a nice day