Anyone experienced in Coding Convention for Objective C ? Please share your knowledge.
Regards,
Quan
On 01/04/2011, at 6:14 PM, Do Hoang Minh Quan wrote:
> Anyone experienced in Coding Convention for Objective C ? Please share your knowledge.
What kind of advice are you looking for? This is a very broad topic.
all the best,
Tony
----------
Tony Arnold
http://thecocoabots.com/
My team and I transferred to ObjC from C++ and Java. We've worked with ObjC for 3 years++. However, sometimes we still get stuck with naming convention.
1/ Is it a good choice if I bring C++/Java naming convention to ObjC? ex: use "a" prefix for parameter, "_" prefix for local...
2/ Do you have any suggestion for naming a class? I currently use "FeatureName" + "base class" = class name (HomeViewController), however some team mates use "Screen" as prefix to identify it inherits from UIViewController (ScreenHome)...etc..
I intend to build a document to force the team to follow naming convention, code convention as defined in the document so I can handle the project easily (maintenance, fix bugs, change devs...)
Thanks,
Quan
> --
> You received this message because you are subscribed to the Google Groups "Australian Cocoaheads" group.
> To post to this group, send email to cocoah...@googlegroups.com.
> To unsubscribe from this group, send email to cocoaheadsau...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cocoaheadsau?hl=en.
>
Thank a lot Robert!On 01/04/2011, at 3:31 PM, Robert Stainsby wrote:Quan, start by looking at Apple's documentation on this topic:Hope this helps.Robert
Dot Notation
Lets get the big one out of the way. Dot syntax is not to be used on any projects that Zarra Studios produces with a singular exception:
CALayer *layer = [CALayer layer]; layer.contents = someCGImageRef;
Braces
- (void)someMethod { //Do something }
@property
Property definitions should be used in place of iVars. As of Xcode 3.2.5, we should no longer be declaring iVars at all. This will help to insure proper memory management. In addition, direct iVar access should be avoided except during initialization of the variable and in the
-viewDidUnload
and-dealloc
methods.
--You received this message because you are subscribed to the Google Groups "Australian Cocoaheads" group.
To post to this group, send email to cocoah...@googlegroups.com.
To unsubscribe from this group, send email to cocoaheadsau...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cocoaheadsau?hl=en.
Cheers, Robert
Zarra's style is interesting. I Like most of it. I have some comments:Dot Notation
Lets get the big one out of the way. Dot syntax is not to be used on any projects that Zarra Studios produces with a singular exception:
CALayer *layer = [CALayer layer]; layer.contents = someCGImageRef;
No dots? Really? I like dots.
Braces
- (void)someMethod { //Do something }
Wrong. He gets that wrong. Just like @alancse
@property
Property definitions should be used in place of iVars. As of Xcode 3.2.5, we should no longer be declaring iVars at all. This will help to insure proper memory management. In addition, direct iVar access should be avoided except during initialization of the variable and in the
-viewDidUnload
and-dealloc
methods.I like this. I don't declare ivars for properties
Zarra's style is interesting. I Like most of it. I have some comments:Dot Notation
Lets get the big one out of the way. Dot syntax is not to be used on any projects that Zarra Studios produces with a singular exception:
CALayer *layer = [CALayer layer]; layer.contents = someCGImageRef;
No dots? Really? I like dots.
Braces
- (void)someMethod { //Do something }
Wrong. He gets that wrong. Just like @alancse
@property
Property definitions should be used in place of iVars. As of Xcode 3.2.5, we should no longer be declaring iVars at all. This will help to insure proper memory management. In addition, direct iVar access should be avoided except during initialization of the variable and in the
-viewDidUnload
and-dealloc
methods.I like this. I don't declare ivars for propertiesAlso ZAssert, DLog, ALog: I'll be poaching these ; )
I always thought m_ had the negative connotation of being Hungarian notation. ivar_ is probably the better choice these days (again personal/team decision). As long as you don't prefix _ for method names or ivars as that is reserved by Apple.
Also, you can declare your private / internal ivars in your .m file as part of your class continuation category:
File: FooBar.m
@interface FooBar ()
{
id internalVar1_;
}
@property (nonatomic, retain) id internalVar1;
- (void)internalMethod;
@end
@implementation FooBar
{
id internalVar2; // Can also declare internal ivars within the implementation block.
}
@synthesize internalVar1 = internalVar1_;
- (void)internalMethod
{
DLog(@"%@ %@", internalVar1, internalVar2);
}
...
@end
For the above to work you have to set your compiler to clang/LLVM 1.5 or higher. Also need to specify: -Xclang -fobjc-nonfragile-abi2 within your "Other C Flags" settings.
Zarra's style is interesting. I Like most of it. I have some comments:Dot Notation
Lets get the big one out of the way. Dot syntax is not to be used on any projects that Zarra Studios produces with a singular exception:
No dots? Really? I like dots.
Braces
- (void)someMethod { //Do something }
Wrong. He gets that wrong. Just like @alancse
Code width
Code should not be set to an arbitrary width. We have wide monitors and an editor that smart wraps the code. Do not limit code to 80 columns or any other arbitrary width unless it is to be printed.
Golden Path
When coding with conditionals, the left hand margin of the code should be the “golden” or “happy” path. We should never see:
- (void)someMethod { if ([someOther boolValue]) { //Do something important } }
That is difficult to read. Instead the code should fail early and fail often:
- (void)someMethod { if (![someOther boolValue]) return; //Do something important }
I've completely switched from the first approach here to the 2nd. Same for the other variants that Zarra goes on to cover. When I did my first programming (on a mighty MONECS system many moons ago) I was taught that approaches like the 2nd here were less computationally efficient & context of the computing power involved at the time that might have made a difference. these days there's no way it can make enough difference to trump the improvement in readability for me.
On Fri, Apr 1, 2011 at 7:44 PM, Pedro fp <bandi...@gmail.com> wrote:Also worth looking at Marcus Zarra's style guide...This one has heavily informed my own style approach.
> Also, you can declare your private / internal ivars in your .m file as part of your class continuation category:
>
> File: FooBar.m
>
> @interface FooBar ()
> {
> id internalVar1_;
> }
You learn something everyday. I didn't realise you could add additional ivars. I knew about adding private methods this way.
Regards
Hmm.. Somehow I missed this feature. Sometimes you have to declare property ivars. For example when you provide a custom implementation of a property setter or something. I always prefix my ivars with m_ to differentiate them from properties.
A point of clarification from Apple's The Objective-C Programming Language: Declared Properties:These synthesised ivars have the same name as the property by default, and can be used in custom setters, getters and elsewhere (typically only init and dealloc methods).
For the modern runtimes (see “Runtime Versions and Platforms” in Objective-C Runtime Programming Guide), instance variables are synthesized as needed. If an instance variable of the same name already exists, it is used.In other words (if I've got the details right), the only time you have to declare property ivars now is for compatibility with Mac OS X 10.5 Leopard and earlier, or for the Mac 32-bit runtime.I'm loathe to comment on a style issue, but I will say this about ivar naming. Synthesised ivars have the same name as the property by default, as do most of the ivars in Apple sample code. Any naming convention that requires a prefix for ivars (such as m_ or ivar_) is just creating extra work and code, given these existing conventions.
For those looking it up in The Objective-C Programming Language guide, they're referred to as class extensions. I've always found it odd that Apple calls them "class continuations" internally, but never in documentation.
Cheers,
Nathan
This style violates two principles I was taught and have adhered to consistently.
· A routine should only have one exit point.
· IF statements should be phrased in the positive
I don’t find your second example easier to read at all.
Bill
Same goes for any other constants or r-values. They should go on the
left of the equality comparison.
--
"Young men know the rules, but old men know the exceptions." — Oliver Wendell Holmes
Cheers,
Nathan
I think this rule is impractical, especially in event-driven environments. Guard clauses are the most concise way for an event consumer to dismiss irrelevant events (e.g. under these conditions, this mouseDragged: event should not be handled).
Martin Fowler's book Refactoring covers guard clauses in "Replace Nested Conditional with Guard Clauses", and Kent Beck defines them as a best practice in Smalltalk Best Practice Patterns. Both are worth reading if you get the chance.
Cheers,
Nathan
To make O()-analysis easy/possible, same reason why Dijkstra didn't like GOTOs.
-- ank
Anybody know of a reason why the images aren't showing on 3.1.3?
Bll
-----Original Message-----
From: cocoah...@googlegroups.com [mailto:cocoah...@googlegroups.com]
Cheers,
Nathan
--
Sent from my iPhone
Tab.image = [UIImage imageNamed:@"Info"];
The help file indicates .image and imageNamed have been available since ios
2.0
Bill
I won't recommend this
> Tab.image = [UIImage imageNamed:@"Info"];
You should add file extension to image path
Tab.image = [UIImage imageNamed:@"Info.png"];
I don't know if you are using xCode 4 or 3.x. However, XCode bypass many warnings that may causes some in-comprehensive bugs.
Quan
ie.
Tab.image = [UIImage imageNamed:@"Info.png"];
Cheers,
Al
Check that you haven't left out the .png in the file names. The earlier SDKs were more forgiving and just assumed .png if it wasn't there.
-----Original Message-----
From: cocoah...@googlegroups.com [mailto:cocoah...@googlegroups.com]
On Behalf Of Do Hoang Minh Quan
From: cocoah...@googlegroups.com [mailto:cocoah...@googlegroups.com]
On Behalf Of Alan Rogers
You should add file extension to image path
Tab.image = [UIImage imageNamed:@"Info.png"];
You need to include the file name extension in code running on anything less
than iOS 4.
void function1()
{
if (foo)
{
return a;
}
return b;
}
void function2()
{
x = function1();
// use x
tidyup();
}
Regards
On 04/04/2011, at 12:40 PM, Bill Tinker wrote:
> The reason is: if you have to run some tidy-up steps, they should all be
> done at the one point, not duplicated at every other exit point.
>
> -----Original Message-----
> From: cocoah...@googlegroups.com [mailto:cocoah...@googlegroups.com]
> On Behalf Of benbritten
> Sent: Monday, 4 April 2011 10:18 AM
>
>
> To be honest; I actually have no idea what the reasoning behind 'only
> one exit point' ever was.
>
>
> If I remember correctly from my old Pascal/Modula-2 days, Pascal, like Java and C#, disallows assignment in boolean expressions outright, never-mind the := assignment operator's different syntax.
>
trudat
-- ank
John.
> void function1()
> {
> if (foo)
> {
> return a;
> }
>
> return b;
> }
I've come more & more to avoid writing more than 1 line where 1 will suffice (so long as the resulting line isn't itself a godawful mess). So where the stuff represented by "return a" & "return b" are (or can be) single line statements I'd write...
void function1()
{
if (foo) return a;
return b;
}
or even...
void function1()
{
return (foo ? a : b);
}
Others?
Cheers, Pedro :-)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"America did not invent human rights, human rights invented America."
Jimmy Carter
> void function1()
> {
> if (foo)
> return a;
>
> return b;
> }
Then again, I think I hate the second example. It's just starting to get too clever for its own good. One liners are nice, but not when I have to think to understand them.
I'd rather parse a few simple lines of code, than one complex line of code.
Tired of Java, C++ C#? - Try Eiffel: simple and powerful
Another approach for better results
www.eiffel.com
> Though I have found some developers run screaming from the ternary operator. I however quite like it (when used appropriately).
Crucial keywords being "when used appropriately". I use the brain pain gauge, if the concise form hurts my brain more to read than the more verbose form then I go with verbose. OTOH I know a PHP coder who uses (condition ? true : false) with quite complex statements in all 3 parts & nested a dozen (& probably more) deep. One glance at a screenful of his code is near migraine inducing for me but he finds it perfectly comprehensible.
Cheers, Pedro :-)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"If we are to be non-violent, we must then not wish for anything
on this earth which the meanest or the lowest of human beings
cannot have."
M K (Mahatma) Gandhi
On 04/04/2011, at 4:01 PM, Jared Kells wrote:
> 50 Emails on coding style is a bit annoying, it's a highly subjective topic.
Cheers, Pedro :-)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"If it is permissible for human beings to harass and humiliate and
ruin other human beings in the name of profit, and to do so without
let or fine or hindrance, then it is not a democratic system we are
talking about, it is a tyranny."
Bob Ellis in 'First Abolish the Customer', Penguin 1998, p100
In the eighties when computing was done on bureau services, an application was found to have such code scattered at various intervals…
For i = 1 to 100,000 : Next
When this was queried with the project manager he explained it was a “Revenue Block” – designed to chew up CPU cycles that were the basis for the billing.
Bill
From: cocoah...@googlegroups.com [mailto:cocoah...@googlegroups.com] On Behalf Of Oliver Jones
Sent: Monday, 4 April 2011 4:36 PM
To: cocoah...@googlegroups.com
Cheers,
Nathan
An argument for the convention of putting the constant on the left.if (nil == something)The compiler won't let you get away with forgetting an "=" character.
Cheers,ChrisOn 04/04/2011, at 11:19 AM, Oliver Jones wrote:I also understand people's aversion of the = character in if conditions due to the possibility of:if (something = nil)when you wantedif (something == nil)But if your compiler isn't warning you about assignment inside an if, get a new compiler.
> It's still totally backwards to how my brain works (where the answer comes after the question) but interesting to know there's method to the madness...
>
Agreed, it's horrible - although C/C++ developers use it all the time.
-- ank
Is that it?!!? I never understood why the result was placed on the left side of the equation.On 04/04/2011, at 11:39, Chris Miles <miles...@gmail.com> wrote:An argument for the convention of putting the constant on the left.if (nil == something)
The compiler won't let you get away with forgetting an "=" character.It's still totally backwards to how my brain works (where the answer comes after the question) but interesting to know there's method to the madness...
> Well, nobody here will care :) but for Java developers, there is also the
> same pattern to avoid a null pointer exception.
>
> Compare:
>
> if ( str!=null && str.equals("im a constant string") ) { ... }
>
> if ( "im a constant string".equals(str) ) { ... }
>
> calling a method on a string is perverse though i use it all the time.
Ironically, you’d do it the other way round in Objective-C.
I'm sure it's already been mentioned, but nowadays there really is no
good reason for writing:
if (value == variable)
The compiler (with the correct warning flags) will warn you if you
accidentally write:
if (variable = value)
I include the following compiler flags in all my projects: “-W -Wall
-Wno-unused-parameter”.
If you actually want to assign a variable within an if statement, you
enclose it in double brackets, like so:
if ((variable = value))
My init methods look like:
if ((self = [super init])) {
// Code here
}
return self;
Although I note that Xcode 4’s template is now:
self = [super init];
if (self) {
// Code here
}
return self;
-- Chris