Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BootLoxes

macrumors 6502a
Original poster
Apr 15, 2019
745
857
So I have been doing ipad swift playgrounds for about a week now. Ive been doing c# with unity for about a year and I have been taking the approach of just making it work.

However the playground kept saying in the exercises to create the most efficient code I can think of to solve the solution. And while it did take more brain work I was able to find ways to do the tasks with much less code.

Now, prior to playgrounds it never occurred to me to do this since I am self taught and no one ever told me. So it got me thinking about the concept of making it work and being fast, or making the code more efficient at the cost of speed.

Should I always go with the cleanest and most efficient code I produce or are there situations where just getting it working is preferred?
 

casperes1996

macrumors 604
Jan 26, 2014
7,392
5,464
Horsens, Denmark
Well, there are many, many different metrics here. Lines of code to do something, legibility, memory consumption, speed of execution, testability.

The wisdom of the field is commit horrible sins, but clean up after yourself. So make it work first, clean up afterwards. But never neglect cleaning it up. You'll regret that in the long run. I'd rate the importance of each of my mentioned metrics in order thusly:
1: legibility. The easier the code is to read the better.
2: Testability. The easier it is to write test cases for the better.
3: Balance of the rest. - The two above are important enough to allow for a lot of extra memory used, a lot slower execution or a lot more work (though often that means less legible too, so many lines of code is typically not gonna happen if you prioritise 1). But if you can save 20MB at the expense of 4x extra CPU work it's not good. Likewise 10% extra speed for 20GB isn't either.
But remember that few lines of code doesn't necessarily mean efficient execution.

I recommend LeetCode code challenges. They tell you how fast and how much memory your solution used compared to other solutions in the same language.
There's also the game Human Resource Machine that's pretty good at teaching efficient execution on a machine instruction level, albeit abstracted into a game format.
 

BootLoxes

macrumors 6502a
Original poster
Apr 15, 2019
745
857
Well, there are many, many different metrics here. Lines of code to do something, legibility, memory consumption, speed of execution, testability.

The wisdom of the field is commit horrible sins, but clean up after yourself. So make it work first, clean up afterwards. But never neglect cleaning it up. You'll regret that in the long run. I'd rate the importance of each of my mentioned metrics in order thusly:
1: legibility. The easier the code is to read the better.
2: Testability. The easier it is to write test cases for the better.
3: Balance of the rest. - The two above are important enough to allow for a lot of extra memory used, a lot slower execution or a lot more work (though often that means less legible too, so many lines of code is typically not gonna happen if you prioritise 1). But if you can save 20MB at the expense of 4x extra CPU work it's not good. Likewise 10% extra speed for 20GB isn't either.
But remember that few lines of code doesn't necessarily mean efficient execution.

I recommend LeetCode code challenges. They tell you how fast and how much memory your solution used compared to other solutions in the same language.
There's also the game Human Resource Machine that's pretty good at teaching efficient execution on a machine instruction level, albeit abstracted into a game format.

I just registered for an account on LeetCode. Thank you
 

MEJHarrison

macrumors 68000
Feb 2, 2009
1,522
2,723
Since it sounds like you're doing this as a hobby and not as a living (and it's still good advice even if you're getting paid), I'd encourage you to think about better ways to do things from time to time. Google it. Read a book. Ask online. That way you'll keep learning and improving. Always striving to be better will sharpen your skills. Too much "just make it work", especially without much peer review, might lead to just being stagnant in your development as a developer. I've worked with those people. They're very consistent in churning out crap.

In the professional world, you don't always get the luxury of moving at your own pace, so sometimes you absolutely have to make it work. I had that argument with a co-worker recently. He was upset about the state of source control branches which was a valid argument (ironically we'd swapped teams recently so it was his mess I inherited). My argument was "production is broken". The only thing I've ever seen that beats that is "payroll is down". Sometimes you just need to get it done and worry about the rest later.
 
  • Like
Reactions: casperes1996

BootLoxes

macrumors 6502a
Original poster
Apr 15, 2019
745
857
Since it sounds like you're doing this as a hobby and not as a living (and it's still good advice even if you're getting paid), I'd encourage you to think about better ways to do things from time to time. Google it. Read a book. Ask online. That way you'll keep learning and improving. Always striving to be better will sharpen your skills. Too much "just make it work", especially without much peer review, might lead to just being stagnant in your development as a developer. I've worked with those people. They're very consistent in churning out crap.

In the professional world, you don't always get the luxury of moving at your own pace, so sometimes you absolutely have to make it work. I had that argument with a co-worker recently. He was upset about the state of source control branches which was a valid argument (ironically we'd swapped teams recently so it was his mess I inherited). My argument was "production is broken". The only thing I've ever seen that beats that is "payroll is down". Sometimes you just need to get it done and worry about the rest later.

Yes this is just a hobby of mine. I usually wake up at 5am to do 2 hours of programming before I have to get ready for work. But I am starting to want to work on improving the quality of my code as I learn more and gain experience. It may one day become a career backup so I want to have a decent standard within my code. I personally do not know any places where people would look at a random guy's code though so I have been using my best judgement.
 

casperes1996

macrumors 604
Jan 26, 2014
7,392
5,464
Horsens, Denmark
Yes this is just a hobby of mine. I usually wake up at 5am to do 2 hours of programming before I have to get ready for work. But I am starting to want to work on improving the quality of my code as I learn more and gain experience. It may one day become a career backup so I want to have a decent standard within my code. I personally do not know any places where people would look at a random guy's code though so I have been using my best judgement.

if you want I’ll offer code review services. I’m a computer science student.
Do you know a about uml diagrams?
I recommend the Bible of software design

Though often programming isn’t black and white. There’s so many ways to solve an issue and your best judgement is as good as anything. But the book I mentioned gives lots of tools for making more informed judgement calls.
 

BootLoxes

macrumors 6502a
Original poster
Apr 15, 2019
745
857
if you want I’ll offer code review services. I’m a computer science student.
Do you know a about uml diagrams?
I recommend the Bible of software design

Though often programming isn’t black and white. There’s so many ways to solve an issue and your best judgement is as good as anything. But the book I mentioned gives lots of tools for making more informed judgement calls.
I had no idea such a book even existed. Going to pick up the kindle copy to read during my downtime. Thank you
 

casperes1996

macrumors 604
Jan 26, 2014
7,392
5,464
Horsens, Denmark
I had no idea such a book even existed. Going to pick up the kindle copy to read during my downtime. Thank you

You're welcome :)
Don't worry about not necessarily understanding the code in the book. Understanding the design patterns is the important part. Then you can adapt it to whatever language you're working with when it's relevant. I am wiring a lot of Java these days and so many of these design patterns are so useful. Book wasn't written for Java, but good programming principles transcend languages. - Well, in this case it's mostly oriented towards Object Oriented languages, but that, at least to an extend, includes Swift.
This book is really considered "THE" book of design patterns for Object Oriented programming. In the industry, many of these patterns are just referred to by name with an expectation of common understanding. I.e. "We should use a singleton for this", or "We should use the strategy pattern to solve this", or a decorator and so on. It's even in a lot of Documentation. If you look at Apple's documentation for the App object, they describe it as a singleton, which as a concept came from this book :)
 

BootLoxes

macrumors 6502a
Original poster
Apr 15, 2019
745
857
You're welcome :)
Don't worry about not necessarily understanding the code in the book. Understanding the design patterns is the important part. Then you can adapt it to whatever language you're working with when it's relevant. I am wiring a lot of Java these days and so many of these design patterns are so useful. Book wasn't written for Java, but good programming principles transcend languages. - Well, in this case it's mostly oriented towards Object Oriented languages, but that, at least to an extend, includes Swift.
This book is really considered "THE" book of design patterns for Object Oriented programming. In the industry, many of these patterns are just referred to by name with an expectation of common understanding. I.e. "We should use a singleton for this", or "We should use the strategy pattern to solve this", or a decorator and so on. It's even in a lot of Documentation. If you look at Apple's documentation for the App object, they describe it as a singleton, which as a concept came from this book :)
Is there an updated version of the book? I noticed the book is well over 2 decades old.
 

casperes1996

macrumors 604
Jan 26, 2014
7,392
5,464
Horsens, Denmark
Is there an updated version of the book? I noticed the book is well over 2 decades old.

It is old, yes, but nothing in it needs to be updated. I don't know of any newer editions, but there's no need for it anyway. Citizen Kane is from the early 40's and is as good a film now as it was back then, and doesn't need to be remade in color with new actors and such. It wouldn't be the same anyway and probably not as good.
Good design patterns hold, and what this book teaches has withstood the test of time. Its design principles are still widely used and regarded today, and in many ways still forms the basis for my university class on software architecture.

If you want something newer anyway though, my professor has written a book called "Flexible, Reliable Software: Using Patterns and Agile Development". It teaches the design patterns from the Gang of Four as well, though not all of them and in as great detail, we use both books. It uses Java as the language and has challenges to practice the patterns since it's a textbook. Professor's name is Henrik Christensen if you're curious.
But despite the age, I still highly recommend the Gang of Four
 

BootLoxes

macrumors 6502a
Original poster
Apr 15, 2019
745
857
It is old, yes, but nothing in it needs to be updated. I don't know of any newer editions, but there's no need for it anyway. Citizen Kane is from the early 40's and is as good a film now as it was back then, and doesn't need to be remade in color with new actors and such. It wouldn't be the same anyway and probably not as good.
Good design patterns hold, and what this book teaches has withstood the test of time. Its design principles are still widely used and regarded today, and in many ways still forms the basis for my university class on software architecture.

If you want something newer anyway though, my professor has written a book called "Flexible, Reliable Software: Using Patterns and Agile Development". It teaches the design patterns from the Gang of Four as well, though not all of them and in as great detail, we use both books. It uses Java as the language and has challenges to practice the patterns since it's a textbook. Professor's name is Henrik Christensen if you're curious.
But despite the age, I still highly recommend the Gang of Four

I couldnt help but notice you saying "gang of four"

What exactly is that? The book you linked earlier goes under a different name
 

acorntoy

macrumors 68000
May 25, 2010
1,997
2,211
I don’t code, I’m just a massive fan of efficiency. Efficiency, always go for efficiency.
 

casperes1996

macrumors 604
Jan 26, 2014
7,392
5,464
Horsens, Denmark
I couldnt help but notice you saying "gang of four"

What exactly is that? The book you linked earlier goes under a different name

Sorry.That was a bit colloquial. Gang of Four does refer to the book I linked. Or rather, it refers to there for our authors of the book. They are known as the Gang of Four and this is their most popular work so it is commonly referred to as just the gang of four (book) or GoF. If you check the Wikipedia page for the book it also says “commonly called Gang of Four”.
 

BootLoxes

macrumors 6502a
Original poster
Apr 15, 2019
745
857
Sorry.That was a bit colloquial. Gang of Four does refer to the book I linked. Or rather, it refers to there for our authors of the book. They are known as the Gang of Four and this is their most popular work so it is commonly referred to as just the gang of four (book) or GoF. If you check the Wikipedia page for the book it also says “commonly called Gang of Four”.

ah ok. I did not know this lol. Thank you.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.