ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

Good coding conventions - Discussion

<< < (6/8) > >>

superboyac:
Here's a question...
your choice: space, underscore, hyphen...which one do you use if you know none of these things mentioned above?  i always was under the impression that spaces are terrible for anything, so that leaves hyphens and underscores.  And when you are trying to indicate a space, and underscore is the preferred choice.  I only use hyphens when i want to distinguish a space that is less important than an underscore.  such as " book-title_author-full-name".  in cases where no special characters are desireable, i'll use uppercase and lowercase to distinguish, like "BookTitleAuthorFullName".  I've actually thought about this way too much.  >:(

Ath:
What language are you using? Dashes aren't legal in a lot of languages (I can't think of one where they are legal)
-Renegade (December 30, 2012, 09:57 PM)
--- End quote ---
The (good?) old language Cobolw has them. The dialect I've been using (MF) doesn't even accept underscores as a valid character in variable-names, AFAIK :o

Renegade:
What language are you using? Dashes aren't legal in a lot of languages (I can't think of one where they are legal)
-Renegade (December 30, 2012, 09:57 PM)
--- End quote ---
The (good?) old language Cobolw has them. The dialect I've been using (MF) doesn't even accept underscores as a valid character in variable-names, AFAIK :o
-Ath (January 01, 2013, 06:31 AM)
--- End quote ---

Ah! We have one! Didn't know that.


Another option for things like file names is the period. e.g.

this.is.a.file.with.periods.txt

Of course, you can't use them in a lot of programming languages as they already have a meaning, e.g. Perl, PHP, C#, Java, etc.

I sometimes use periods in file names. All depends on where the file will be used.

fenixproductions:
Although I am spaghetti master I would recommend to look here:
http://stylecop.codeplex.com/
Just throw few c# sources from Internet into it to see general styles.

Also worth to check:
http://msdn.microsoft.com/en-us/library/aa260844(v=vs.60).aspx
http://msdn.microsoft.com/en-us/library/xzf533w0%28VS.71%29.aspx
http://msdn.microsoft.com/en-us/library/vstudio/ff926074.aspx

f0dder:
Fair amount of sensible things have already been said - I agree with a lot of it.

I prefer well-named and small (private) functions/methods over comments. Comments are fine for documenting your public APIs, as well as some implementation details, quirks and hacky workarounds - but other than that, I prefer to split into small and well-defined steps, and let the code speak for itself. If you end up with method names that are long or weird, that's often a good indication of a code smell.

However, what one person might find "clever" is what another finds "DRYw". I don't advocate using esoteric language features just for the heck of it, but I'm also not a fan of avoiding anything-but-the-basics because you need to cater to throwaway unskilled outsourced programmers. I routinely use the ternary operatorw, as I find it can make code easier to read (yes, sometimes even with nested ternaries!). It isn't suitable in every situation, and bad use can make code unreadable. I often use it when declaring final variables conditionally, when the logic isn't bad enough to warrant factoring out to it's own method.

Use of the ternary operator doesn't mean I'm in the "as few lines of code as possible" camp, though. I often break condition of if-statements into (properly named) booleans or method calls, which obviously results in more code lines... but also in better readability.

As an example of what some people might find "clever", here's a small snippet of code for dealing with JCRw nodes. It's a somewhat low abstraction to be operating at, but sometimes it's the right tool for the job - but there's so much red tape and overhead to deal with, when all you really want is "possibly the value at some subnodes property, or a default value if subnode or property doesn't exist or something goes wrong." - Java doesn't have Lambdas, but it does have the (much clunkier, syntax-wise) anonymous inner classes.

Without further ado:

--- Code: Java ---public class NodeHelper {        public static final Logger log = LoggerFactory.getLogger(NodeHelper.class);         // ...other overloads omitted         /**         * Returns a property (or, on error, a default value) from a subnode of a given node.          * @param node root node         * @param path "path/to/subnode@property"         * @param defValue default value         * @return given property of subnode, or default value         */        public static Calendar safeGetProperty(Node node, String path, Calendar defValue) {                return safeGetPropertyInternal(node, path, defValue, new Transformer<Calendar>() {                        public Calendar transform(Property prop) throws Exception {                                return prop.getDate();                        }                });        }                private interface Transformer<T> { public T transform(Property prop) throws Exception; };         private static <T> T safeGetPropertyInternal(Node node, String path, T defValue, Transformer<T> transformer)        {                try {                        final String[] pathAndProperty = path.split("@");                        final String subPath = pathAndProperty[0];                        final String property= pathAndProperty[1];                                                if(node.hasNode(subPath)) {                                final Node subNode = node.getNode(subPath);                                if(subNode.hasProperty(property)) {                                        return transformer.transform(subNode.getProperty(property));                                }                        }                }                catch(Exception ex) {                        log.error("safeGetPropertyInternal: exception, returning default value", ex);                }                return defValue;        }}
It's pretty quick-and-dirty code, but it works well enough for production (*knock on wood*). There's still a lot of Java red tape, Scala or C# (or even C++11) lambdas would have made the various safeGetProperty() into oneliners. Still, no superfluous exception handling all over the place, a fair armount of code lines saved (but most importantly, the core logic is centralized - one place to bugfix). And the "cleverness" is hidden as an implementation detail behind a simple-to-use public API.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version