mardi 5 janvier 2016

improving redundant if else query (maybe with design pattern?) AND reduce global variables?

My code works fine, but it seems for me very amateurish.

1) For example: I'm working with bluetooth and I have always to check, whether there is a device and if the user has a connection.

 if isThereADevice != nil && isThereADevice.connected() {
   //do stuff via bluetooth
 } else {
   //do an alternative
 }

This construct exists > 25 in my "project". It would be cool to find a better solution, but my coding experience isn't so good. I would be happy to get some information, code snippets or ideas, which I can google :)

2) The 2nd stupid thing is, that I have a lot global variables. This doesn't look like good style.

For example: In the game at the beginning the user pick a stone. This stone has an ID. Overall I have five UIViewController and in every UIViewController I need this ID.

I just created just a Globals.swift and define:

var globalID: String = "default"

Meanwhile I have > 20 global variables like time, rounds, some objects... When do I use globals in Swift? Do I use them? What is an solution for my ID-problem?

Aucun commentaire:

Enregistrer un commentaire