Should a function have only one return statement

This is a form of thought that seems to have evolved over time.

I remember when I started programming the de facto standard was one-entry, one-exit. And that is still being taught in many OO courses in universities.

However, there are many schools of thoughts (including more recently mine) that come down on the side that a moderate number of return statements can actually enhance code readability.

For example, compare this:

versus this:

which uses guard clauses [Beck] / [Fowler’s Refactoring].

http://sourcemaking.com/refactoring/replace-nested-conditional-with-guard-clauses

and

http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement/36714#36714

and

http://programmers.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from

Refactoring – Replace Nested Conditional with Guard Clauses

Say you’ve got multiple nested if statements. 

It’s not clear what the path of execution is.

Here’s an example with multiple nested ifs:

and without:

  }; 

http://sourcemaking.com/refactoring/replace-nested-conditional-with-guard-clauses

Refactoring – decompose conditional

Complex conditional logic is tricky to debug.

It may seem obvious initially but when you land at the start with an edge case it can be a time sink hole. Especially if you keep on hitting it.

And the more and deeper nested you get the more complex it becomes.

Decompose Conditional is a simple design pattern that takes a complicated if/then/else statement and pulls it apart.

The condition is put in its own method.

And so are the then / else parts.

Extract the conditional and each leg as follows:

Adjust UILabel height

To resize the UILabel height to take into account font size, use:

sizeWithFont constrainedToSize:lineBreakMode: 


e.g. 



http://stackoverflow.com/questions/446405/adjust-uilabel-height-depending-on-the-text


Vertically align text within a UILabel

or, for multiline text:

http://stackoverflow.com/questions/1054558/vertically-align-text-within-a-uilabel

and

http://doing-it-wrong.mikeweller.com/2012/07/youre-doing-it-wrong-2-sizing-labels.html

PixelPumper – a new native app for the Mac for WordPress blogs

If you’ve run a blog you know how tedious updating a web app can be. 

1. open up your WordPress Admin panel 

 2. wait 5 seconds for it to load 

 3. click Add New Post 

 4. wait 5 seconds for it to load 

 5. enter your text and click Preview 

 6. wait 5 seconds for it to load 

 7. make a change and click Preview 

 8. wait 5 seconds for it to load 

 9. click Publish 

 

Or, if you want to make a change, you do a Search, wait 5 seconds, click the post you want, wait 5 seconds, then  go through the entire tedious process again. 

 So, I’m instantly blown away by PixelPumper. It’s a native Mac app that lets you blog to WordPress. The searching is real-time and no tedious waiting other than the post to the server. And even that is quicker ‘cos you’re only doing a 1 way trip (i.e. the content goes to the server rather than a 2 way trip where you’re uploading the content when you click Publish and then waiting for the resulting web page to return).  

 I have absolutely no affiliation with these guys but it absolutely rocks. Check it out here: 

https://itunes.apple.com/us/app/pixelpumper/id595901917?mt=12&ign-mpt=uo%3D4

 

And it’s free! Sound too good to be true? It’s only free whilst it’s in development.

PixelPumper

And I love the drag and drop images!

Blog posted using PixelPumper.

AdSense – yellow box

Couple of reasons:

1. Your AdSense account or ad may not be approved yet

Solution: wait

2. You may have some CSS that's making the background yellow

Solution: change the CSS

If you're running WordPress some installs, apparently (it wasn't the case for me), have the attribute set to yellow by default.

A final note: there are some good WordPress plugins which make the job of adding AdSense into your blog easier.