We’re rewarding the question askers & reputations are being recalculated! Read more.
203

I had the same question about a year ago so I looked at some code myself. Here is what I found (constants were ALL_CAPS in every project, by the way): ╔═══════════════════════╦═════════════╦════════════╦══════════════╦════════════╦════════════╗ ║ PHP Project ║ Classes ║ Methods ║ Properties ║ Functions ║ Variables ║ ╠════════════════════...


120

BEWARE! Assertions are removed at runtime unless you explicitly specify to "enable assertions" when compiling your code. Java Assertions are not to be used on production code and should be restricted to private methods (see Exception vs Assertion), since private methods are expected to be known and used only by the developers. Also assert will throw ...


101

This is really between you and your team mates. Nobody else can tell you the right answer. However, if I may dare read between the lines, the fact that you call this style "bad" gives some information that suggests it's better to take it slow. Very few coding styles are actually "bad." There are ones I would not use, myself, but they always have a rhyme or ...


83

Honestly, do you believe having functions which are difficult to use, with 300 lines, is just a matter of style? So following the bad example could keep the overall program in a better state because of consistency? I guess you do not believe so, otherwise you would not have asked this question here. My guess is these functions are so long because in our ...


68

Sure there is a good reason to name it more explicitly. It's not primarily be the method definition that should be self-explanatory, but the method use. And while findById(string id) and find(string id) are both self-explanatory, there is a huge difference between findById("BOB") and find("BOB"). In the former case you know that the random literal is, in ...


46

You need to use an exception. Using an assertion would be a misuse of the feature. Unchecked exceptions are designed to detect programming errors of the users of your library, while assertions are designed to detect errors in your own logic. These are separate issues that should not be mixed. For example, an assertion assert myConnection.isConnected(); ...


42

No. In the book Pragmatic Programmer the author talks about the Broken Window Theory. This theory state: Consider a building with a few broken windows. If the windows are not repaired, the tendency is for vandals to break a few more windows. Eventually, they may even break into the building, and if it's unoccupied, perhaps become squatters or light ...


38

In Java, the answer would be "neither of the above". Best practice would be to assemble pathnames using the java.io.File class; e.g. File assets = new File("images"); File sounds = new File(assets, "sounds"); The File class also takes care of platform-specific pathname separators. There is a separate issue of whether your pathname should start with a ...


37

Nearly every major programming language has a library to handle the directory separators for you. You should leverage them. This will simplify your code and prevent bugs. In my experience, the usual reason for combining strings like this is that they come from different sources. Sometimes it's different pieces from a configuration file. Sometimes it's a ...


36

Advantages of FindById(). Future-proofing: If you start with Find(int), and later have to add other methods (FindByName(string), FindByLegacyId(int), FindByCustomerId(int), FindByOrderId(int), etc), people like me tend to spend ages looking for FindById(int). Not really a problem if you can and will change Find(int) to FindById(int) once it becomes ...


33

You are probably writing code like this: notes_director = argv[1] chdir(notes_director) files = glob('*.txt') rand_file = choice(files) with open(rand_file) as notes_file: points = notes_file.readlines() rand_point = choice(points) You should move this code into a function: def main(): notes_director = argv[1] chdir(notes_director) ...


32

Typically, you'd think that start <= end, so not really. Consider renaming it to give a hint to the business logic you are trying to implement.


26

You will find that the developers themselves are not using short variable names. Whilst developing, they are using meaningful and detailed variable names. Then, in the build/release process, the code they've written is ran through a minifier/obfuscator with the intention of minimizing the size of the file, as a best practise to speed up a website. This is ...


26

I don't think there's something like an 'official' convention. As far as I know, the following is considered good practice by many experienced C# developers: PascalCase for public member variables (string MyName = "James") camelCase for local variables (string myName = "James") _leadingUnderscore for private member variables (string _myName = "James") ...


25

Yes, go for it. Structure != style You're talking about structure, not style. Style guidelines do not (usually) prescribe structure, since structure is generally chosen for its appropriateness for a specific problem, not appropriateness to an organization. Be careful Just be darned sure you're not causing negative consequences in other areas that may ...


24

There is no such thing as "the most popular coding style", it's strictly a matter of your team's conventions and personal preferences. Since you are targeting developers, you should research popular conventions for your platform and follow the one you feel is more convenient, readable and, well, closer to your personal style. For PHP a popular set of naming ...


22

The first naming is just plainly wrong. Want a proof? What does the next piece of code do? if (this.ah == PcX.Def) { this.Z.SecN.Coll(); } The second naming is ok. It's explicit enough, but not too long. It's the one which is used by .NET Framework, so other developers won't be lost. This being said, don't create aliases: you create additional code ...


21

Note that in .NET you should use the Path.Combine method. var path = System.IO.Path.Combine("assets", "sounds"); The reason for this is that it 'knows' the correct characters to be used when constructing the folder names. This takes away the 'problem' of pre or post fixing.


21

Yes. According to PEP8s rule on constants: Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL. Long Version: In the Python community (as in many other communities) exist conventions about how to write code. This is different from working code: ...


19

In the normal course of business, time only works in one direction, and "start" generally comes before "end". I'd be inclined to make "start" the earliest date in the range and "end" the latest. But if your query simply searches for anything between the two dates and doesn't depend on them being in order (which would make for a more robust, easier to use ...


18

In the code you posted, there's no reason to store the function result in a variable before using it. (Unless perhaps the name of the variable is sufficiently meaningful that it makes the code clearer to the reader.) On the other hand, if you need to refer to the value more than once, you should probably store it in a variable rather than calling the ...


16

The convention appears to stem from the Coffman-Graham Algorithm which is designed: "...for arranging the elements of a partially ordered set into a sequence of levels. The algorithm chooses an arrangement such that an element that comes after another in the order is assigned to a lower level, and such that each level has a number of elements that ...


16

What do you mean by "my editor"? And what do you mean by "git -w"? Are you using an editor and command line tools instead of an IDE? May I recommend IntelliJ IDEA? It is the best java IDE ever, and it has no problem with either kind of whitespace, or even with mixed whitespace within the same file. Generally, if a massive change has to be made to the ...


16

Use the package name. This type of problem is precisely why Java uses the package naming convention that it does. It prevents these sorts of problems, whether it's two teams in the same company or two teams on opposite sides of the earth.


15

Why not just name them what they are? That would seem to be the most intuitive to me. function myFunc(recentDate, oldDate)


14

I like this question. The following is from my head but I think it fits quite well. status is used to describe an outcome of an operation (e.g. success/fail). state is used to describe a stage in a process (e.g. pending/dispatched). I also like this definition: status is a final (resulting) state. It is quite clear when applied to programming. Much less ...


13

Just a guess: Tree structures grow downward (root at top, leaves at bottom) because people read from the top of the page toward the bottom. Furthermore, if you were to draw a large tree that spanned several pages, it would be awkward to ask the reader to skip ahead a few pages and then work backward. Furthermore, whether the convention started for the ...


13

First what would make sense in your application if both dates were the same day? In my opinion the obvious answer is 0 and if that's your answer as well then the logical conclusion is to map dates that differ by one day to 1 and not 2. If you map dates that differ by a day to 2 then you have a gap and there is no way to map dates that don't coincide to 1. ...


13

The short answer is, of course, whether you want to break with naming conventions for what are essentially constants... Quoting from the JLS: Constant Names The names of constants in interface types should be, and final variables of class types may conventionally be, a sequence of one or more words, acronyms, or abbreviations, all uppercase, with ...


Only top voted, non community-wiki answers of a minimum length are eligible