Hungarian notation (ui16Counter...)
Member prefixes (m_)
Interfaces and implementations I don’t agree as this is not the case in .NET framework internals
Should be nouns.
Should be verbs.
Use get / set for accessors. Is for predicates. Not needed for properties.
Say what you mean. Mean what you say.
fetch = get = retrieve
controller = manager = driver
And agree with colleagues !
Do not use the same word for two different concepts :
add != insert != append
Patterns, etc.
… between variables and classes for example.
Avoid useless prefix (cf. IntelliSense)
Avoid unnecessary long names
Functions should do one thing. They should do it well. They should do it only.
A function is doing more than one thing if you can extract another function from it.
Functions with sections should be splitted.
→ The Stepdown Rule
At least, avoid their duplication with polymorphism.
SRP (Single Responsibility Principle)
OCP (Open Closed Principle) :
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
Abstraction is the key.
Low coupling.
Solution : Move the switch to an abstract factory.
Long names are better than short enigmatic names.
“pretty much what you expected”
Difficult to test a lot of arguments.
Output arguments are harder to understand.
… are ugly (SRP : more than one thing).
Solution : split the method.
Also : output arguments should be avoided. If a function must change the state of something, have it change the state of its owning object.
Change the state or return information, don’t do both.
cf. RDUrls class.
Duplication may be the root of all evil in software.
return, break and continue are ok for (short) functions.
No goto.
Comments are always failure.
Don’t comment bad code, rewrite it !
Not in comments !
javadoc for internal code.
Position markers
Closing brace comments
Attribution and bylines
Commented-out code
HTML comments
Nonlocal information
Too much information
Inobvious connection
Function headers
Small files are easier to understand.
I usually remove blank lines between functions because we already have one with the closing bracket of the previous function.
It implies close association.
Concepts that are closely related should be kept vertically close to each other.
In particular :
In my/our code, I/we usually use regions :
Private fields.
Private methods.
Then : public methods.
Ctrl-M shortcuts ease automatic opening and closing of all functions/regions.
So I usually organize my code so that it reads from bottom to top. This might be because I start coding with Pascal and C++ when this order is required by the compiler.
Ex : spaces around operators, precedence, argument separation...
Should not be used anymore.
Breaking indentation should be avoided.
I seldom break this rule for very short properties :
public Name { get { return name; } }
Dummy scopes are dangerous : like while ();
A team should agree on a common set of rules.
Hide implementations.
Procedural code makes it easy to add new functions without changing the existing data structures.
OO code makes it easy to add new classes without changing existing functions.
Procedural code makes it hard to add new data structures because all functions must change.
OO code makes it hard to add new functions because all classes must change.
A module should not know about the innards of the objects it manipulates.
A method F in a class C should only call methods from :
Chains of calls : a().b().c()
Half objects half data structures are the worst of both worlds.
DTO like Active Record should not implement business rules.
Try to write tests that force exceptions, and than add behavior to your handler to satisfy your tests.
ie. not java throws’.
Wrapping 3rd party API is a best-practice.
To avoid if or exception catching for example.
Use the special case pattern : create a class that handles a special case.
Other possibility : return empty enumerable.
I prefer returning null when I use[CanBeNull] and [NotNull] Re# attributes.
Same remark.
Best practice : add dedicated class that encapsulates third party object to hide it and to provide an interface tailored and constrained to the needs of the application.
Do not use third party objects in public APIs.