30 days with swift — Day 03 — Cleaning the code

Guilherme Akio Sakae
2 min readMar 4, 2015

Missed the last day, check here:

This is how the code become better

Structs and enums are a great tool to improve our code amazines, they are simpler than Classes and of course less powerful but sometimes you just don’t need all that power, sometimes a water gun is enough.

The examples try to give you real world applications to help you understand enums and structs, they repeat several times the main concept of enums and structs, by now it’s fixed in my head:

Enum: Use when you have a finite set of options, like a dropdown menu.
Struct: Enums with steroids, you can override the init method and create custom methods, much more powerful.

Day highlights

  • Enums to rescue the code: your code become much more cleaner when you start to use enums, you can use almost always when you have a finite set of options, enums give you concise way to store options.
  • Enum methods: Create custom methods to create new behaviours to you application.
  • Concepts like methods vc functions: basically, methods and functions are the same thing, but methods are part of structs, enums and classes. Functions are functions without “fathers”.
  • Structs have use cases everywhere: you can use struct everywhere, and it’s considered good practice, now we start to see some object oriented stuff here.

Day Three Conclusion

This start to get interesting again, our code starts to look more professional using thins like enums and Structs, with this new organization we start to use some concepts used in object oriented programming, this is the new course by the way.

I can imagine lot’s of cases to use this new knowledge, but in real world programming this isn’t enough, our knowledge so far can only complement all of the other required to create complex applications, but it’s a start no doubt, in the next courses we’re going to start to use OOP (object oriented programming) and close this first half of the track.

See you all tomorrow!

--

--