coding-style

Why use #if 0 for block commenting out?

Reverse engineering code and I'm kind of appalled at the style, but I wanted to make sure there's no good reason for doing these things.... Is it just me or is this a horrible coding style if ( pwbuf ) sprintf(username,"%s",pwbuf->pw_name); else sprintf(username,"%d",user_id); And why wrap code not intended for compilation in an #if...

Style Question: if block in or around function?

Let's say that I have a function that should only execute if some constant is defined. which of the following would be better option 1: wrap all the function calls in an if block: if(defined('FOO_BAR_ENABLED')) { foobar(); } I figure this way the intent is more clear, but it requires checking the constant every time the function i...

List of highly-regarded PHP style guides?

I've recently been shaking up my coding habits and have been toying with various approaches to coding style... things like using a space between method call parentheses and the parameters they contain, how to deal with multi-line array definitions and method calls, etc. I'm wary of creating my personal flavor of everything, though, and ...

Module name redefines built-in

I'm making a game in Python, and it makes sense to have one of my modules named 'map'. My preferred way of importing is to do this: from mygame import map As pylint is telling me, however, this is redefining a built-in. What's the common way of dealing with this? Here are the choices I can come up with: 1) Ignore the pylint warnin...

String comparison order in Java

Are both the String comparison methods below considered to be equal public class TestString { public static final String CONSTVAL="foo"; public boolean testString1(String testVal) { return testVal.equalsIgnoreCase(CONSTVAL); } public boolean testString2(String testVal) { return CONSTVAL.equalsIgnoreCase...

PEP8 and PyQt, how to reconcile

I'm starting to use PyQt in some projects and I'm running into a stylistic dilemma. PyQt's functions use camel case, but PEP8, which I prefer to follow, says to use underscores and all lowercase for function names. So on the one hand, I can continue to follow PEP8, meaning that my code will have mixed functions calls to camel case and ...

Shorthand for referring to Perl/Moose package names?

In both Python and Java we have import to eliminate the repetition of fully-qualified package/module names throughout code. Is there any equivalent in Perl/Moose? I think it would really make Moose nicer to use if we didn't have to repeat MyApp::Model::Item. Instead, I'd like to [somehow declare] MyApp::Model::Item; and later on, simply ...

Proper order of Documentating, Testing, Coding

Hey all, I suppose this is more of an opinion than right / wrong. But I was wondering how you develop - I personally have tried coding before testing as well as test-driven development, and realized how much nicer test-driven development is, if you can hold your horses. But what I'm wondering is how do you develop? Also where does doc...

C# StyleCop - Using "this." prefix for base class members like current class members or not?

StyleCop has a rule about using "this." prefix to calling class members (SA1101). Is this rule holds true about a member (for example a method) of a class which is inherited from its base class. Example: class BaseClass { protected void F1() { ... } } class ChildClass : BaseClass { protected void F2() ...

Is it bad practice to leave commented-out code in production releases

I regularly see production code from developers (large companies and individuals) that contains code that has been commented out. Presumably this removes earlier attempts at achieving the functionality that didn't work for some reason. To my mind, this is messy, but potentially has some benefits e.g. on returning to refactor or extend...

PHP/PDO: style of write many queries on one page?

An example of my scenario is a large setup page for an application, the method I use is for example: //query 1 $stmt = $dbh->prepare("..."); $stmt->execute(); //query 2 $stmt = $dbh->prepare("..."); $stmt->execute(); Would this be an accepted method to write more queries? I have no clue how it's supposed to be done (or who does what,...

Is there a style checker for c++?

I have worked with java for a while now, and I found checkstyle to be very useful. I am starting to work with c++ and I was wondering if there is a style checker with similar functionality. I am mainly looking for the ability to write customized checks. ...

Replacing if(x) Foreach() with Foreach.Where(x)

Probably a stupid question but I have a lot of: if(X) { foreach(var Y in myList.Where(z => z == 1) { } } constructs in some code Is replacing it with foreach(var Y in myList.Where(z => X && z == 1) { } insane? It is probably less readable, but will the compiler optimize it to make it pretty much the same code? ...

Project-specific Checkstyle configuration in Eclipse

Is there a way to define a configuration XML for Checkstyle in Eclipse that would be project specific, as in "you put a XML file in the project tree and Checkstyle recognizes it"? As far as I've researched, I've seen how to change the configuration for Eclipse as a whole, and how to include a Supressions Filter, which (as far as I under...

Coding Style: lock/unlock internal or external?

Hello, Another possibly inane style question: How should concurrency be locked? Should the executor or caller be responsible for locking the thread? e.g. in no particular language... Caller::callAnotherThread() { _executor.method(); } Executor::method() { _lock(); doSomething(); _unlock(); } OR Caller::callAnothe...

Lazy Loading Question

Which is the better approach public class Account { public UserAccount GetUserDetails(string acctId) { return new UserAccount().GetDetails(); //call method from class UserAccount } public UserAccount GetUserDetails(string acctId) { return new UserOtherDetails().GetDetails(); //call method from another class } } or...

Encapsulating functions in a second .java file?

Hello! I've been messing with Android for a couple of weeks, i found many tutorials to follow, but i didnt find anywhere some "Style rules" to make the code looks better. I would like to know if its possible (im sure that it is, but dont know how to make it xD) to use more .java files to organize the functions. I mean, right now, i hav...

Is there anything wrong with using private classes in my Java Servlet?

I have Servlet that looks something like this: public class MyServlet extends Servlet { private class Page { private Page(HttpServletRequest request, HttpServletResponse response) { /* Do stuff */ } } protected void doGet(HttpServletRequest request, HttpServletResponse response) { Page page = new Page(request...

Objective-C Class Prefixes

What's your preference with naming ObjC classes? I'm a little uncertain what would be the most reasonable approach on this so it would be nice to hear some other opinions. Apple recommends prefixing cocoa classes, because ObjC doesn't support namespaces. The Google ObjC styleguide (which i mostly aim for) does away with them, unless you...

What are the advantages of using a container element for lists in XML?

When specifying XML formats that contain a list of items, there is often a choice of at least two distinct styles. One uses a container element for the list, the other doesn't. As an example: if specifying a document with multiple pages, one could do this: <document> <title>...</title> <pages> <page>...</page> <page>...</pag...