coding-style

Returning from a method with implicit or explicit "else" or with a single "return" statement?

Some people consider multiple return statements as bad programming style. While this is true for larger methods, I'm not sure if it is acceptable for short ones. But there is another question: Should else explicitly be written, if there is a return statement in the previous if? Implicit else: private String resolveViewName(Viewable v...

Is it possible to have several different textcolors in one textarea?

I'd like serveral words / phases in a textarea to appear in different colors... How would I go about doing this? Below is an example, I'd like the word green to appear green etc etc... <textarea style="width: 100%; height: 100%; resize: none;"> Is it possible to have multiple colors in a textarea? How would i set certain phases or word...

What should go in CodeIgniter models?

I've always used the model as something to more or less store and execute database queries. I have heard about the fat model, thin controller concept. The way I setup my models right now causes a lot of junk in controllers for things like validating forms, formatting data. Does form validation, file uploading and data formatting belong ...

C++ coding standard for small group using modern IDEs

Hi, We are going to start a new project in our team which consists of less than 10 developers. We have access to modern IDEs such as VS2010. The project is extremely dynamic (users' needs change very quick) and cross platform. Therefore, I need a highly readable and very detailed C++ coding standard so new developers can easily change ...

MVC2 Html Helpers

I generated a View using VS 2010 functionality. This auto code generation created this code: <%: Html.TextBoxFor(model => model.DateDeleted, String.Format("{0:g}", Model.DateDeleted)) %> I created a custom helper method (extension method) to create a JQuery Calendar control. This is the syntax for this control on the View: ...

Code Review question - should I allow this passing of an auto_ptr as parameter?

Consider the following example code which I have recently seen in our code base: void ClassA::ExportAnimation(auto_ptr<CAnimation> animation) { ... does something } // calling method: void classB::someMethod() { auto_ptr<CAnimation> animation (new CAnimation(1,2)); ClassA classAInstance; classAInstance.ExportAnimation(animation) ...

Do you have any personal visual clue in your code for other user and not the compiler ?

i use certain suffix in my variable name, a combination of an underscore and a property. such as : $variable_html = variable that will be parse in html code. $variable_str = string variable $variable_int = integer variable $variable_flo = float variable. Do you have other visual clues? Maybe something you write for variable, functi...

cleanest way to skip a foreach if array is empty

Not a major problem but I was wondering if there is a cleaner way to do this. It would be good to avoid nesting my code with an unnecessary if statement. If $items is empty php throws an error. $items = array('a','b','c'); if(!empty($items)) { // <-Remove this if statement foreach($items as $item) { print $item; } } I could p...

Where to find resources for learning C++ features and possibilities?

What is the problem? For quite a few years I've been coding workarounds and such stuff cause I didn't know there was an "easier" or actually the normal way to do something - and I didn't ask either. Recently I've discovered plenty of can't live without stuff like templates, forward declarations and similar stuff. Now, what I'd like is a...

What are the best practices for safe type conversion in C# ?

Hi, what are the best practices for type conversions in C# ? int temp=System.ConvertToInt32(Request.QueryString["Id"]); if (temp!=null) { // logic goes here } This fails if Id somehow turns out to be 'abc' Please advice the use of of ternary operators and other single line statements apart from if else statements (like u...

Building a long query and have a lot of if statements - is there a more elegant way?

I have to build a query based on certain conditions. Is there a better way of doing it than the way I have done below? It works fine but I can see it getting out of hand fairly quickly if there were more conditions since I check if any previous conditions had been met every time I check a new one. $sql = "SELECT DISTINCT fkRespond...

Suggestion for writing more readable code?

What tips/suggestions do you have for writing more understandable code? I've been somewhat frustrated by the lack of structure and bad format of some code I've been maintaining lately and would like to propose a series of guidelines for writing more understandable code. Any suggestion might help, no matter the language. Regards. ...

exception for notifying that subclass should implement a method in python

Hi! suppose I want to create an abstract class in python with some methods to be implemented by subclasses. (in Python) for example: class Base(): def f(self): print "Hello." self.g() print "Bye!" class A(Base): def g(self): print "I am A" class B(Base): def g(self): print "I am B...

How should I be writing the HTML etc for security roles in the application I've inherited?

Hello everyone, As the title suggests, I've inherited a php/MySQL application which has fairly well written procedural code, but only has one admin user login. The client naturally wants to split out functionality into different users/roles and ultimately update the application code with best-practice techniques. For the example of upd...

How to manage references to permissions in the code and in the database?

How do people manage permissions between their code base and the database? For example, my application is becoming littered with: if($objects['username']['access_type'] == 'edit'){ // print the HTML to edit the username } or in OO: if($user->getPermission('username')->canEdit()){ // print the HTML to edit the username } How...

Why isn't main defined `main(int argc, std::vector<std::string> argv)` ?

This question is only half tongue-in-cheek. I sometimes dream of a world without naked arrays or c strings. If you're using c++, shouldn't the preferred definition of main be something like: int main(int argc, std::vector<std::string> argv) ? Edit: Even better: int main(std::vector<std::string> argv) There are already multiple d...

D/Phobos Style guide

I've just begun looking at the phobos source, and it's littered with several different styles and commented out code. The style guide on the web side is very small, and I only found broken links from 2006 and another one from 2004... Is there a newer, more comprehensive guide available? PS: Originally asked at the D.learn newsgroup, ...

Which of these functions is the most proper in terms style and cleanliness?

I'm having an internal dispute about the right way to do something. Maybe you can help :) Let's say I have the functions foo(x, y) { if (x satisfies condition c1) if (y satisfies condition c2) bar(x) else bar(x) ... } bar(x) { ... } An alternative to this would be foo(x, y) { doBarFlag = false if (x...

Pythonic way of repeating a method call on different finite arguments

I was staring at a piece of Python code I produced, which, though correct, is ugly. Is there a more pythonic way of doing this? r = self.get_pixel(x,y, RED) g = self.get_pixel(x,y, GREEN) b = self.get_pixel(x,y, BLUE) t = function(r,g,b) if t: r2, g2, b2 = t self.set_pixel(x,y,RED, r2) self...

idiomatic way of taking action on attempt to loop over an empty iterable

Suppose that I am looping over a iterable and would like to take some action if the iterator is empty. The two best ways that I can think of to do this are for i in iterable: # do_something if not iterable: # do_something_else and empty = True for i in iterable: empty = False # do_something if empty: # do_someth...