coding-style

PHP Newbies: How to write good code [closed]

孤者浪人 提交于 2019-12-30 05:23:11
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . Since many PHP-related questions are very basic here, I'd propose to prepare a collection of tips and tricks. This might be a starting

Is it a good practice to add names to __all__ using a decorator?

吃可爱长大的小学妹 提交于 2019-12-30 00:51:09
问题 Is this a good practice in Python (from Active State Recipes -- Public Decorator)? import sys def public(f): """Use a decorator to avoid retyping function/class names. * Based on an idea by Duncan Booth: http://groups.google.com/group/comp.lang.python/msg/11cbb03e09611b8a * Improved via a suggestion by Dave Angel: http://groups.google.com/group/comp.lang.python/msg/3d400fb22d8a42e1 """ all = sys.modules[f.__module__].__dict__.setdefault('__all__', []) if f.__name__ not in all: # Prevent

accessing “module scope” vars

假装没事ソ 提交于 2019-12-29 20:32:48
问题 I'm currently learning Python, and I have to work on a Python 2.7 project. Accessing "module scope" variables in functions of the module itself is a bit confusing for me, and I didn't succeed in finding a satisfying way. My attempts so far: Way 1: my_module.py my_global_var = None def my_func(): global my_global_var my_global_var = 'something_else' Here I think that confusing local and "module scope" vars may be quite easy. Way 2: my_module.py import my_module my_global_var = None def my_func

accessing “module scope” vars

孤街醉人 提交于 2019-12-29 20:32:01
问题 I'm currently learning Python, and I have to work on a Python 2.7 project. Accessing "module scope" variables in functions of the module itself is a bit confusing for me, and I didn't succeed in finding a satisfying way. My attempts so far: Way 1: my_module.py my_global_var = None def my_func(): global my_global_var my_global_var = 'something_else' Here I think that confusing local and "module scope" vars may be quite easy. Way 2: my_module.py import my_module my_global_var = None def my_func

How do I initialize classes with lots of fields in an elegant way?

二次信任 提交于 2019-12-29 11:35:29
问题 In my application, I have to instantiate many different types of objects. Each type contains some fields and needs to be added to a containing type. How can I do this in an elegant way? My current initialization step looks something like this: public void testRequest() { //All these below used classes are generated classes from xsd schema file. CheckRequest checkRequest = new CheckRequest(); Offers offers = new Offers(); Offer offer = new Offer(); HotelOnly hotelOnly = new HotelOnly(); Hotel

How do I initialize classes with lots of fields in an elegant way?

六眼飞鱼酱① 提交于 2019-12-29 11:35:23
问题 In my application, I have to instantiate many different types of objects. Each type contains some fields and needs to be added to a containing type. How can I do this in an elegant way? My current initialization step looks something like this: public void testRequest() { //All these below used classes are generated classes from xsd schema file. CheckRequest checkRequest = new CheckRequest(); Offers offers = new Offers(); Offer offer = new Offer(); HotelOnly hotelOnly = new HotelOnly(); Hotel

Coding Standards for pure C (not C++)

杀马特。学长 韩版系。学妹 提交于 2019-12-29 11:00:12
问题 I come from a java background (from my CS classes) and a semester of C++. I am just finishing up a OpenCV project for my Co-Op that's in pure C, so I'm a bit late in asking this question. What are the design processes and coding standards for pure C? I'm familiar with object oriented programming, design and best practices. I'm just a bit at a loss at a non-object oriented language like C. Every single variable and function appears to be global. It makes it feel like a real mess to me. 回答1:

What is the idiomatic way to handle/unwrap nested Result types?

大憨熊 提交于 2019-12-29 06:59:09
问题 I read that using unwrap on a Result is not a good practice in Rust and that it's better to use pattern matching so any error that occurred can be handled appropriately. I get the point, but consider this snippet that reads a directory and prints the accessed time for each entry: use std::fs; use std::path::Path; fn main() { let path = Path::new("."); match fs::read_dir(&path) { Ok(entries) => { for entry in entries { match entry { Ok(ent) => { match ent.metadata() { Ok(meta) => { match meta

Coding Style Standards for Android

馋奶兔 提交于 2019-12-29 02:21:08
问题 I would like to know if there is some standard code styling for Android(maybe a book?) (styling XML , Java programming , file naming , etc...) 回答1: There is a good description of code style rules here. If you want to enforce this rules without remembering all of them, and you are using eclipse then you can use formatting rules provided by Android team: android-formatting.xml. Just import it into eclipse using Preferences->Java->Code Style->Formatter, click Import. 回答2: There is a ton of

What's the correct way to sort Python `import x` and `from x import y` statements?

和自甴很熟 提交于 2019-12-29 02:20:29
问题 The python style guide suggests to group imports like this: Imports should be grouped in the following order: standard library imports related third party imports local application/library specific imports However, it does not mention anything how the two different ways of imports should be laid out: from foo import bar import foo There are multiple ways to sort them (let's assume all those import belong to the same group): first from..import , then import from g import gg from x import xx