New answers tagged

-1

Over the years there has been a variety of ways to write multi-word names for variables, functions, etc. PROGRAMFORSORTINGLOTSOFTHINGS is hard to read. PROGRAM_FOR_SORTING_LOTS_OF_THINGS is a bit easier. program_for_sorting_lots_of_things is easier yet. ProgramForSortingLotsOfThings does away with the underscore and maintains readability. Powershell ...


1

There are many metrics for code quality, e.g. utility accuracy reliability maintainability, modifiability, extensibility usability portability testability, test coverage security performance internationalizability ... Which metrics apply and their relative importance depends on the situation. E.g. if you're writing a one-off utility program for yourself, ...


0

Neither are. Good code is code that is both efficient during runtime and easy to maintain. Code that run efficiently enough is good because the application runs efficiently enough, but if it is too difficult to maintain, it becomes a technical debt: something that will cost (possibly lots) to work with down the road, and should really be rewritten/...


0

I don't see anything wrong with the table format. Personal preference, but I would use a ternary like this: return i>0 ? sqrt(i) : i==0 ? 0 : 1j * sqrt(-i) No need to repeat return every time :)


1

As enderland has already said, you're assuming you only ever have one "return" as the action, and that you can tag that "return" onto the end of the condition. I'd like to give some extra detail for why this isn't going to be successful. I don't know what your preferred languages are, but I've been coding in C for a long time. There are a number of coding ...


1

If your expression really is that easy most programming languages offer the ?: branching operator: return ( i > 0 ) ? sqrt( i) : ( i == 0 ) ? 0 /* else */ : 1j * sqrt( -i ) This is a short readable tabular format. But the important part is: I see at one glance what the "major" action is. This is a return statement! And the value is ...


4

There is the 'switch' statement that provides this kind of thing for special cases, but I guess that's not what you're asking about. I have seen if statements in table format, but there has to be a large number of conditions to make it worthwhile. 3 if statements are best shown in the traditional format, but if you had 20, then its much easier to display ...


1

Tabular layout can be useful in few limited cases, but there are few times it is useful with if. In simple cases ?: may be a better choice. In medium cases a switch is often a better fit (if your language has one). In complicated cases you might find that a call tables are a better fit. There have been many times when refactoring code that I have ...


59

One reason may be that you're not using languages where it's popular. A few counter-examples: Haskell with guards and with patterns: sign x | x > 0 = 1 | x == 0 = 0 | x < 0 = -1 take 0 _ = [] take _ [] = [] take n (x:xs) = x : take (n-1) xs Erlang with patterns:...


6

Tabular formats can be very nice if things always fit within the allotted width. If something exceeds the allotted width, however, then it often becomes necessary to either have part of the table that isn't lined up with the rest of it, or else adjust the layout of everything else in the table to fit match the long item. If source files were edited using ...


43

I am a firm believer in 'code is read many times, written few -- so readability is very important.' A key thing that helps me when I read other people's code is that is follows the 'normal' patterns that my eyes are trained to recognize. I can read the indented form most easily because I've seen it so many times that it registers almost automatically (with ...


78

It's more readable. A few reasons why: Nearly every language uses this syntax (not all, most - your example appears to be Python, though) isanae pointed out in a comment that most debuggers are line based (not statement based) It starts looking even more ugly if you have to inline semicolons or braces It reads top to bottom more smoothly It looks horribly ...


5

Is there any advantage here that I am missing? Yes, there is. I, along with a large number of software projects, used to follow the practice outlined in the question. Many projects, including modern ones I work on, now take the opposite approach in a source file: The header file that declares the functions being defined in the source file is the very ...


0

No, your ordering of includes is not bad practice. One advantage of putting the local header file first, especially the file that declares the functions whose definition is in the current file, is that you can ensure that your header files are self-contained (they don't depend on any other header files being included before them). Other than that, the ...


1

This is a very broad question, as it depends on the language used, and the features that language offers: Can "plain old data structures" be made immutable? Does the language enforce encapsulation of private functions and data, or is it by convention? Is the language statically or dynamically typed? Does it allow functions outside of static classes? Does ...


0

Classes vs "free data" + functions The freedom of data is always at the expense of the freedom of the programmer: With a POD you're free to do whatever you want. And what you want, you will implement in a nicely overloaded function. But other programmers (or your future yourself) are also free to do whatever they want and express their creativity. They ...


2

This applies to any language. If you don't plan on doing anything with the exception, or as a result of the exception being caught, then don't bother catching it. It saves you a few lines of code and indentation. Seeing an except block could tell other programmers, who are busy and aren't reading every line of code, "Oh good. CrazyPython is handling this ...


-1

actually, it isn't about programming style, is and == are very different: the is operator checks if the two items reference the same object the == operator checks if the objects that the two items reference are the identicle for example: l = [1,2,3,4,5,6,7,8,9,1,2,3,4,] l2 = l # make ir reference the object that l references l3 = l[:] # ...


1

See Luc's valid comment on your question re. URL templates. I would add... anytime you want a magic number that is neither 0 or 1 and is also not referring to a specific independent feature of the business domain, that is a code smell. Your issue here isn't that you're hardcoding a magic number so much as you're hardcoding something which is replicating ...


-1

OK there are two competing questions here. One is what is the 'official' 'best' way to program this and Two is that way actually more readable so here is my 'official' 'best' way public class ChannelInfo { public String ChannelId { get; set; } } public class PathExtractor : IPathExtractor { private const int ...


11

In layman's terms: If you will be extracting the channelID in several places, then you should create a function. Such a function, being cohesive, should read from the state. Part of that state would be the constant with a name like DELIMITERS_BEFORE_ID. I find that this: String channelId = channelIDdWithPath.split("/")[DELIMS_BEFORE_ID]; ...is more ...


9

Your should focus on what makes your code readable and the spirit of the guidelines rather than try to prove you can write bad code even when following the guidelines. A constant named NUMBER_OF_DELIMITERS_BEFORE_ID_IN_CHAT_CHANNEL_PATH is not very clear or understandable. So yes a magic number is probably not any worse than using such a badly named ...


2

DAO code goes into the Model, not the controller. DAO code is actually farther away from the controller than the business logic, and that logic doesn't go into the controller either. Write a layer that contains methods that convert CRUD operations (Create, Read, Update, Delete) into DAO operations. Call the newly-created CRUD methods from your controller ...


1

No rule is absolute in software development and everything depends on context. That being said, I would not consider your example to be necessarily a good case for boolean arguments. An API like show(bool isLoggedIn) does not tell me what is going to happen without me reading documentation or code, whereas functionality of show(string messageToDisplay) ...



Top 50 recent answers are included