Coding style is a set of guidelines that helps readability and understanding of the source code.

learn more… | top users | synonyms (1)

9
votes
4answers
679 views

What makes for “good style” in Java? [closed]

I had asked this question on Stackoverflow, and before it got booed off, I received the helpful suggestion from Péter Török that this might be a better place to post it. I've been programming in Java ...
19
votes
2answers
1k views

Is it a good idea to provide different function signatures that do the same thing?

Here is a C++ class that gets constructed with three values. class Foo{ //Constructor Foo(std::string, int, char); private: std::string foo; char bar; int baz; }; All of ...
2
votes
3answers
241 views

Should I make a variable readonly when I modify it, but I don't actually set it outside of the constructor?

Background: I was writing some code. It looked something like this: class X { private List<int> _myList = new List<int>(); public void MyMethod(int x) { ...
9
votes
2answers
933 views

Is this awkward spacing some type of style?

In reading another programmers code, he uses a format I have never seen. E.G. namespace MyNs.HereWeAre {//tab here for some reason public class SomeClass ...
4
votes
2answers
402 views

C++11 Header-only Library: in-class body vs. out-class body code?

BACKGROUND:optional Since 2002 I've been slowly building my own Windows Native C++ library. And let's just say that I have jumped 150KLOC (reusable code) having everything I need and the kitchen sink ...
91
votes
22answers
5k views

Elegant ways to handle if(if else) else

This is a minor niggle, but every time I have to code something like this, the repetition bothers me, but I'm not sure that any of the solutions aren't worse. if(FileExists(file)) { contents = ...
4
votes
6answers
1k views

Clarification of “avoid if-else” advice [duplicate]

The experts in clean code advise not to use if/else since it's creating an unreadable code. They suggest rather using IF and not to wait till the end of a method without real need. Now, this if/else ...
2
votes
4answers
2k views

Microsoft's coding standards for ASP.NET controls

I cannot find any naming standards/conventions in MSDN for naming ASP.NET controls. One of the following standards tends to be used by programmers: lblAddress AddressLabel Address According to ...
-1
votes
1answer
85 views

What should always be mentioned in the first lines of a file released as open source code? [closed]

I am looking for standards regarding open source code release. Most of the time, one may find a copyright statement and a few lines about the project at the beginning of each file, but I am having a ...
0
votes
1answer
194 views

How to format this line according to PEP 8? [closed]

I'm trying to adhere to PEP 8, with a 78 character limit on the length of my lines. I have the following statement: startTime = time.strptime(request.GET.get('st', (dt.datetime.now() - ...
0
votes
4answers
218 views

Coding convention regarding the usage of underscores [closed]

There seems to be divided opinion on this subject, and I wanted to get people's insights on whether they've found using underscore prefixes and suffixes to be helpful with coding or not. You know, ...
3
votes
3answers
321 views

Blank lines in C

I'm working through K&R and I notice that their code is extremely tightly spaced. I haven't developed C since at University, but professionally most source from other languages I've worked in has ...
65
votes
13answers
11k views

Why do most of us use 'i' as a loop counter variable?

Has anyone thought about why so many of us repeat this same pattern using the same variable names? for (int i = 0; i < foo; i++) { // ... } It seems most code I've ever looked at uses i, j, ...
57
votes
16answers
4k views

Is inconsistent formatting a sign of a sloppy programmer?

I understand that everyone has their own style of programming and that you should be able to read other people's styles and accept it for what it is. However, would one be considered a sloppy ...
58
votes
15answers
5k views

Should I use parentheses in logical statements even where not necessary?

Let's say I have a boolean condition a AND b OR c AND d and I'm using a language where AND has a higher order of operation precedent than OR. I could write this line of code: If (a AND b) OR (c AND ...
0
votes
3answers
161 views

which style of member-access is preferable [duplicate]

the purpose of oop using classes is to encapsulate members from the outer space. i always read that accessing members should be done by methods. for example: template<typename T> class foo_1 { ...
20
votes
2answers
9k views

What's wrong with relative imports in Python?

I recently upgraded versions of pylint, a popular Python style-checker. It has gone ballistic throughout my code, pointing out places where I import modules in the same package, without specifying ...
8
votes
7answers
510 views

Commenting/In-Code Documentation Styles

This might be a stupid question, but it's been in the back of my head for a while and I can't find a decent answer anywhere else. I have a teacher who says we should explicitly list each parameter ...
2
votes
2answers
117 views

Helper Methods Placement

Here's a question that's always bugged me. I'm going to use java as an example because I've almost never run into a problem in java where I didn't need to use helper methods in its class structure. ...
3
votes
3answers
894 views

articles in variable names and hard-coding strings

re-edited by author: no this is not 2 questions. This is one question about code review questions containing two separate points. Please do not edit my question. For naming variables, the two sides ...
5
votes
3answers
370 views

Coding Convention: C++ Header/Interface Files

I'm reading through a PDF on C++ programming from this page on Stanford's site. It says that when you're creating a C++ header file for a library interface, use a #ifndef preprocessor command to make ...
2
votes
4answers
104 views

function exit condition on parameter consistency check [duplicate]

When checking for parameter consistency a the top of a function body, what is the best strategy? This one: protected void function(Object parameter) if (parameter == null) return; ...
6
votes
3answers
369 views

Is the usage of internal scope blocks within a function bad style?

There are some (quite rare) cases where there is a risk of: reusing a variable which is not intended to be reused (see example 1), or using a variable instead of another, semantically close (see ...
9
votes
5answers
824 views

What are the downsides of mixing tabs and spaces? [duplicate]

The advantage of using tabs for indentation is that people can configure their editor to use the tab width they are comfortable with. The only argument against this seems to be that people don't want ...
64
votes
19answers
12k views

How important is it to reduce the number of lines in code?

I am a Software developer who works on J2SE (core java). Often during our code reviews we are asked to reduce the number of lines in our code. It's not about removing redundant code, it's about ...
34
votes
14answers
6k views

In languages that don't allow underscores in integer constants, is it a good practice to create a constant for 1 billion?

In languages that don't allow underscores in integer literals, is it a good idea to create a constant for 1 billion? e.g. in C++: size_t ONE_BILLION = 1000000000; Certainly, we shouldn't create ...
4
votes
5answers
1k views

sizeof style: sizeof(type) or sizeof variable?

I've seen two styles of using sizeof for memory-related operations (such as in memset or malloc): sizeof(type), and sizeof variable or sizeof(variable) Which one would you prefer, or would you use ...
5
votes
3answers
545 views

Reason for placing function type and method name on different lines in C

I just started at a company and one of the style comments at my first code review was that the return type and method name should be on different lines. For example, this void foo() { } should be ...
3
votes
5answers
1k views

Programming Style in Large Scale C++ Applications

Recently I've been browsing source code of large applications written in C++ to learn a bit but I couldn't help but notice that most if not all use a lot of IFDEFs and class-less functions (where they ...
17
votes
13answers
2k views

Is it an appropriate use of #define to make typing repeated code easier?

Is there any view on whether using the #define to define full lines of code for simplifying coding is good or bad programming practice? For example, if I needed to print a bunch of words together, I'd ...
0
votes
1answer
80 views

Writing contextually related control statements next to each other [duplicate]

Recently I've been messing around with this. Note that I'm not using LINQ because while this could be easily be done that way, I could do this in any language. Allow me to exemplify: foreach ( var i ...
2
votes
4answers
296 views

How do you decide what code to put into a function?

I started out with a script that was a few hundred lines. Later, I realized I wanted another script that would require much of the same code. I decided to wrap certain areas of the original script ...
7
votes
3answers
677 views

Is relying on implicit argument conversion considered dangerous?

C++ has a feature (I cannot figure out the proper name of it), that automatically calls matching constructors of parameter types if the argument types are not the expected ones. A very basic example ...
1
vote
2answers
126 views

Is repeating links to the same class in a single javadoc comment a bad practice?

I'm currently writing an API and its documentation. For example I have something like this: public interface Event { } public interface Process { } public interface EventProcessor { /** * ...
15
votes
7answers
852 views

Should I write compact code or code with lots of spaces? [duplicate]

I have two friends that have completely different schools of thought on how to lay out their code. The first says that code should be well-indented and use lots of spaces and to name variables ...
3
votes
7answers
644 views

Ensuring that headers are explicitly included in CPP file

I think it's generally good practice to #include the header for any types used in a CPP file, regardless of what is already included via the HPP file. So I might #include <string> in both my HPP ...
1
vote
2answers
171 views

Any suggestions on how to over-rely on Google? [closed]

I don't know whether you Google a lot while you coding. I discover my coding practise always rely on Googling. One of the reason why is I don't familiar with the framework, So, I will do How to XXX in ...
1
vote
5answers
188 views

Is it a good practice to write a method that gets something and checks the value? [duplicate]

Occassinally I have to write methods like this: string GetReportOutputDirectoryAndMakeSureExist() { string path = Path.Combine ( ... ) //whatever logic if(!Directory.Exists(path)) ...
20
votes
6answers
2k views

Why the recent shift to removing/omitting semicolons from Javascript?

It seems to be fashionable recently to omit semicolons from Javascript. There was a blog post a few years ago emphasising that in Javascript, semicolons are optional and the gist of the post seemed to ...
1
vote
3answers
343 views

Code Base with awful code conventions, follow them?

With no time to refactor, when you are working on legacy code, with the most awful conventions, what is the best practice? Will trying to follow better coding style would improve readability or ...
8
votes
2answers
621 views

What does Uncle Bob mean by 'noun phrase names'?

I am reading Clean Code by Uncle Bob. Because I am not a native-English speaker, I couldn't understand following statement: Classes and objects should have noun or noun phrase names like ...
3
votes
3answers
817 views

When are chained assignments (i.e. a=b=c) bad form?

I'm working on a VB.Net WinForms project and found myself writing code like this: this.Fizz.Enabled = this.Buzz.Enabled = someCondition; I couldn't decide whether that was bad code or not. Are ...
42
votes
12answers
3k views

Coding style (do more, then simplify) [duplicate]

I'm a CS student and I have been coding for a few months shy of a year now, and I seem to have developed what I think may be a "bad" habit and I'm wondering if anyone does the same (or whether it's a ...
6
votes
3answers
279 views

Dynamic typing function arguments - how to keep readability high?

Dynamic typing newbie here, hoping for some wizened words of wisdom. I'm curious if there is a set of best practices out there for dealing with function arguments (and let's be honest, variables in ...
3
votes
2answers
391 views

Programming by Intention, Depth-First or Breadth-First?

Say I have the following graph of dependencies between procedures/functions/methods: o / \ v e / \ / \ r f l w That is, function o first calls function v, and then ...
1
vote
4answers
4k views

Forward declaration vs include

Reduce the number of #include files in header files. It will reduce build times. Instead, put include files in source code files and use forward declarations in header files. I read this in here. ...
1
vote
1answer
125 views

Less code or less operation

Sometimes I hesitate between "More code to avoid unnecessary operations" and "less code but with redundant operations". Let me just take an example (Win32 API): I try to paint some controls manually ...
0
votes
2answers
613 views

unique_ptr and references - coding style question

I'm pretty sure this is a question purely about aesthetics but I wanted to get all your opinions on it before I start proliferating this type of code in my codebase. Consider the following code: ...
5
votes
2answers
256 views

Is it OK to have a method return different types based on a parameter?

I'm looking for a reference to clean coding styles that I can pass to a team member. In particular, the rule that a method should not change its return type based on an input parameter. If you need ...
9
votes
6answers
1k views

Should my code be DRY or readable if it can't be both?

I'm writing Ruby code for a simple encryption exercise and have frequently run across this dilemma (the exercise is a solitaire cipher if you must know). It is a question of whether I should pad out ...