Some Reasons to rid Your Code of Print

The EVIL Print statement

I love a "band-wagon" that I can join up with, sing the anthem and jump on...

So I thought - what are the reasons I DO NOT Like print statements. And it took quite a bit to come up with a list... There really not that bad.


  • They get shipped with your product and then (in the case of iOS) don't do much of anything valuable in production. [For other systems this differs - but generally the output or "console" may or may not be a place the typical end-person sees (user would work better).

  • They are very quick to use... easy and we learn them so early in our practice...

  • My biggest reason - they are littered all through your code base and sprinkled with class & function names. Which are rarely the place you want to jump to when you use a code search technique. So all those - "bogus" search-hits... getting rid of those. Yeah - a big win. Let's focus there.

  • Somebody said they are relative expensive for the OS to perform.

  • ... next ...

My recent experience of learning about OS_LOG has just kinda ... kicked the stone down the road. I've removed my code's print statements and rather wholesale replace them with the simplest thing that might work... the logger() call.

Could this work?

But you can see in my quick and dirty replacement - I've still got that function call - creating bogus search hits for "savePrivate"... so not the best. How do I rid myself of needing the message to contain the function's name?

It occurred to me in the old days of C compilers - we used pre-processor macros to automate this very type of issue... Swift doesn't have a pre-processor. But if you are lucky... you will hit upon the built-in identifiers ... I've yet to find the Apple Documentation for these __FILE__, __FUNCTION__ etc. But I know they exist... Will it work?

NOPE! That did not work as advertised on StackOverflow...

Oh... they are not interpreted inside a string - they are identifiers/constants... and they changed the name to be less ...screaming snake case... and more Swifty. Try:


Self.logger.notice("Tell me your name \(#file) \(#line) \(#function)")

That works! Now with those few tricks - you can replace the function calls in your logger text strings.

One... More... Thing>>>


Self.logger.notice("\(#line) Did Evolution-0028 get implemented? Making #line at the beginning magical?")


[Rumpelstiltskin] 314 Did Evolution-0028 get implemented? Making #line at the beginging magical?


Why - YES. Yes it did.